Skip to content

Instantly share code, notes, and snippets.

@jessecurry
Created October 24, 2014 16:20
Show Gist options
  • Save jessecurry/95598f8578d4a7b37a43 to your computer and use it in GitHub Desktop.
Save jessecurry/95598f8578d4a7b37a43 to your computer and use it in GitHub Desktop.
First pass for a keypress directive for Angular.js
.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