Java Reference
In-Depth Information
means that it's the most reliable way to find out the character that was pressed on
the keyboard.
3. The keyup event occurs when a key is released.
To understand the differences in these events, it is important to distinguish between a phys-
ical key on the keyboard and a character that appears on the screen. The keydown event
is the action of pressing a key, whereas the keypress event is the action of a character
being typed on the screen.
To see an example of this add the following to scripts.js:
Pressing a key will result in the whole document changing color., because the event listener
was applied to the whole document. If you hold a key down, the event will continue to fire,
creating a psychedelic effect on the page.
To see the keyup event working, add the code that uses an anonymous function to show
the exact time the key was released in the console:
addEventListener("keyup", function stop(event){
var date = new Date;
console.log("You stopped pressing the key on " + date);
The keydown and keyup event objects both have an event.keyCode property that
returns a numerical code that represents the key that fired the event; for example, the J key
has a key code of 74. You can find the code for each key on the JavaScript key code page.
The keypress event object has an event.charCode property that returns a numerical
Unicode character code representing the character that will be shown on the screen.
These two properties are similar, but not the same. For example, on a UK keyboard, press-
ing the 3 key results in a keyCode of 51 and a charCode of 51. But if the Shift key is
held down while 3 is pressed the charCode will be 163, but the keyCode will still be
Search WWH ::

Custom Search