Keyboard Event Handling: onkeydown
Question: Which key did the user press?
Answer: To find out which key the user has pressed, your script should check the
(the key code of the key just pressed)
if the event occured while Shift was pressed)
if the event occured while Ctrl was pressed)
if the event occured while Alt was pressed)
(Similarly, to identify the key that the user just released, use the
Click here and press any key
the key name will be displayed below:
This example uses the function fromKeyCode that maps the
Caution: Do not use the
event is suitable for identifying characters rather than keys;
may or may not occur for special/functional keys, while
events are triggered for practically every key.* This is precisely why we use the
event here: we want to be able to identify every key (or as many keys as realistically possible).
event is not normally triggered for the PrntScrn key;
is triggered for PrntScrn in most browsers.