vtterm.keys

Turn key presses into the bytes a VT100 sends.

key_bytes maps one key code and modifier mask (the SDL-style codes in OmniPython’s keys module) to bytes: letters and symbols on a US layout, Ctrl+letter as a control code, and the arrows, Home, End, Page Up, Page Down, Insert, Delete and F1 to F4 as escape sequences. KeyEvents reads key-down events from a function such as a desktop display’s get_events, and mouse events too when you give it a Mouse.

vtterm.keys.key_bytes(key, mod=0, app_cursor=False)[source]

Return the bytes a VT100 sends for a key, or None for a key it doesn’t send.

Parameters:
  • key (int) – An SDL-style key code (keys.K_a is 97).

  • mod (int) – Modifier mask (keys.KMOD_SHIFT and so on).

  • app_cursor (bool) – The program asked for application cursor keys (ESC [ ? 1 h), so arrows send ESC O A instead of ESC [ A.

Returns:

bytes – What to send, or None.

vtterm.keys.scroll_key(screen, key, mod)[source]

Page the scrollback for Shift+Page Up or Shift+Page Down.

Parameters:
  • screen (Screen) – The screen whose view moves.

  • key (int) – An SDL-style key code.

  • mod (int) – Modifier mask.

Returns:

bool – True if the key was a scrollback key, so it shouldn’t be sent to the program. On the alternate screen it never is.

class vtterm.keys.KeyEvents(get_events, screen=None, other=None, mouse=None)[source]

Bases: object

Reads key presses from an event function and returns their bytes.

Parameters:
  • get_events (callable) – Returns a list of events, such as a desktop display’s get_events. Events with type, key and mod fields that are key-downs are turned into bytes; the rest go to other if given.

  • screen (Screen) – Its app_cursor mode picks the arrow sequences.

  • other (callable) – Called with each event that isn’t a key-down.

  • mouse (Mouse) – A Mouse whose reports for mouse events are returned with the keys’ bytes, in order.

read()[source]

Return the bytes for the keys pressed since the last call, or None.