I'm trying to create an interaction similar to the konami code "up,up,down,down,a,b,a,b, enter" -> something happens.
Is it possible to listen for arrow keyspress using ng-keypress? it seems to not work?
html:
input( ng-keypress='changed($event)' )
Js
$scope.changed = (evt) ->
console.log(evt)
this will not log out arrow key events?
Do I have to roll out my own listeners on the window? if so how can I achieve this in angular?
If you want to perform any event on any specific keyboard button press, in that case, you can use @HostListener. For this, you have to import HostListener in your component ts file. import { HostListener } from '@angular/core'; then use below function anywhere in your component ts file.
You can practice using the arrow keys by clicking anywhere in the following text box and then use the up, right, down, or left arrows to move around in the box. Move from one cell to another cell in a spreadsheet. Use the right and left arrow keys to move between columns and up and down arrow keys to move between rows.
To detect the arrow key when it is pressed, use onkeydown in JavaScript. The button has key code. As you know the left arrow key has the code 37. The up arrow key has the code 38 and right has the 39 and down has 40.
DEMO
$scope.key = function($event){
console.log($event.keyCode);
if ($event.keyCode == 38)
alert("up arrow");
else if ($event.keyCode == 39)
alert("right arrow");
else if ($event.keyCode == 40)
alert("down arrow");
else if ($event.keyCode == 37)
alert("left arrow");
}
EDIT
Change it from ng-keypress
to ng-keydown.
DEMO
<input ng-keydown="key($event)" />
You could use a custom directive to listen for keydown and keypressed events. A possible implementation that i have tested is the following:
var app = angular.module('app', []);
// a helper service to detect the arrow keys from the key codes
app.factory('keys', function() {
var keysEnum = { left: 37, up: 38, right: 39, down: 40 };
var theKeys = [ keysEnum.left, keysEnum.up, keysEnum.right, keysEnum.down ];
function isIn(val) {
var isin = false
if (theKeys.indexOf(val) >= 0) {
isin = true;
}
return isin;
}
function keyFromCode(code) {
var key = 'unknown';
switch (code) {
case 37:
key = 'left';
break;
case 38:
key = 'up';
break;
case 39:
key = 'right';
break;
case 40:
key = 'down';
break;
}
return key;
}
return {
isIn: isIn,
keyFromCode: keyFromCode
};
});
// custom directive to detect the arrow key pressed
app.directive('keypressed', [ 'keys', function (keys) {
return function (scope, element, attrs) {
// listen for the events keydown, keypress
element.bind("keydown keypress", function (event) {
var code = event.which;
// if an arrow key is pressed then do something
if(keys.isIn(code)) {
console.log(keys.keyFromCode(code));
}
});
};
}]);
You could use the above directive like this:
<span ng-app="app">
<input keypressed />
</span>
From the code that you posted it seems that you use an html template engine so in your case you could use the directive like this:
input( keypressed )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With