Skip to content

Keys

Usage

Most keys have a constant that matches the text on the key, or the name of the key if the text on the key isn't a legal Python identifier.

However, due to differences between platforms, there's no representation of "modifier" keys like Control, Command, Option, or the Windows Key. Instead, Toga provides three generic modifier constants, and maps those to the modifier keys, matching the precedence with which they are used on the underlying platforms:

Platform MOD_1 MOD_2 MOD_3
Linux Control Alt Tux/Windows/Meta
macOS Command (⌘) Option Control (^)
Windows Control Alt Not supported

Key combinations can be expressed by combining multiple Key values with the + operator.

from toga import Key

just_an_a = Key.A
shift_a = Key.SHIFT + Key.A
# Windows/Linux - Control-Shift-A:
# macOS - Command-Shift-A:
modified_shift_a = Key.MOD_1 + Key.SHIFT + Key.A

The order of addition is not significant. Key.SHIFT + Key.A and Key.A + Key.SHIFT will produce the same key representation.

Key combinations involving shifted punctuation characters, such as Key.EXCLAMATION or Key.QUESTION do not need to supply Key.SHIFT, and should be preferred over expressions like Key.SLASH + Key.SHIFT which may be difficult or impossible to type on non-QWERTY keyboards.

Reference

Bases: Enum

An enumeration providing a symbolic representation for the characters on a keyboard.

