问题
I'm working on capturing keys pressed to use with an API for fuzzy search results.
Anyways I'm using this code to capture keypresses in my Markup and Directive Controller:
<input type="text"
placeholder="Search"
ng-click="searchPop($event)"
ng-keypress="typingMainSearch($event)">
vs.typingMainSearch = function(e) {
console.log(e.keyCode);
vs.searchPopoverDisplay = true;
};
However when I type a
I get 97
, when I type b
I get 98
.
Those are incorrect values when I compare them to any place I look online.
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Is it possible to listen for arrow keyspress using ng-keypress?
http://jsfiddle.net/YKeye/
Why is this the case? a is suppose to be 65 and b is suppose to be 66:
var key = {
'a': 65,
'b': 66,
'c': 67,
'd': 68,
'e': 69,
'f': 70,
'g': 71,
'h': 72,
'i': 73,
'j': 74,
'k': 75,
'l': 76,
'm': 77,
'n': 78,
'o': 79,
'p': 80,
'q': 81,
'r': 82,
's': 83,
't': 84,
'u': 85,
'v': 86,
'w': 87,
'x': 88,
'y': 89,
'z': 90
}
回答1:
I think what you're looking for is ng-keyup
instead, this will work for you:
<input type="text"
placeholder="Search"
ng-click="searchPop($event)"
ng-keyup="typingMainSearch($event)">
According to Mozilla the keyCode
property of keypress is deprecated.
Also - this is why you saw 97 when clicking 'a' for instance (taken from the Mozilla link):
The Unicode reference number of the key;
You can find the unicode numbering table here.
来源:https://stackoverflow.com/questions/30128431/keycode-displaying-incorrect-code-value-in-angularjs