It's not pretty, but a work around is to bind to keydown
to capture which key has been pressed, and input
if you want to obtain the value, including the key typed:
(function () {
var keyCode;
$('#txtInput')
.on('keydown', function (e) {
// value not updated yet
keyCode = e.keyCode;
// Enter key does not trigger 'input' events; manually trigger it
if (e.keyCode === 13) $(this).trigger('input');
})
.on('input', function (e) {
console.log(keyCode, this.value);
});
}());
If you type 'a' the following occurs:
keydown
fires.
e.keyCode
is set to the ASCII value of the key pressed.
this.value
is ''
(i.e. the same before 'a' has been typed).
input
fires.
e.keyCode
is undefined
.
this.value
is 'a'
.
You can also manually trigger an input
event if the enter (13) key is pressed; input
isn't fired by this key by default.