Angular service for keydown detection
Sometimes, it could be useful to detect a pressed key during a click or another event. Let's say you have a validation button. It would rock if it pops up a confirmation message when you click on it, but not if you hold the shift key, right ?
Angular-keydown makes it really easy!
http://msieurtoph.github.io/angular-keydown
npm i angular-keydown --save
Please, visit http://msieurtoph.github.io/angular-keydown for live examples.
angular.module('', ['msieurtoph.ngKeydown'])
.directive('myTest', ['Keydown', function(Keydown){
return {
restrict: 'A',
link: function(scope){
scope.click = function(){
if (Keydown.shift){
scope.message = 'Shift key has been pressed when clicking!';
} else {
scope.message = 'Shift key nas not been pressed when clicking!';
}
};
}
}]);
});
<button my-test ng-click="click()">Hold shift key and click me</button>
<pre ng-bind="message"></pre>
The service lets you know whether the following keys are pressed :
shift
,ctrl
,alt
up
,down
,right
,left
arrowspgUp
,pgDown
space
,escape
,enter
,tab
For other keys you can look to Keydown.others[_theKeyCode_]
For instance, Keydown.others[65]
will equal true
if the a
key is down.
Keydown.pressed
gives you the list of pressed keys.
For instance, if a
, b
and c
keys are down, Keydown.pressed
will equal [65, 66, 67]