Home

KEYPRESSED

KeyPressed is a name used in several programming environments to handle keyboard input events. In the related editors and libraries, it is commonly implemented as an event handler function that executes when a user presses a key. Some environments also expose a boolean variable named keyPressed that indicates when any key is currently pressed. The term is thus both a function and, in some contexts, a state indicator.

In Processing, keyPressed is an event-handling function you can define to run code in response to a

In p5.js, keyPressed is a callback that fires when a key is pressed in the browser. Inside

Common usage patterns include distinguishing between ordinary and coded keys, handling repeated presses, and implementing keyboard

key
press.
When
a
key
is
pressed,
the
function
is
invoked,
and
two
built‑in
variables
provide
details:
key
holds
the
character
value
of
the
key,
and
keyCode
holds
a
code
for
special
keys.
If
a
coded
key
(such
as
an
arrow
key)
is
pressed,
key
will
equal
CODED,
and
keyCode
will
contain
the
specific
value
(e.g.,
LEFT,
RIGHT,
UP,
DOWN).
The
boolean
keyPressed
becomes
true
while
a
key
is
down,
and
keyReleased()
is
called
when
the
key
is
released.
the
function,
key
contains
the
last
key
pressed
as
a
string,
and
keyCode
contains
the
numeric
code.
P5.js
also
provides
constants
such
as
LEFT_ARROW,
UP_ARROW,
and
functions
like
keyIsDown(code)
to
test
the
state
of
a
key
over
time.
controls
for
interactive
programs.
While
the
exact
behavior
can
vary
by
language
and
environment,
keyPressed
generally
serves
as
a
simple,
event-driven
mechanism
to
respond
to
user
input
from
the
keyboard.
See
also
KeyEvent,
KeyListener,
and
related
input
handling
concepts
in
the
respective
platform
documentation.