Skip to content

Commit

Permalink
Merge pull request #2553 from freakboy3742/gtk-keys
Browse files Browse the repository at this point in the history
Add handling for INS, DEL, NUM LOCK, and some shifted characters.
  • Loading branch information
freakboy3742 authored May 6, 2024
2 parents 73e56ed + 5f4127e commit b546ab7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/2220.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Key handling for Insert, Delete, Num Lock, Scroll Lock, and some other esoteric keys was added for GTK and Winforms. Some uses of bare Shift on GTK were also improved.
7 changes: 7 additions & 0 deletions core/src/toga/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Key(Enum):

HOME = "<home>"
END = "<end>"
INSERT = "<insert>"
DELETE = "<delete>"
PAGE_UP = "<pg up>"
PAGE_DOWN = "<pg dn>"
Expand All @@ -128,6 +129,7 @@ class Key(Enum):
LEFT = "<left>"
RIGHT = "<right>"

NUMLOCK = "<num lock>"
NUMPAD_0 = "numpad:0"
NUMPAD_1 = "numpad:1"
NUMPAD_2 = "numpad:2"
Expand All @@ -147,6 +149,11 @@ class Key(Enum):
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(">"))
Expand Down
28 changes: 26 additions & 2 deletions gtk/src/toga_gtk/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
Gdk.KEY_X: Key.X,
Gdk.KEY_Y: Key.Y,
Gdk.KEY_Z: Key.Z,
Gdk.KEY_Caps_Lock: Key.CAPSLOCK,
Gdk.KEY_Tab: Key.TAB,
Gdk.KEY_bracketleft: Key.OPEN_BRACKET,
Gdk.KEY_bracketright: Key.CLOSE_BRACKET,
Expand All @@ -121,7 +122,10 @@
Gdk.KEY_less: Key.LESS_THAN,
Gdk.KEY_greater: Key.GREATER_THAN,
Gdk.KEY_question: Key.QUESTION,
Gdk.KEY_Insert: Key.INSERT,
Gdk.KEY_Delete: Key.DELETE,
Gdk.KEY_Begin: Key.BEGIN,
Gdk.KEY_Menu: Key.MENU,
Gdk.KEY_Home: Key.HOME,
Gdk.KEY_End: Key.END,
Gdk.KEY_Page_Up: Key.PAGE_UP,
Expand All @@ -130,6 +134,7 @@
Gdk.KEY_Right: Key.RIGHT,
Gdk.KEY_Up: Key.UP,
Gdk.KEY_Down: Key.DOWN,
Gdk.KEY_Num_Lock: Key.NUMLOCK,
Gdk.KEY_KP_Enter: Key.NUMPAD_ENTER,
Gdk.KEY_KP_0: Key.NUMPAD_0,
Gdk.KEY_KP_1: Key.NUMPAD_1,
Expand All @@ -150,11 +155,23 @@
Gdk.KEY_KP_Up: Key.UP,
Gdk.KEY_KP_Down: Key.DOWN,
Gdk.KEY_KP_Delete: Key.DELETE,
Gdk.KEY_KP_Insert: Key.INSERT,
Gdk.KEY_KP_Add: Key.NUMPAD_PLUS,
Gdk.KEY_KP_Subtract: Key.NUMPAD_MINUS,
Gdk.KEY_KP_Multiply: Key.NUMPAD_MULTIPLY,
Gdk.KEY_KP_Divide: Key.NUMPAD_DIVIDE,
Gdk.KEY_KP_Delete: Key.DELETE,
Gdk.KEY_KP_Begin: Key.BEGIN,
Gdk.KEY_Pause: Key.PAUSE,
Gdk.KEY_ISO_Left_Tab: Key.TAB,
Gdk.KEY_Scroll_Lock: Key.SCROLLLOCK,
Gdk.KEY_Shift_L: Key.SHIFT,
Gdk.KEY_Shift_R: Key.SHIFT,
Gdk.KEY_Control_L: Key.MOD_1,
Gdk.KEY_Control_R: Key.MOD_1,
Gdk.KEY_Alt_L: Key.MOD_2,
Gdk.KEY_Alt_R: Key.MOD_2,
Gdk.KEY_Hyper_L: Key.MOD_3,
Gdk.KEY_Hyper_R: Key.MOD_3,
}

GTK_KEY_NAMES = {
Expand All @@ -172,7 +189,14 @@

def toga_key(event):
"""Convert a GDK Key Event into a Toga key."""
key = GDK_KEYS[event.keyval]
try:
key = GDK_KEYS[event.keyval]
except KeyError: # pragma: no cover
# Ignore any key event code we can't map. This can happen for weird key
# combination (ctrl-alt-tux), and if the X server has weird key
# bindings. If we can't map it, we can't really type it either, so we
# need to no-cover this branch.
return None

modifiers = set()

Expand Down
6 changes: 6 additions & 0 deletions winforms/src/toga_winforms/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Key.BACK_QUOTE.value: WinForms.Keys.Oemtilde, # No idea what the code should be
Key.MINUS.value: WinForms.Keys.OemMinus,
Key.EQUAL.value: WinForms.Keys.Oemplus,
Key.CAPSLOCK.value: WinForms.Keys.CapsLock,
Key.TAB.value: WinForms.Keys.Tab,
Key.OPEN_BRACKET.value: WinForms.Keys.OemOpenBrackets,
Key.CLOSE_BRACKET.value: WinForms.Keys.OemCloseBrackets,
Expand All @@ -30,13 +31,18 @@
Key.SPACE.value: WinForms.Keys.Space,
Key.PAGE_UP.value: WinForms.Keys.PageUp,
Key.PAGE_DOWN.value: WinForms.Keys.PageDown,
Key.INSERT.value: WinForms.Keys.Insert,
Key.DELETE.value: WinForms.Keys.Delete,
Key.HOME.value: WinForms.Keys.Home,
Key.END.value: WinForms.Keys.End,
Key.UP.value: WinForms.Keys.Up,
Key.DOWN.value: WinForms.Keys.Down,
Key.LEFT.value: WinForms.Keys.Left,
Key.RIGHT.value: WinForms.Keys.Right,
Key.NUMLOCK.value: WinForms.Keys.NumLock,
Key.NUMPAD_DECIMAL_POINT.value: WinForms.Keys.Decimal,
Key.SCROLLLOCK.value: WinForms.Keys.Scroll,
Key.MENU.value: WinForms.Keys.Menu,
}
WINFORMS_KEYS.update(
{str(digit): getattr(WinForms.Keys, f"D{digit}") for digit in range(10)}
Expand Down

0 comments on commit b546ab7

Please sign in to comment.