Home

keydown

The keydown event is fired when a key is pressed down on the keyboard. It is part of the KeyboardEvent interface and can be listened for on window, document, or individual elements using addEventListener or the onkeydown handler. The event fires for all keys, including non‑printing keys such as Escape, Arrow keys, and function keys.

In the standard event sequence, keydown occurs before keypress (which is primarily for printable characters and

The event object carries several useful properties. key is the value of the key pressed, reflecting the

Keydown listeners are commonly used for keyboard shortcuts, game controls, or live input handling. They bubble

is
deprecated
in
modern
web
practice)
and
before
keyup.
For
most
keys
you
will
see
a
keydown,
then
a
keyup
when
the
key
is
released;
printable
characters
may
also
generate
a
keypress
event
on
some
platforms,
though
this
is
being
phased
out
in
favor
of
keydown/keyup.
current
layout
and
modifiers
(for
example,
"a"
or
"A"
depending
on
Shift).
code
identifies
the
physical
key
on
the
keyboard
(for
example,
"KeyA"
or
"ArrowLeft"),
which
is
layout‑independent.
keyCode,
which
is
deprecated,
and
which
are
legacy
ways
to
identify
keys.
Other
properties
include
altKey,
ctrlKey,
shiftKey,
and
metaKey
to
detect
modifier
keys;
repeat
indicates
the
key
is
being
held
down;
isComposing
indicates
an
IME
composition
is
in
progress.
Methods
such
as
preventDefault()
can
stop
the
browser’s
default
action
for
that
key,
and
stopPropagation()
can
prevent
the
event
from
bubbling
up
the
DOM
chain.
by
default,
though
capture
can
be
used
if
needed.
Because
behaviors
can
vary
with
keyboard
layout
and
browser,
prefer
e.key
and
e.code
over
deprecated
codes
and
test
across
target
environments.