Source code in core/src/toga/keys.py
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class Key(Enum):
    """An enumeration providing a symbolic representation for the characters on
    a keyboard."""

    A = "a"
    B = "b"
    C = "c"
    D = "d"
    E = "e"
    F = "f"
    G = "g"
    H = "h"
    I = "i"  # noqa: E741
    J = "j"
    K = "k"
    L = "l"
    M = "m"
    N = "n"
    O = "o"  # noqa: E741
    P = "p"
    Q = "q"
    R = "r"
    S = "s"
    T = "t"
    U = "u"
    V = "v"
    W = "w"
    X = "x"
    Y = "y"
    Z = "z"

    _0 = "0"
    _1 = "1"
    _2 = "2"
    _3 = "3"
    _4 = "4"
    _5 = "5"
    _6 = "6"
    _7 = "7"
    _8 = "8"
    _9 = "9"

    EXCLAMATION = "!"
    AT = "@"
    HASH = "#"
    DOLLAR = "$"
    PERCENT = "%"
    CARET = "^"
    AMPERSAND = "&"
    ASTERISK = "*"
    OPEN_PARENTHESIS = "("
    CLOSE_PARENTHESIS = ")"

    MINUS = "-"
    UNDERSCORE = "_"
    EQUAL = "="
    PLUS = "+"
    OPEN_BRACKET = "["
    CLOSE_BRACKET = "]"
    OPEN_BRACE = "{"
    CLOSE_BRACE = "}"
    BACKSLASH = "\\"
    PIPE = "|"
    SEMICOLON = ";"
    COLON = ":"

    QUOTE = "'"
    DOUBLE_QUOTE = '"'
    COMMA = ","
    LESS_THAN = "<"
    FULL_STOP = "."
    GREATER_THAN = ">"
    SLASH = "/"
    QUESTION = "?"
    BACK_QUOTE = "`"
    TILDE = "~"

    ESCAPE = "<esc>"
    TAB = "<tab>"
    SPACE = " "

    BACKSPACE = "<backspace>"
    ENTER = "<enter>"

    CAPSLOCK = "<caps lock>"
    SHIFT = "<shift>"

    # Modifiers keys are handled slightly different on every platform
    # However, there is a broad platform-specific precedence for them.
    # Preserve the platform precedence behind generic naming.
    MOD_1 = "<mod 1>"  # CMD on macOS, CTRL on Linux/Windows
    MOD_2 = "<mod 2>"  # OPT on macOS, ALT on Linux/Windows
    MOD_3 = "<mod 3>"  # CTRL on macOS, Flag on Windows, Tux on Linux

    F1 = "<f1>"
    F2 = "<f2>"
    F3 = "<f3>"
    F4 = "<f4>"
    F5 = "<f5>"
    F6 = "<f6>"
    F7 = "<f7>"
    F8 = "<f8>"
    F9 = "<f9>"
    F10 = "<f10>"
    F11 = "<f11>"
    F12 = "<f12>"
    F13 = "<f13>"
    F14 = "<f14>"
    F15 = "<f15>"
    F16 = "<f16>"
    F17 = "<f17>"
    F18 = "<f18>"
    F19 = "<f19>"

    EJECT = "<eject>"

    HOME = "<home>"
    END = "<end>"
    INSERT = "<insert>"
    DELETE = "<delete>"
    PAGE_UP = "<pg up>"
    PAGE_DOWN = "<pg dn>"

    UP = "<up>"
    DOWN = "<down>"
    LEFT = "<left>"
    RIGHT = "<right>"

    NUMLOCK = "<num lock>"
    NUMPAD_0 = "numpad:0"
    NUMPAD_1 = "numpad:1"
    NUMPAD_2 = "numpad:2"
    NUMPAD_3 = "numpad:3"
    NUMPAD_4 = "numpad:4"
    NUMPAD_5 = "numpad:5"
    NUMPAD_6 = "numpad:6"
    NUMPAD_7 = "numpad:7"
    NUMPAD_8 = "numpad:8"
    NUMPAD_9 = "numpad:9"
    NUMPAD_CLEAR = "numpad:clear"
    NUMPAD_DECIMAL_POINT = "numpad:."
    NUMPAD_DIVIDE = "numpad:/"
    NUMPAD_ENTER = "numpad:enter"
    NUMPAD_EQUAL = "numpad:="
    NUMPAD_MINUS = "numpad:-"
    NUMPAD_MULTIPLY = "numpad:*"
    NUMPAD_PLUS = "numpad:+"

    SCROLLLOCK = "<scroll lock>"
    BEGIN = "<begin>"
    MENU = "<menu>"
    PAUSE = "<pause>"

    def is_printable(self) -> bool:
        """Does pressing the key result in a printable character?"""
        return not (self.value.startswith("<") and self.value.endswith(">"))

    def __add__(self, other: Key | str) -> str:
        """Allow two Keys to be concatenated, or a string to be concatenated to a Key.

        Produces a single string definition.

        e.g.,
        `Toga.Key.MOD_1 + 'a'` -> `"<mod 1>a"`
        `Toga.Key.MOD_1 + Toga.Key.SHIFT + 'a'` -> `"<mod 1><shift>a"`
        """
        try:
            # Try Key + Key
            return self.value + other.value
        except AttributeError:
            return self.value + other

    def __radd__(self, other: str) -> str:
        """Same as add."""
        return other + self.value

A = 'a'

B = 'b'

C = 'c'

D = 'd'

E = 'e'

F = 'f'

G = 'g'

H = 'h'

I = 'i'

J = 'j'

K = 'k'

L = 'l'

M = 'm'

N = 'n'

O = 'o'

P = 'p'

Q = 'q'

R = 'r'

S = 's'

T = 't'

U = 'u'

V = 'v'

W = 'w'

X = 'x'

Y = 'y'

Z = 'z'

_0 = '0'

_1 = '1'

_2 = '2'

_3 = '3'

_4 = '4'

_5 = '5'

_6 = '6'

_7 = '7'

_8 = '8'

_9 = '9'

EXCLAMATION = '!'

AT = '@'

HASH = '#'

DOLLAR = '$'

PERCENT = '%'

CARET = '^'

AMPERSAND = '&'

ASTERISK = '*'

OPEN_PARENTHESIS = '('

CLOSE_PARENTHESIS = ')'

MINUS = '-'

UNDERSCORE = '_'

EQUAL = '='

PLUS = '+'

OPEN_BRACKET = '['

CLOSE_BRACKET = ']'

OPEN_BRACE = '{'

CLOSE_BRACE = '}'

BACKSLASH = '\\'

