Reputation: 7949
I've set up my Knockout bindings to have a keypress event because I wish to detect Enter on an input field.
If I have the following:
data-bind="event: { keypress: KeyPress }"
and my view-model has
this.KeyPress = function(data, event) {
console.log(event.keyCode);
}
then the keyCode shows up as expected.
However, I wish to pass an additional parameter to the function KeyPress
, so following the example from here, I now have
data-bind="event: { keypress: function (data, event) { KeyPress('myParam', data, event); return true; } }" />
and my corresponding function now becomes
this.KeyPress = function(p, data, event) {
console.log(event.keyCode);
}
Now, the keyCode
property always returns 0. I can see inside the event
object that the charCode
property is being set for each key-press, but not the keyCode
property (and the charCode
property isn't much use because all non-printable characters are 0).
I can also see that the event
object has an originalEvent
property, but the keyCode
in here is the same, i.e. 0.
So can anyone tell me how to get the keyCode
property propagating through the function literal?
Here is a JsFiddle demonstrating it.
Upvotes: 1
Views: 773
Reputation: 8321
I'm guessing that you get this result 0
only in firefox
so change your code into this:
this.KeyPress = function(p, data, event) {
console.log(window.event ? event.keyCode : event.which);
}
Upvotes: 3