From 7384200fe4fbb9f18e213faeda386dd59c25e462 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Sun, 22 Nov 2026 04:08:53 -0500 Subject: [PATCH] The application shortcut filter asks whether an event is a key before reading it `ApplicationShortcutFilter` is installed on the application, so every event of every object passes through it, and nearly none is a key. It decoded a sequence from each: `KeyEventSequence.from_event` reads the event's type, which in PySide builds a Python enum member, four Python calls an event in all. In a Toad window with nothing pressed that was 64 Python calls a frame (its frame-work count), a quarter of everything Python did in a frame. The event's class is asked first, which is answered without leaving C. Tests: tests/test_application_shortcuts.py, 5 passed (one new: an event that is no key is let through with nothing of it read). Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016gZLdx92gKjvnNKianjAXG --- .../services/application_shortcuts.py | 8 +++++++- tests/test_application_shortcuts.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pyqt_reactive/services/application_shortcuts.py b/src/pyqt_reactive/services/application_shortcuts.py index f82014d4..f61f3a13 100644 --- a/src/pyqt_reactive/services/application_shortcuts.py +++ b/src/pyqt_reactive/services/application_shortcuts.py @@ -6,7 +6,7 @@ from typing import Generic, TypeVar from PySide6.QtCore import QEvent, QObject -from PySide6.QtGui import QAction, QKeySequence +from PySide6.QtGui import QAction, QKeyEvent, QKeySequence from PySide6.QtWidgets import QApplication from pyqt_reactive.widgets.editors.simple_code_editor import CodeEditorDialog @@ -54,6 +54,12 @@ def replace_actions(self, actions: Mapping[str, Callable[[], None]]) -> None: def eventFilter(self, obj, event): # noqa: N802 - Qt virtual method name del obj + # Every event the application delivers passes here, and nearly none is a key. Asking + # the event's class first answers that without leaving C: reading its type builds a + # Python enum member (two Python calls), and decoding a sequence from it took four + # Python calls an event, 64 a frame in a Toad window with nothing pressed. + if not isinstance(event, QKeyEvent): + return False sequence = KeyEventSequence.from_event(event) if sequence is None: return False diff --git a/tests/test_application_shortcuts.py b/tests/test_application_shortcuts.py index 66ecb23d..77a1ce0d 100644 --- a/tests/test_application_shortcuts.py +++ b/tests/test_application_shortcuts.py @@ -87,3 +87,19 @@ def test_an_open_popup_keeps_its_keys_from_application_commands(qapp) -> None: assert calls == ["back"] finally: lifecycle.close() + + +def test_an_event_that_is_no_key_is_let_through_without_reading_its_type(): + """The filter sees every event of the application: one that is not a key event is + answered by its class, with nothing of it read.""" + from pyqt_reactive.services.application_shortcuts import ApplicationShortcutFilter + + class Untouchable: + def __getattr__(self, name): + raise AssertionError(f"read {name} of an event that is no key") + + ran = [] + held = ApplicationShortcutFilter({"Ctrl+B": lambda: ran.append("b")}) + assert held.eventFilter(None, Untouchable()) is False + assert held.eventFilter(None, QEvent(QEvent.Type.Paint)) is False + assert ran == []