gtkkeys.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def key_grid(on_key):
  39. """Grid with a button per key; returns (grid, {key: button})"""
  40. grid = Gtk.Grid()
  41. grid.set_row_spacing(4)
  42. grid.set_column_spacing(4)
  43. grid.set_column_homogeneous(True)
  44. buttons = {}
  45. for key, label, _names, col, row in KEY_DEFS:
  46. b = Gtk.Button(label=label)
  47. # a focused button would take Space and Enter from the shortcuts
  48. b.set_can_focus(False)
  49. b.connect("clicked", lambda _b, k=key: on_key(k))
  50. grid.attach(b, col, row, 1, 1)
  51. buttons[key] = b
  52. return grid, buttons
  53. class KeyWindow(object):
  54. """Test mode: a window on the desktop whose key presses go to the panel"""
  55. def __init__(self, on_key, on_close):
  56. self.on_key = on_key
  57. self.window = Gtk.Window(title="Radio panel keys")
  58. self.window.set_resizable(False)
  59. self.window.connect("destroy", lambda _w: on_close())
  60. self.window.connect("key-press-event", self._key_press)
  61. box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  62. box.set_border_width(8)
  63. self.window.add(box)
  64. hint = Gtk.Label(label="Keys typed here go to the panel.")
  65. hint.set_xalign(0)
  66. box.pack_start(hint, False, False, 0)
  67. grid, _ = key_grid(self._pressed)
  68. box.pack_start(grid, False, False, 0)
  69. self.last = Gtk.Label(label="")
  70. self.last.set_xalign(0)
  71. box.pack_start(self.last, False, False, 0)
  72. self.window.show_all()
  73. def _pressed(self, key):
  74. self.last.set_text("last key: " + key)
  75. self.on_key(key)
  76. def _key_press(self, _w, event):
  77. key = key_for_event(event)
  78. if key is None:
  79. return False
  80. self._pressed(key)
  81. return True