Py5KeyEvent
Contents
Py5KeyEvent¶
Datatype for providing information about key events.
Examples¶
1def setup():
2 py5.size(200, 200, py5.P2D)
3 py5.rect_mode(py5.CENTER)
4
5
6def draw():
7 py5.square(py5.random(py5.width), py5.random(py5.height), 10)
8
9
10def key_pressed(e):
11 modifiers = e.get_modifiers()
12 msgs = []
13 if modifiers & e.SHIFT:
14 msgs.append('shift is down')
15 if modifiers & e.CTRL:
16 msgs.append('control is down')
17 if modifiers & e.META:
18 msgs.append('meta is down')
19 if modifiers & e.ALT:
20 msgs.append('alt is down')
21 py5.println('key pressed: ' + (', '.join(msgs) if msgs else 'no modifiers'))
1def setup():
2 py5.size(200, 200, py5.P2D)
3 py5.rect_mode(py5.CENTER)
4
5
6def draw():
7 py5.square(py5.random(py5.width), py5.random(py5.height), 10)
8
9
10def key_pressed(e):
11 pressed_key = e.get_key()
12 if pressed_key != py5.CODED:
13 py5.println(f'the {pressed_key} key was pressed')
Description¶
Datatype for providing information about key events. An instance of this class will be passed to user-defined key event functions if py5 detects those functions accept 1 (positional) argument, as demonstrated in the example code. The key event functions can be any of key_pressed()
, key_typed()
, or key_released()
. Key events can be generated faster than the frame rate of the Sketch, making key event functions useful for capturing all of a user’s keyboard activity.
Underlying Processing class: KeyEvent
The following methods and fields are provided:
get_action(): Return the key event’s action.
get_key(): Return the key for the key event.
get_key_code(): Return the key code for the key event.
get_millis(): Return the event’s timestamp.
get_modifiers(): Integer value used to identify which modifier keys (if any) are currently pressed.
get_native(): Retrieve native key event object.
is_alt_down(): Return boolean value reflecting if the Alt key is down.
is_auto_repeat(): Identifies if the pressed key is auto repeating, as faciliated by the computer’s operating system.
is_control_down(): Return boolean value reflecting if the Control key is down.
is_meta_down(): Return boolean value reflecting if the Meta key is down.
is_shift_down(): Return boolean value reflecting if the Shift key is down.
Updated on April 27, 2022 10:44:51am UTC