@@ -220,6 +220,18 @@ def _get_hotkey_keys(self) -> list[str]:
220220 hotkey_keys .append (key )
221221 return hotkey_keys
222222
223+ def _get_hotkey_scan_codes (self ) -> set [int ]:
224+ """Return the scan codes for the configured hotkey (including modifier variants)."""
225+ scan_codes : set [int ] = set ()
226+ for key_name in self ._get_hotkey_keys ():
227+ try :
228+ for code in keyboard .key_to_scan_codes (key_name ):
229+ scan_codes .add (code )
230+ except Exception :
231+ # If keyboard cannot resolve a key name on this layout, skip it.
232+ continue
233+ return scan_codes
234+
223235 def lock_keyboard (self ) -> None :
224236 """
225237 Block ALL keyboard input using comprehensive scan code blocking.
@@ -258,9 +270,15 @@ def lock_keyboard(self) -> None:
258270 """
259271 self .blocked_keys .clear ()
260272
273+ # Determine scan codes used by the unlock hotkey so we do NOT block them.
274+ hotkey_scan_codes = self ._get_hotkey_scan_codes ()
275+
261276 # Block full scan code range (0-255) to cover all keyboards including
262277 # multimedia keys, F13-F24, and regional/international layouts
263278 for i in range (256 ):
279+ if i in hotkey_scan_codes :
280+ # Skip blocking the unlock hotkey scan codes so the hotkey still works.
281+ continue
264282 try :
265283 keyboard .block_key (i )
266284 self .blocked_keys .add (i )
@@ -273,6 +291,8 @@ def lock_keyboard(self) -> None:
273291 # Block extended scan codes that Windows uses for brightness/backlight controls.
274292 # These live outside the 0-255 range and need explicit handling.
275293 for extended_code in EXTENDED_SCAN_CODES :
294+ if extended_code in hotkey_scan_codes :
295+ continue
276296 try :
277297 keyboard .block_key (extended_code )
278298 self .blocked_keys .add (extended_code )
0 commit comments