"""Keys from the desktop: the shortcut table and button grid of the simulator window, and the test mode window that feeds the PC keyboard to a real panel.""" import sys import gi gi.require_version("Gtk", "3.0") gi.require_version("Gdk", "3.0") from gi.repository import Gdk, Gtk # noqa: E402 # key, button label, GDK key names, grid column and row KEY_DEFS = ( ("prev", "⏮ Prev (Z)", ("z", "Z", "XF86AudioPrev"), 0, 0), ("play", "▶ Play (X)", ("x", "X", "XF86AudioPlay"), 1, 0), ("pause", "⏯ Pause (C)", ("c", "C", "space", "XF86AudioPause"), 2, 0), ("stop", "⏹ Stop (V)", ("v", "V", "XF86AudioStop"), 3, 0), ("next", "⏭ Next (B)", ("b", "B", "XF86AudioNext"), 4, 0), ("esc", "Esc", ("Escape",), 0, 1), ("up", "▲ Vol+ (↑)", ("Up", "XF86AudioRaiseVolume"), 2, 1), ("menu", "Menu (M)", ("m", "M", "Menu"), 4, 1), ("left", "◀ (←)", ("Left",), 1, 2), ("ok", "OK (Enter)", ("Return", "KP_Enter"), 2, 2), ("right", "▶ (→)", ("Right",), 3, 2), ("down", "▼ Vol− (↓)", ("Down", "XF86AudioLowerVolume"), 2, 3), ("list", "List (L)", ("l", "L"), 4, 3), ) # by keyval: GDK versions differ in the names they give back (AudioPlay or # XF86AudioPlay), but all of them parse the X names _BY_KEYVAL = {} for _key, _label, _names, _col, _row in KEY_DEFS: for _n in _names: _v = Gdk.keyval_from_name(_n) if _v and _v != Gdk.KEY_VoidSymbol: _BY_KEYVAL[_v] = _key def require_display(): if Gdk.Display.get_default() is None: print("cannot open the X display (is DISPLAY set?)", file=sys.stderr) sys.exit(1) def key_for_event(event): return _BY_KEYVAL.get(event.keyval) def key_grid(on_key): """Grid with a button per key; returns (grid, {key: button})""" grid = Gtk.Grid() grid.set_row_spacing(4) grid.set_column_spacing(4) grid.set_column_homogeneous(True) buttons = {} for key, label, _names, col, row in KEY_DEFS: b = Gtk.Button(label=label) # a focused button would take Space and Enter from the shortcuts b.set_can_focus(False) b.connect("clicked", lambda _b, k=key: on_key(k)) grid.attach(b, col, row, 1, 1) buttons[key] = b return grid, buttons class KeyWindow(object): """Test mode: a window on the desktop whose key presses go to the panel""" def __init__(self, on_key, on_close): self.on_key = on_key self.window = Gtk.Window(title="Radio panel keys") self.window.set_resizable(False) self.window.connect("destroy", lambda _w: on_close()) self.window.connect("key-press-event", self._key_press) box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) box.set_border_width(8) self.window.add(box) hint = Gtk.Label(label="Keys typed here go to the panel.") hint.set_xalign(0) box.pack_start(hint, False, False, 0) grid, _ = key_grid(self._pressed) box.pack_start(grid, False, False, 0) self.last = Gtk.Label(label="") self.last.set_xalign(0) box.pack_start(self.last, False, False, 0) self.window.show_all() def _pressed(self, key): self.last.set_text("last key: " + key) self.on_key(key) def _key_press(self, _w, event): key = key_for_event(event) if key is None: return False self._pressed(key) return True