Debian Codegraph @

tools/key_event_test.py

raw
1 #!/usr/bin/python
2 # key_event_test.py
3 #
4 # Command-line tool to print raw key event details for testing and debugging.
5 # This tool opens a small window to receive key events and prints the raw GDK
6 # event data for each press and release. Output goes to stdout.
7 #
8 # Usage: python3 key_event_test.py
9 # If Orca is running, enter bypass mode (Alt+Backspace) to release any grabs
10 # associated with Orca commands (other than Alt+Backspace, which will remain
11 # grabbed for the purpose of exiting bypass mode).
12 # Close the window to stop.
13 #
14 # Copyright 2026 Igalia, S.L.
15 # Author: Joanmarie Diggs <[email protected]>
16 #
17 # This library is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU Lesser General Public
19 # License as published by the Free Software Foundation; either
20 # version 2.1 of the License, or (at your option) any later version.
21 #
22 # This library is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 # Lesser General Public License for more details.
26 #
27 # You should have received a copy of the GNU Lesser General Public
28 # License along with this library; if not, write to the
29 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
30 # Boston MA 02110-1301 USA.
31
32 # pylint: disable=wrong-import-position
33
34 """Command-line tool to print raw key event details for testing and debugging."""
35
36 import gi
37
38 gi.require_version("Gdk", "3.0")
39 gi.require_version("Gtk", "3.0")
40 from gi.repository import Gdk, Gtk # pylint: disable=no-name-in-module
41
42
43 def on_key_event(_widget: Gtk.Window, event: Gdk.EventKey) -> bool:
44 """Prints raw key event data."""
45
46 event_type = "press" if event.type == Gdk.EventType.KEY_PRESS else "release"
47 name = Gdk.keyval_name(event.keyval) or ""
48 print(
49 f"{event_type:8s}"
50 f" hw_code={event.hardware_keycode:<4d}"
51 f" keyval={event.keyval:<6d}"
52 f" state=0x{int(event.state):08x}"
53 f" name={name}"
54 )
55 return False
56
57
58 window = Gtk.Window(title="Key Event Test")
59 window.set_default_size(400, 200)
60 window.connect("destroy", Gtk.main_quit)
61 window.connect("key-press-event", on_key_event)
62 window.connect("key-release-event", on_key_event)
63 window.show_all()
64 print("Press keys to test. Close window to stop.\n")
65 Gtk.main()