mokr.input.keyboard

Module Contents

Classes

Keyboard

Class to allow sending key events to emulate a keyboard.

class mokr.input.keyboard.Keyboard(client: mokr.connection.DevtoolsConnection)

Class to allow sending key events to emulate a keyboard.

Use Keyboard.type("text") to type text normally; this will dispacth multiple key events for each character in the given text.

For finer control, use Keyboard.down, Keyboard.up and Keyboard.send_character.

Special keys and modifier key can be sent. To view all key names and their associated event payload, use Keyboard.key_definitions.

Parameters:

client (DevtoolsConnection) – Remote DevtoolsConnection instance.

property key_definitions: dict[str, dict[str, str | int]]

Dictionary of key names and their corresponding event codes.

async down(key: str, text: str | None = None) None

Send a “keyDown” event with the given key. Does not automatically send a “keyUp” event, use Keyboard.up for that.

Modifier keys effect this method, meaning sending Keyboard.down("shift") and then Keyboard.down("m") will type an uppercase “M”.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • text (str | None, optional) – Force an “input” event to be sent, as well. Defaults to None.

async up(key: str) None

Send a “KeyUp” event with the given key.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • text (str | None, optional) – Force an “input” event to be sent, as well. Defaults to None.

async send_character(char: str) None

Send a character char into the parent mokr.frame.Frame. Does not send “keyDown” or “keyUp” event, only sends “keyPress” and “input”.

Modifier keys do not effect this method, meaning sending Keyboard.down("shift") and then Keyboard.send_character("m") will type a lowercase “m”.

Parameters:

char (str) – Character to send, does not accept special characters.

async type_text(text: str, delay: int | float = 0) None

Type characters into the page; whatever element is focused will receive the sent input.

Note that modifier keys do not alter text case, meaning sending Keyboard.down("shift") and typing Keyboard.type_text("mokr") will not type “MOKR”.

Parameters:
  • text (str) – Text to type.

  • delay (int | float, optional) – Time in milliseconds to wait between each character typed. Defaults to 0.

async press(key: str, delay: int | float | None = None) None

Press the given key by sending Keyboard.down and Keyboard.up events, with the given delay in between.

Modifier keys effect this method, meaning sending Keyboard.down("shift") and then Keyboard.press("m") will type an uppercase “M”.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • delay (int | float | None, optional) – Time in milliseconds to “hold” the key down. Defaults to None.