PIPE = '|'

SEMICOLON = ';'

COLON = ':'

QUOTE = "'"

DOUBLE_QUOTE = '"'

COMMA = ','

LESS_THAN = '<'

FULL_STOP = '.'

GREATER_THAN = '>'

SLASH = '/'

QUESTION = '?'

BACK_QUOTE = '`'

TILDE = '~'

ESCAPE = '<esc>'

TAB = '<tab>'

SPACE = ' '

BACKSPACE = '<backspace>'

ENTER = '<enter>'

CAPSLOCK = '<caps lock>'

SHIFT = '<shift>'

MOD_1 = '<mod 1>'

MOD_2 = '<mod 2>'

MOD_3 = '<mod 3>'

F1 = '<f1>'

F2 = '<f2>'

F3 = '<f3>'

F4 = '<f4>'

F5 = '<f5>'

F6 = '<f6>'

F7 = '<f7>'

F8 = '<f8>'

F9 = '<f9>'

F10 = '<f10>'

F11 = '<f11>'

F12 = '<f12>'

F13 = '<f13>'

F14 = '<f14>'

F15 = '<f15>'

F16 = '<f16>'

F17 = '<f17>'

F18 = '<f18>'

F19 = '<f19>'

EJECT = '<eject>'

HOME = '<home>'

END = '<end>'

INSERT = '<insert>'

DELETE = '<delete>'

PAGE_UP = '<pg up>'

PAGE_DOWN = '<pg dn>'

UP = '<up>'

DOWN = '<down>'

LEFT = '<left>'

RIGHT = '<right>'

NUMLOCK = '<num lock>'

NUMPAD_0 = 'numpad:0'

NUMPAD_1 = 'numpad:1'

NUMPAD_2 = 'numpad:2'

NUMPAD_3 = 'numpad:3'

NUMPAD_4 = 'numpad:4'

NUMPAD_5 = 'numpad:5'

NUMPAD_6 = 'numpad:6'

NUMPAD_7 = 'numpad:7'

NUMPAD_8 = 'numpad:8'

NUMPAD_9 = 'numpad:9'

NUMPAD_CLEAR = 'numpad:clear'

NUMPAD_DECIMAL_POINT = 'numpad:.'

NUMPAD_DIVIDE = 'numpad:/'

NUMPAD_ENTER = 'numpad:enter'

NUMPAD_EQUAL = 'numpad:='

NUMPAD_MINUS = 'numpad:-'

NUMPAD_MULTIPLY = 'numpad:*'

NUMPAD_PLUS = 'numpad:+'

SCROLLLOCK = '<scroll lock>'

BEGIN = '<begin>'

MENU = '<menu>'

PAUSE = '<pause>'

is_printable()

Does pressing the key result in a printable character?

Source code in core/src/toga/keys.py
159
160
161
def is_printable(self) -> bool:
    """Does pressing the key result in a printable character?"""
    return not (self.value.startswith("<") and self.value.endswith(">"))

__add__(other)

Allow two Keys to be concatenated, or a string to be concatenated to a Key.

Produces a single string definition.

e.g., Toga.Key.MOD_1 + 'a' -> "<mod 1>a" Toga.Key.MOD_1 + Toga.Key.SHIFT + 'a' -> "<mod 1><shift>a"

Source code in core/src/toga/keys.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def __add__(self, other: Key | str) -> str:
    """Allow two Keys to be concatenated, or a string to be concatenated to a Key.

    Produces a single string definition.

    e.g.,
    `Toga.Key.MOD_1 + 'a'` -> `"<mod 1>a"`
    `Toga.Key.MOD_1 + Toga.Key.SHIFT + 'a'` -> `"<mod 1><shift>a"`
    """
    try:
        # Try Key + Key
        return self.value + other.value
    except AttributeError:
        return self.value + other

__radd__(other)

Same as add.

Source code in core/src/toga/keys.py
178
179
180
def __radd__(self, other: str) -> str:
    """Same as add."""
    return other + self.value