change
event and the keys used to enter the value
The change
event is not something that has keys associated. Please read jQuery's .change()
documentation:
The change event is sent to an element when its value changes. This event is limited to elements, boxes and elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
The example is this:
- You have some input field.
- You enter "
some name
" text into the field.
- Before going to some other field, you decide to change it to "
some test
", eg. by hitting backspace couple times, then you navigate to next field (eg. by using Tab key, or by tapping Next on iOS keyboard etc.),
- The
onchange
event handler is fired, when you navigate to other field (or rather, as soon as the field loses focus), so the information about all keys used to enter value are pretty unrelated.
Solution using keypress
event
To solve that problem, you would need to implement your solution using keypress
event, eg. thanks to jQuery's .keypress()
function. In such case, event object's ctrlKey
attribute (listed eg. here) lets you know about the status of the Ctrl key. Example usage is here: https://stackoverflow.com/a/4604093/548696
Demo for saving keys on keypress
/keydown
/keyup
and reading on change
It is available here:
http://jsfiddle.net/tadeck/vPu94/
The demo clears the recorded keys on focus, saves them on every keypress
event (you can easily edit that and test different cases) and reads them (and outputs on the screen) whenever change
event is fired (so when the changed field leaves focus).