-
-
Notifications
You must be signed in to change notification settings - Fork 817
Dedicated mouse hook #20436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dedicated mouse hook #20436
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c37f68b
first draft
Boumtchack e01319f
tests
Boumtchack 8130cf8
revert changes
Boumtchack 9d6f4f6
refactoring
Boumtchack f40ae23
minor changes
Boumtchack a561914
copilot review
Boumtchack 346a962
logic changes
Boumtchack adb78b5
review modification
Boumtchack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # Copyright (C) 2026 NV Access Limited, Antoine Haffreingue | ||
| # This file may be used under the terms of the GNU General Public License, version 2 or later, as modified by the NVDA license. | ||
| # For full terms and any additional permissions, see the NVDA license file: https://github.com/nvaccess/nvda/blob/master/copying.txt | ||
|
|
||
| """Dedicated low-level mouse hook for the magnifier. | ||
|
|
||
| Runs in its own thread so it stays responsive even when the wx main thread is | ||
| busy (e.g. at high Windows display scale factors), which is the root cause of | ||
| the DPI-related lag compared to the built-in Windows Magnifier. | ||
| """ | ||
|
|
||
| import ctypes | ||
| import threading | ||
| from ctypes import byref | ||
| from ctypes.wintypes import MSG | ||
| from logHandler import log | ||
| from winBindings import user32 | ||
| from winInputHook import MSLLHOOKSTRUCT, HC_ACTION, WH_MOUSE_LL | ||
|
|
||
| WM_MOUSEMOVE: int = 0x0200 | ||
| WM_QUIT: int = 0x0012 | ||
|
|
||
|
|
||
| class MagnifierMouseHook: | ||
| """Installs a WH_MOUSE_LL hook in a dedicated thread. Calls onMouseMove(x, y) on every mouse move.""" | ||
|
|
||
| def __init__(self, onMouseMove): | ||
|
Boumtchack marked this conversation as resolved.
Outdated
|
||
| self._onMouseMove = onMouseMove | ||
| self._thread: threading.Thread | None = None | ||
| self._cCallback = None # kept alive to prevent GC of the ctypes callback | ||
| self._hookReady = threading.Event() | ||
| self._hookInstalled: bool = False | ||
|
|
||
| def start(self) -> None: | ||
| self._thread = threading.Thread(target=self._run, name="magnifierMouseHook", daemon=True) | ||
| self._thread.start() | ||
| self._hookReady.wait(timeout=1.0) | ||
|
|
||
| def stop(self) -> None: | ||
| if self._thread: | ||
| user32.PostThreadMessage(self._thread.ident, WM_QUIT, 0, 0) | ||
| self._thread.join(timeout=1.0) | ||
| self._thread = None | ||
| self._cCallback = None | ||
|
Boumtchack marked this conversation as resolved.
|
||
|
|
||
| def _run(self) -> None: | ||
| def _onRawMouseEvent(code, eventType, mouseDataPointer): | ||
|
Boumtchack marked this conversation as resolved.
Outdated
|
||
| if code == HC_ACTION and eventType == WM_MOUSEMOVE: | ||
| mouseData = MSLLHOOKSTRUCT.from_address(mouseDataPointer) | ||
| try: | ||
| self._onMouseMove(mouseData.pt.x, mouseData.pt.y) | ||
| except Exception: | ||
| log.exception("Error in magnifier mouse hook callback") | ||
| return user32.CallNextHookEx(0, code, eventType, mouseDataPointer) | ||
|
|
||
| self._cCallback = user32.HOOKPROC(_onRawMouseEvent) | ||
| hookHandle = user32.SetWindowsHookEx(WH_MOUSE_LL, self._cCallback, None, 0) | ||
| self._hookInstalled = bool(hookHandle) | ||
| self._hookReady.set() | ||
| if not hookHandle: | ||
| log.error(f"Failed to install magnifier mouse hook (error {ctypes.GetLastError()})") | ||
| return | ||
|
|
||
| windowsMessage = MSG() | ||
| while user32.GetMessage(byref(windowsMessage), None, 0, 0): | ||
| pass | ||
|
|
||
| user32.UnhookWindowsHookEx(hookHandle) | ||
|
Boumtchack marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # Copyright (C) 2025-2026 NV Access Limited, Antoine Haffreingue | ||
| # This file may be used under the terms of the GNU General Public License, version 2 or later, as modified by the NVDA license. | ||
| # For full terms and any additional permissions, see the NVDA license file: https://github.com/nvaccess/nvda/blob/master/copying.txt | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
| from _magnifier.fullscreenMagnifier import FullScreenMagnifier | ||
| from _magnifier.utils.types import Coordinates | ||
| from tests.unit.test_magnifier.test_magnifier import _TestMagnifier | ||
|
|
||
|
|
||
| class _TestMagnifierWithHook(_TestMagnifier): | ||
| """Extends _TestMagnifier with a mock for MagnifierMouseHook.""" | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.mouseHook_patcher = patch("_magnifier.magnifier.MagnifierMouseHook") | ||
| self.MockMouseHook = self.mouseHook_patcher.start() | ||
| self.mock_hook_instance = MagicMock() | ||
| self.MockMouseHook.return_value = self.mock_hook_instance | ||
|
|
||
| def tearDown(self): | ||
| self.mouseHook_patcher.stop() | ||
| super().tearDown() | ||
|
|
||
|
|
||
| class TestMouseHookLifecycle(_TestMagnifierWithHook): | ||
| """Tests for the WH_MOUSE_LL hook lifecycle managed by the base Magnifier class.""" | ||
|
|
||
| def testHookStartedWithMagnifier(self): | ||
| """Mouse hook is started when the magnifier starts.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
|
|
||
| self.MockMouseHook.assert_called_once_with(magnifier._onMouseMove) | ||
| self.mock_hook_instance.start.assert_called_once() | ||
|
|
||
| magnifier._stopMagnifier() | ||
|
|
||
| def testHookStoppedWithMagnifier(self): | ||
| """Mouse hook is stopped and cleared when the magnifier stops.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
| magnifier._stopMagnifier() | ||
|
|
||
| self.mock_hook_instance.stop.assert_called_once() | ||
| self.assertIsNone(magnifier._mouseHook) | ||
|
|
||
|
|
||
| class TestOnMouseMove(_TestMagnifierWithHook): | ||
| """Tests for FullScreenMagnifier._onMouseMove — the hook callback.""" | ||
|
|
||
| def testUpdatesMagnifier(self): | ||
| """_onMouseMove updates currentCoordinates and calls _fullscreenMagnifier.""" | ||
| magnifier = FullScreenMagnifier() | ||
| magnifier._startMagnifier() | ||
| magnifier._fullscreenMagnifier = MagicMock() | ||
|
|
||
| with patch("_magnifier.magnifier.getFollowState", return_value=True): | ||
| magnifier._onMouseMove(500, 400) | ||
|
|
||
| magnifier._fullscreenMagnifier.assert_called_once() | ||
| self.assertEqual(magnifier.currentCoordinates, Coordinates(500, 400)) | ||
| magnifier._stopMagnifier() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.