gtkkeys.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Keys from the desktop: the shortcut table and button grid of the simulator
  2. window, and the test mode window that feeds the PC keyboard to a real panel."""
  3. import sys
  4. import gi
  5. gi.require_version("Gtk", "3.0")
  6. gi.require_version("Gdk", "3.0")
  7. from gi.repository import Gdk, Gtk # noqa: E402
  8. # key, button label, GDK key names, grid column and row
  9. KEY_DEFS = (
  10. ("prev", "⏮ Prev (Z)", ("z", "Z", "XF86AudioPrev"), 0, 0),
  11. ("play", "▶ Play (X)", ("x", "X", "XF86AudioPlay"), 1, 0),
  12. ("pause", "⏯ Pause (C)", ("c", "C", "space", "XF86AudioPause"), 2, 0),
  13. ("stop", "⏹ Stop (V)", ("v", "V", "XF86AudioStop"), 3, 0),
  14. ("next", "⏭ Next (B)", ("b", "B", "XF86AudioNext"), 4, 0),
  15. ("esc", "Esc", ("Escape",), 0, 1),
  16. ("up", "▲ Vol+ (↑)", ("Up", "XF86AudioRaiseVolume"), 2, 1),
  17. ("menu", "Menu (M)", ("m", "M", "Menu"), 4, 1),
  18. ("left", "◀ (←)", ("Left",), 1, 2),
  19. ("ok", "OK (Enter)", ("Return", "KP_Enter"), 2, 2),
  20. ("right", "▶ (→)", ("Right",), 3, 2),
  21. ("down", "▼ Vol− (↓)", ("Down", "XF86AudioLowerVolume"), 2, 3),
  22. ("list", "List (L)", ("l", "L"), 4, 3),
  23. )
  24. # by keyval: GDK versions differ in the names they give back (AudioPlay or
  25. # XF86AudioPlay), but all of them parse the X names
  26. _BY_KEYVAL = {}
  27. for _key, _label, _names, _col, _row in KEY_DEFS:
  28. for _n in _names:
  29. _v = Gdk.keyval_from_name(_n)
  30. if _v and _v != Gdk.KEY_VoidSymbol:
  31. _BY_KEYVAL[_v] = _key
  32. def require_display():
  33. if Gdk.Display.get_default() is None:
  34. print("cannot open the X display (is DISPLAY set?)", file=sys.stderr)
  35. sys.exit(1)
  36. def key_for_event(event):
  37. return _BY_KEYVAL.get(event.keyval)
  38. class HeldKeys(object):
  39. """Tells X autorepeat from presses: GDK sends repeats as presses with no
  40. release in between"""
  41. def __init__(self, widget, on_key):
  42. self.held = set()
  43. self.on_key = on_key
  44. widget.connect("key-press-event", self._press)
  45. widget.connect("key-release-event", self._release)
  46. def _press(self, _w, event):
  47. key = key_for_event(event)
  48. if key is None:
  49. return False
  50. repeat = event.keyval in self.held
  51. self.held.add(event.keyval)
  52. self.on_key(key, repeat)
  53. return True
  54. def _release(self, _w, event):
  55. self.held.discard(event.keyval)
  56. return False
  57. def key_grid(on_key):
  58. """Grid with a button per key; returns (grid, {key: button})"""
  59. grid = Gtk.Grid()
  60. grid.set_row_spacing(4)
  61. grid.set_column_spacing(4)
  62. grid.set_column_homogeneous(True)
  63. buttons = {}
  64. for key, label, _names, col, row in KEY_DEFS:
  65. b = Gtk.Button(label=label)
  66. # a focused button would take Space and Enter from the shortcuts
  67. b.set_can_focus(False)
  68. b.connect("clicked", lambda _b, k=key: on_key(k))
  69. grid.attach(b, col, row, 1, 1)
  70. buttons[key] = b
  71. return grid, buttons
  72. class KeyWindow(object):
  73. """Test mode: a window on the desktop whose key presses go to the panel"""
  74. def __init__(self, on_key, on_close):
  75. self.on_key = on_key
  76. self.window = Gtk.Window(title="Radio panel keys")
  77. self.window.set_resizable(False)
  78. self.window.connect("destroy", lambda _w: on_close())
  79. self.keys = HeldKeys(self.window, self._pressed)
  80. box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  81. box.set_border_width(8)
  82. self.window.add(box)
  83. hint = Gtk.Label(label="Keys typed here go to the panel.")
  84. hint.set_xalign(0)
  85. box.pack_start(hint, False, False, 0)
  86. grid, _ = key_grid(self._pressed)
  87. box.pack_start(grid, False, False, 0)
  88. self.last = Gtk.Label(label="")
  89. self.last.set_xalign(0)
  90. box.pack_start(self.last, False, False, 0)
  91. self.window.show_all()
  92. def _pressed(self, key, repeat=False):
  93. self.last.set_text("last key: " + key)
  94. self.on_key(key, repeat)