Created
October 24, 2014 16:20
-
-
Save jessecurry/95598f8578d4a7b37a43 to your computer and use it in GitHub Desktop.
First pass for a keypress directive for Angular.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('jcKeypress', function () { | |
return { | |
template: '', | |
restrict: 'AE', | |
scope: { | |
keyCode: '@', | |
keyAction: '&' | |
}, | |
link: function(scope, element, attrs) { | |
attrs.altKey = attrs.altKey || false; | |
scope.altKey = scope.$eval(attrs.altKey); | |
var keyListener = function(event) { | |
console.log('KeyboardEvent: ' + event.keyCode + ' altKey: ' + event.altKey); | |
if ( event.altKey === scope.altKey ) { | |
if ( event.keyCode === parseInt(scope.keyCode) ) { | |
if ( scope.keyAction ) { | |
scope.$apply(function() { | |
scope.keyAction(); | |
}); | |
} | |
event.preventDefault(); | |
} | |
} | |
}; | |
// window.addEventListener('keydown', keyListener); | |
window.addEventListener('keypress', keyListener); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment