Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/pyqt_reactive/services/application_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_application_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
Loading