KeyEvent

Source code in pzp/keys.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class KeyEvent:
    def __init__(self, ch: str, action: Optional[str]) -> None:
        """
        Key Event represents a key action on the keyboard.

        Args:
            ch: Pressed key
            action: Action

        Attributes:
            ch: Pressed key
            action: Action
        """
        self.ch = ch
        self.action = action

    def __str__(self) -> str:
        return f"<{key_to_str(self.ch)}, {self.action or '-'}>"  # pragma: no cover

__init__(ch, action)

Key Event represents a key action on the keyboard.

Parameters:
  • ch (str) –

    Pressed key

  • action (Optional[str]) –

    Action

Attributes:
  • ch

    Pressed key

  • action

    Action

Source code in pzp/keys.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def __init__(self, ch: str, action: Optional[str]) -> None:
    """
    Key Event represents a key action on the keyboard.

    Args:
        ch: Pressed key
        action: Action

    Attributes:
        ch: Pressed key
        action: Action
    """
    self.ch = ch
    self.action = action

KeysHandler

Source code in pzp/keys.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class KeysHandler:
    def __init__(self, keys_binding: Optional[KeysBinding] = None) -> None:
        """
        Keys handler is a collection of bindings of keys to actions.

        Args:
            keys_binding: Custom key binding

        Attributes:
            keycodes_actions: key => action mapping
        """
        self.keycodes_actions: Dict[str, str] = {}
        if keys_binding:
            self.update(keys_binding)

    def update(self, keys_binding: KeysBinding) -> None:
        for action, keys in keys_binding.items():
            self.set_keys_binding(keys, action)

    def set_keys_binding(self, keys: Sequence[str], action: str) -> None:
        "Add a binding for one or more keys to an action"
        for key in keys:
            self.set_key_binding(key, action)

    def set_key_binding(self, key: str, action: str) -> None:
        "Add a binding for one key to an action"
        self.keycodes_actions[KEYS[key] if len(key) > 1 else key] = action

    def get_key_event(self, ch: Optional[str] = None, timeout: Optional[int] = None) -> KeyEvent:
        if ch is None:
            ch = get_char(timeout)
        if ch is None:
            ch = "\0"
            action = None
        else:
            action = self.keycodes_actions.get(ch)
        return KeyEvent(ch=ch, action=action)

    def __str__(self) -> str:
        return "\n".join(f"<{key_to_str(ch)}, {action or '-'}>" for ch, action in self.keycodes_actions.items())  # pragma: no cover

__init__(keys_binding=None)

Keys handler is a collection of bindings of keys to actions.

Parameters:
  • keys_binding (Optional[KeysBinding], default: None ) –

    Custom key binding

Attributes:
  • keycodes_actions

    key => action mapping

Source code in pzp/keys.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def __init__(self, keys_binding: Optional[KeysBinding] = None) -> None:
    """
    Keys handler is a collection of bindings of keys to actions.

    Args:
        keys_binding: Custom key binding

    Attributes:
        keycodes_actions: key => action mapping
    """
    self.keycodes_actions: Dict[str, str] = {}
    if keys_binding:
        self.update(keys_binding)

set_key_binding(key, action)

Add a binding for one key to an action

Source code in pzp/keys.py
114
115
116
def set_key_binding(self, key: str, action: str) -> None:
    "Add a binding for one key to an action"
    self.keycodes_actions[KEYS[key] if len(key) > 1 else key] = action

set_keys_binding(keys, action)

Add a binding for one or more keys to an action

Source code in pzp/keys.py
109
110
111
112
def set_keys_binding(self, keys: Sequence[str], action: str) -> None:
    "Add a binding for one or more keys to an action"
    for key in keys:
        self.set_key_binding(key, action)

key_to_str(ch)

Return the textual representation of a char

Source code in pzp/keys.py
65
66
67
def key_to_str(ch: str) -> str:
    "Return the textual representation of a char"
    return f"0x{ord(ch):x}" if len(ch) == 1 else f"{ch}"