Py5KeyEvent

Py5KeyEvent#

Datatype for providing information about key events.

Examples#

def setup():
    py5.size(200, 200, py5.P2D)
    py5.rect_mode(py5.CENTER)


def draw():
    py5.square(py5.random(py5.width), py5.random(py5.height), 10)


def key_pressed(e):
    modifiers = e.get_modifiers()
    msgs = []
    if modifiers & e.SHIFT:
        msgs.append('shift is down')
    if modifiers & e.CTRL:
        msgs.append('control is down')
    if modifiers & e.META:
        msgs.append('meta is down')
    if modifiers & e.ALT:
        msgs.append('alt is down')
    py5.println('key pressed: ' + (', '.join(msgs) if msgs else 'no modifiers'))
def setup():
    py5.size(200, 200, py5.P2D)
    py5.rect_mode(py5.CENTER)


def draw():
    py5.square(py5.random(py5.width), py5.random(py5.height), 10)


def key_pressed(e):
    pressed_key = e.get_key()
    if pressed_key != py5.CODED:
        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 March 06, 2023 02:49:26am UTC