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 == []