Let’s now dive into keyboard events.
There are 2 types of events when interacting with keyboard events:
keydown
the keyboard key has been pressedkeyup
the keyboard key has been released
keydown
is also fired when the key repeats while the button stays pressed.
While you typically listen to mouse and touch events on a specific element, it’s common to listen for keyboard events on the document object:
document.addEventListener('keydown', event => {
// key pressed
})
The parameter passed to the event listener is a KeyboardEvent.
This event object, in addition to the Event object properties offers us (among others) these unique properties:
altKey
true if alt key was pressed when the event was firedcode
the code of the key pressed, returned as a stringctrlKey
true if ctrl key was pressed when the event was firedkey
the value of the key pressed, returned as a stringlocale
the keyboard locale valuelocation
the location of the key on the keyboardmetaKey
true if the meta key was pressed when the event was firedrepeat
true if the key has been repeated (e.g. the key has not been released)shiftKey
true if the shift key was pressed when the event was fired
The demo is a keylogger that will show you the values of some of the properties I listed above.
Lessons in this unit:
Are you intimidated by Git? Can’t figure out
merge vs rebase? Are you afraid of screwing up
something any time you have to do something in
Git? Do you rely on ChatGPT or random people’s
answer on StackOverflow to fix your problems?
Your coworkers are tired of explaining Git to
you all the time? Git is something we all need
to use, but few of us really master it. I
created this course to improve your Git (and
GitHub) knowledge at a radical level. Launching
May 21, 2024. Join the waiting list!