Skip to content

One keyboard and mouse for a Mac and a Windows PC over the local network, in either direction.

Beamer 1.4.2

Toby Kalkmane5339fa82d4b
diff --git a/README.md b/README.md
index db533db..48350e2 100644
--- a/README.md
+++ b/README.md
@@ -229,7 +229,10 @@ on its own opens Start. The PC's keys arrive on the Mac the reverse way. Chords
Windows held are sent as real virtual-key presses, so they land on the right key for the active
Windows keyboard layout. The Mac reads its own layout too, so on a German or French Mac Cmd+Z is
still undo and Cmd+A still selects all, and a letter typed from the PC lands on the key that types
-it. The `key_map` object in each app's config JSON can still override single keys.
+it. When the two machines are on layouts of different alphabets, say the PC on Russian and the Mac
+on English, a letter types what the receiving machine's own layout has on that key, as its own
+keyboard would, and shortcuts keep working. The `key_map` object in each app's config JSON can
+still override single keys.
The trigger key is never forwarded. On both machines it is recorded by pressing it: Right Option on
the Mac and Right Ctrl on the PC to start with. Either can double-tap or hold.
diff --git a/VERSION b/VERSION
index 347f583..9df886c 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.4.1
+1.4.2
diff --git a/mac_app/bridge.py b/mac_app/bridge.py
index 4c2f29a..307b03c 100644
--- a/mac_app/bridge.py
+++ b/mac_app/bridge.py
@@ -26,6 +26,7 @@ import protocol
from key_codes import (
KEY_NAME_TO_CODE,
MODIFIER_KEY_CODES,
+ PRINTABLE_KEY_FALLBACKS,
SPECIAL_KEY_NAMES,
)
@@ -226,9 +227,11 @@ class QuartzEventTranslator:
if character is None:
return KeyResult(False, is_down, is_repeat, [])
message_type = protocol.MSG_KEYDOWN if is_down else protocol.MSG_KEYUP
- return KeyResult(False, is_down, is_repeat, [
- {"type": message_type, "data": {"key": character}}
- ])
+ data = {"key": character}
+ # Where the key sits on a US keyboard, for a PC whose layout cannot type the character.
+ if keycode in PRINTABLE_KEY_FALLBACKS:
+ data["us"] = PRINTABLE_KEY_FALLBACKS[keycode]
+ return KeyResult(False, is_down, is_repeat, [{"type": message_type, "data": data}])
def mouse_messages(self, event_type, event):
quartz = self.quartz
diff --git a/mac_app/input_injector_mac.py b/mac_app/input_injector_mac.py
index f4d1057..ba96de3 100644
--- a/mac_app/input_injector_mac.py
+++ b/mac_app/input_injector_mac.py
@@ -21,10 +21,11 @@ import functools
import logging
import threading
import time
+import unicodedata
from typing import Dict, List, Optional, Set, Tuple
import keyboard_layout
-from key_codes import KEY_NAME_TO_CODE
+from key_codes import KEY_NAME_TO_CODE, US_KEY_CODES
try:
import Quartz
@@ -100,12 +101,34 @@ def current_flags(mods_down: Set[str]) -> int:
return flags
-def plan_key_event(name: str, down: bool, mods_down: Set[str], held: Optional[Dict[str, int]] = None) -> Optional[Tuple[int, Optional[str]]]:
+def _script(character: str) -> str:
+ return unicodedata.name(character, "").split(" ")[0]
+
+
+def _by_place(name: str, us: Optional[str], mods_down: Set[str]) -> Optional[int]:
+ """The key at `us`, the place the sender's key sits on a US keyboard, for a character this
+ layout cannot place: under a chord, which has to reach the app as a shortcut, and for a letter
+ of another script than the one this layout types there, so a PC on Russian types this Mac's
+ English and one on English types its Russian, as the Mac's own keyboard would. Anything else,
+ an é from a French PC say, stays text: the key it sits on types something unrelated here."""
+ code = US_KEY_CODES.get(us)
+ if code is None:
+ return None
+ if mods_down & CHORD_MODIFIERS:
+ return code
+ here = keyboard_layout.char_for(code)
+ if name.isalpha() and here and here.isalpha() and _script(name) != _script(here):
+ return code
+ return None
+
+
+def plan_key_event(name: str, down: bool, mods_down: Set[str], held: Optional[Dict[str, int]] = None, us: Optional[str] = None) -> Optional[Tuple[int, Optional[str]]]:
"""Pure planning: (key code, unicode string or None) for one key, or None
when there is nothing to send. Mutates `mods_down` exactly as inject_key
does, so a test can inspect the bookkeeping. `held` keeps the key each
character went down on, so its release lets go of that key even if the
- layout changed in between."""
+ layout changed in between. `us` is where the key sits on a US keyboard,
+ sent by a physical keyboard and absent from a phone's."""
lowered = name.lower()
if lowered in MODIFIER_FLAG_NAMES:
# Recorded before the event is built, so the modifier's own event
@@ -127,9 +150,16 @@ def plan_key_event(name: str, down: bool, mods_down: Set[str], held: Optional[Di
# key, and a plain "z" from the PC is not posted as the US Z key, which types "y" on a
# German Mac. keyboard_layout is also what bridge.py reads keys with, so the two directions
# agree about which key is which.
- if held is not None and not down and name in held:
- return held.pop(name), None
+ if held is not None and name in held:
+ # A repeat stays on the key the press went down on, even if the layout changed since, so
+ # its release lets go of the key that is actually down.
+ return (held[name] if down else held.pop(name)), None
key_code = keyboard_layout.code_for(name.lower())
+ if key_code is None:
+ key_code = _by_place(name, us, mods_down)
+ if key_code is not None and name.isupper() and not mods_down:
+ # A capital from caps lock: posting the key would type this layout's small letter.
+ return 0, keyboard_layout.char_for(key_code).upper()
if key_code is not None and (name.islower() or not name.isalpha() or mods_down):
if held is not None and down:
held[name] = key_code
@@ -141,9 +171,9 @@ def plan_key_event(name: str, down: bool, mods_down: Set[str], held: Optional[Di
@_locked
-def inject_key(name: str, down: bool) -> None:
+def inject_key(name: str, down: bool, us: Optional[str] = None) -> None:
quartz = _require()
- plan = plan_key_event(name, down, _mods_down, _held_codes)
+ plan = plan_key_event(name, down, _mods_down, _held_codes, us)
if plan is None:
return
key_code, text = plan
diff --git a/mac_app/key_codes.py b/mac_app/key_codes.py
index 51e0c76..0351218 100644
--- a/mac_app/key_codes.py
+++ b/mac_app/key_codes.py
@@ -111,6 +111,7 @@ PRINTABLE_KEY_FALLBACKS = {
0x2F: ".",
0x32: "`",
}
+US_KEY_CODES = {char: code for code, char in PRINTABLE_KEY_FALLBACKS.items()}
# What the settings window calls a key. Names the wire and config keep as they are. AppKit-free,
# so widgets.py re-exports key_title rather than defining it, and anything that only needs a
diff --git a/mac_app/receiver.py b/mac_app/receiver.py
index cc7b14a..4b063e8 100644
--- a/mac_app/receiver.py
+++ b/mac_app/receiver.py
@@ -118,9 +118,9 @@ def handle_message(message: dict, injector=None, scale: Optional[InputScale] = N
if not isinstance(data, dict):
raise protocol.ProtocolError("message data must be a JSON object")
if message_type == protocol.MSG_KEYDOWN:
- injector.inject_key(data["key"], down=True)
+ injector.inject_key(data["key"], down=True, us=data.get("us"))
elif message_type == protocol.MSG_KEYUP:
- injector.inject_key(data["key"], down=False)
+ injector.inject_key(data["key"], down=False, us=data.get("us"))
elif message_type == protocol.MSG_MOUSEMOVE:
dx, dy = int(data["dx"]), int(data["dy"])
if scale is not None and (dx or dy):
diff --git a/mac_app/tests/test_bridge.py b/mac_app/tests/test_bridge.py
index 3fa2084..d77cfad 100644
--- a/mac_app/tests/test_bridge.py
+++ b/mac_app/tests/test_bridge.py
@@ -339,7 +339,7 @@ class TranslatorTests(unittest.TestCase):
FakeQuartz.kCGEventKeyDown, event, 0x3D, self.key_map
)
self.assertEqual(result.messages, [
- {"type": protocol.MSG_KEYDOWN, "data": {"key": "v"}}
+ {"type": protocol.MSG_KEYDOWN, "data": {"key": "v", "us": "v"}}
])
def test_shift_still_composes_its_character(self):
@@ -353,7 +353,7 @@ class TranslatorTests(unittest.TestCase):
FakeQuartz.kCGEventKeyDown, event, 0x3D, self.key_map
)
self.assertEqual(result.messages, [
- {"type": protocol.MSG_KEYDOWN, "data": {"key": "V"}}
+ {"type": protocol.MSG_KEYDOWN, "data": {"key": "V", "us": "v"}}
])
def test_printable_key_uses_unicode(self):
@@ -369,7 +369,7 @@ class TranslatorTests(unittest.TestCase):
self.key_map,
)
self.assertEqual(result.messages, [
- {"type": protocol.MSG_KEYDOWN, "data": {"key": "å"}}
+ {"type": protocol.MSG_KEYDOWN, "data": {"key": "å", "us": "a"}}
])
def test_printable_keyup_reuses_keydown_character(self):
@@ -386,7 +386,7 @@ class TranslatorTests(unittest.TestCase):
self.translator.key_result(FakeQuartz.kCGEventKeyDown, down, 0x3D, self.key_map)
result = self.translator.key_result(FakeQuartz.kCGEventKeyUp, up, 0x3D, self.key_map)
self.assertEqual(result.messages, [
- {"type": protocol.MSG_KEYUP, "data": {"key": "A"}}
+ {"type": protocol.MSG_KEYUP, "data": {"key": "A", "us": "a"}}
])
def test_command_modifier_uses_configured_mapping(self):
@@ -586,7 +586,7 @@ class ControllerTests(unittest.TestCase):
self.assertIsNone(returned)
self.assertEqual(
self.controller.outbound.get_nowait(),
- {"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}},
+ {"type": protocol.MSG_KEYDOWN, "data": {"key": "a", "us": "a"}},
)
def test_queue_failure_restores_local_before_returning_event(self):
@@ -1617,7 +1617,7 @@ class GestureWiringTests(unittest.TestCase):
self.assertIsNone(keyboard_returned)
self.assertEqual(
controller.outbound.get_nowait(),
- {"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}},
+ {"type": protocol.MSG_KEYDOWN, "data": {"key": "a", "us": "a"}},
)
def test_swipe_while_redirecting_is_translated_and_swallowed(self):
diff --git a/mac_app/tests/test_ignored_inputs.py b/mac_app/tests/test_ignored_inputs.py
index a2b90f9..673301a 100644
--- a/mac_app/tests/test_ignored_inputs.py
+++ b/mac_app/tests/test_ignored_inputs.py
@@ -56,7 +56,7 @@ class TapTests(unittest.TestCase):
def test_any_other_key_still_goes_to_windows(self):
self.assertIsNone(self.tap(FakeQuartz.kCGEventKeyDown, key_event(KEY_A, "a")))
- self.assertEqual(self.sent(), [(protocol.MSG_KEYDOWN, {"key": "a"})])
+ self.assertEqual(self.sent(), [(protocol.MSG_KEYDOWN, {"key": "a", "us": "a"})])
def test_a_key_ignored_while_held_on_windows_is_still_released_there(self):
self.configure([])
diff --git a/mac_app/tests/test_keyboard_layout.py b/mac_app/tests/test_keyboard_layout.py
index 21fdce7..efd0b08 100644
--- a/mac_app/tests/test_keyboard_layout.py
+++ b/mac_app/tests/test_keyboard_layout.py
@@ -16,6 +16,7 @@ from tests.test_bridge import FakeQuartz
GERMAN = {0x00: "a", 0x06: "y", 0x07: "x", 0x08: "c", 0x09: "v", 0x0C: "q", 0x10: "z", 0x18: None, 0x21: "ü", 0x29: "ö", 0x32: "<"}
FRENCH = {0x00: "q", 0x06: "w", 0x0C: "a", 0x0D: "z", 0x10: "y", 0x12: "&", 0x29: "m"}
+RUSSIAN = {0x00: "ф", 0x06: "я", 0x08: "с", 0x09: "м", 0x0C: "й", 0x10: "н", 0x12: "1", 0x13: "2", 0x21: "х", 0x2F: "ю"}
class LayoutTest(unittest.TestCase):
@@ -61,11 +62,11 @@ class ShortcutToWindowsTests(LayoutTest):
def test_german_cmd_z_is_undo_not_redo(self):
keyboard_layout.install(GERMAN)
- self.assertEqual(self.press(0x10, "z"), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z"}}])
+ self.assertEqual(self.press(0x10, "z"), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z", "us": "y"}}])
def test_french_cmd_a_selects_all(self):
keyboard_layout.install(FRENCH)
- self.assertEqual(self.press(0x0C, "a"), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}}])
+ self.assertEqual(self.press(0x0C, "a"), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "a", "us": "q"}}])
def test_a_repeat_after_a_layout_switch_sends_the_first_press_character(self):
translator = QuartzEventTranslator(FakeQuartz)
@@ -75,14 +76,14 @@ class ShortcutToWindowsTests(LayoutTest):
event = {FakeQuartz.kCGKeyboardEventKeycode: 0x06, FakeQuartz.kCGKeyboardEventAutorepeat: repeat, "unicode": keyboard_layout.char_for(0x06)}
return translator.key_result(event_type, event, 0x3D, key_map).messages
- self.assertEqual(key(FakeQuartz.kCGEventKeyDown, 0), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z"}}])
+ self.assertEqual(key(FakeQuartz.kCGEventKeyDown, 0), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z", "us": "z"}}])
keyboard_layout.install(GERMAN)
- self.assertEqual(key(FakeQuartz.kCGEventKeyDown, 1), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z"}}])
- self.assertEqual(key(FakeQuartz.kCGEventKeyUp, 0), [{"type": protocol.MSG_KEYUP, "data": {"key": "z"}}])
+ self.assertEqual(key(FakeQuartz.kCGEventKeyDown, 1), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "z", "us": "z"}}])
+ self.assertEqual(key(FakeQuartz.kCGEventKeyUp, 0), [{"type": protocol.MSG_KEYUP, "data": {"key": "z", "us": "z"}}])
def test_option_still_sends_the_key_not_the_composed_character(self):
keyboard_layout.install(GERMAN)
- self.assertEqual(self.press(0x06, "¥", modifier=0x3A), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "y"}}])
+ self.assertEqual(self.press(0x06, "¥", modifier=0x3A), [{"type": protocol.MSG_KEYDOWN, "data": {"key": "y", "us": "z"}}])
class TypingOntoThisMacTests(LayoutTest):
@@ -117,6 +118,71 @@ class TypingOntoThisMacTests(LayoutTest):
self.assertEqual(self.plan("["), (0, "["))
+class TwoScriptTests(LayoutTest):
+ """GitHub issue 3: a PC switched to Russian sends the C key as "с", which this Mac on English
+ cannot place, so it was typed as text with Ctrl cleared, and Ctrl+C typed "с". Keys now carry
+ where they sit on a US keyboard, and a key lands there when a shortcut needs it or its letter
+ is from a script this layout does not type there."""
+
+ def plan(self, name, mods=None, us=None, down=True, held=None):
+ return injector.plan_key_event(name, down, set() if mods is None else mods, held, us=us)
+
+ def test_ctrl_c_from_a_pc_on_russian_is_cmd_c_on_an_english_mac(self):
+ self.assertEqual(self.plan("с", {"cmd"}, us="c"), (0x08, None))
+
+ def test_a_russian_letter_from_the_pc_types_this_macs_letter(self):
+ self.assertEqual(self.plan("с", us="c"), (0x08, None))
+ self.assertEqual(self.plan("С", {"shift"}, us="c"), (0x08, None))
+
+ def test_a_capital_without_shift_is_this_macs_capital(self):
+ self.assertEqual(self.plan("С", us="c"), (0, "C"))
+
+ def test_a_capital_without_shift_lets_go_of_the_character_it_typed(self):
+ self.assertEqual(self.plan("С", us="c", down=False, held={}), (0, "C"))
+
+ def test_a_repeat_after_a_layout_switch_stays_on_the_key_that_is_down(self):
+ held = {}
+ self.assertEqual(self.plan("z", us="z", held=held), (0x06, None))
+ keyboard_layout.install(GERMAN)
+ self.assertEqual(self.plan("z", us="z", held=held), (0x06, None))
+ self.assertEqual(self.plan("z", us="z", down=False, held=held), (0x06, None))
+ self.assertEqual(held, {})
+
+ def test_a_pc_on_english_types_russian_on_a_mac_on_russian(self):
+ keyboard_layout.install(RUSSIAN)
+ self.assertEqual(self.plan("c", us="c"), (0x08, None))
+ self.assertEqual(self.plan("c", {"cmd"}, us="c"), (0x08, None))
+
+ def test_matching_layouts_still_place_by_character(self):
+ keyboard_layout.install(RUSSIAN)
+ self.assertEqual(self.plan("с", us="c"), (0x08, None))
+
+ def test_an_accent_from_the_same_script_is_still_typed_as_text(self):
+ # French é sits on the US 2 key; a US Mac has no é, and 2 is not what was meant.
+ self.assertEqual(self.plan("é", us="2"), (0, "é"))
+
+ def test_a_shortcut_on_an_accented_key_lands_on_its_place(self):
+ self.assertEqual(self.plan("é", {"cmd"}, us="2"), (0x13, None))
+
+ def test_a_key_with_no_place_keeps_the_old_answer(self):
+ # A phone's keyboard, or a PC on an older Beamer, sends no place.
+ self.assertEqual(self.plan("с", {"cmd"}), (0, "с"))
+
+ def test_the_release_lets_go_of_the_key_the_press_went_down_on(self):
+ held = {}
+ self.assertEqual(self.plan("с", us="c", held=held), (0x08, None))
+ keyboard_layout.install(RUSSIAN)
+ self.assertEqual(self.plan("с", us="c", down=False, held=held), (0x08, None))
+ self.assertEqual(held, {})
+
+ def test_the_mac_sends_where_its_key_sits(self):
+ keyboard_layout.install(RUSSIAN)
+ translator = QuartzEventTranslator(FakeQuartz)
+ event = {FakeQuartz.kCGKeyboardEventKeycode: 0x08, FakeQuartz.kCGKeyboardEventAutorepeat: 0, "unicode": "с"}
+ messages = translator.key_result(FakeQuartz.kCGEventKeyDown, event, 0x3D, dict(config.DEFAULT_KEY_MAP)).messages
+ self.assertEqual(messages, [{"type": protocol.MSG_KEYDOWN, "data": {"key": "с", "us": "c"}}])
+
+
@unittest.skipUnless(sys.platform == "darwin", "reads macOS's own layouts")
class SystemLayoutTests(unittest.TestCase):
"""The same answers read from the system's German and French layouts through UCKeyTranslate,
@@ -155,6 +221,12 @@ class SystemLayoutTests(unittest.TestCase):
for code, char in FRENCH.items():
self.assertEqual(chars.get(code), char, hex(code))
+ def test_russian(self):
+ for source_id in ("com.apple.keylayout.Russian", "com.apple.keylayout.RussianWin"):
+ chars = self.chars(source_id)
+ for code, char in RUSSIAN.items():
+ self.assertEqual(chars.get(code), char, f"{source_id} {code:#x}")
+
def test_dead_keys_are_marked(self):
# German's 0x18 is ´, which starts an accent rather than typing.
chars = self.chars("com.apple.keylayout.German")
diff --git a/win_app/capture_win.py b/win_app/capture_win.py
index df8151f..04d89c3 100644
--- a/win_app/capture_win.py
+++ b/win_app/capture_win.py
@@ -46,7 +46,7 @@ import threading
import time
from typing import Callable, Dict, List, Optional, Set, Tuple
-from input_injector import INJECTED_MARK
+from input_injector import INJECTED_MARK, SCAN_TO_US
LOGGER = logging.getLogger(__name__)
@@ -83,6 +83,7 @@ WM_XBUTTONDOWN = 0x020B
WM_XBUTTONUP = 0x020C
WM_MOUSEHWHEEL = 0x020E
+LLKHF_EXTENDED = 0x01
LLKHF_INJECTED = 0x10
LLMHF_INJECTED = 0x01
@@ -368,7 +369,7 @@ class Hooks:
"""The two low-level hooks, the raw-input sink, and the thread that owns
all three.
- `on_key(name, down, vk)` and `on_mouse(message, x, y, mouse_data)` are called
+ `on_key(name, down, vk, us)` and `on_mouse(message, x, y, mouse_data)` are called
on the hook thread and return True when the event has been consumed and
must not reach the rest of Windows. `on_motion(dx, dy)` is called with
the mouse's own relative counts and consumes nothing -- raw input is a
@@ -500,7 +501,8 @@ class Hooks:
name = key_name(data.vkCode, data.scanCode, self._state, _to_unicode)
if name is None:
return user32.CallNextHookEx(None, code, wparam, lparam)
- if self._on_key(name, down, data.vkCode):
+ us = None if data.flags & LLKHF_EXTENDED else SCAN_TO_US.get(data.scanCode)
+ if self._on_key(name, down, data.vkCode, us):
return 1
except Exception:
# Never let an exception here swallow a key: fail open, log once
diff --git a/win_app/input_injector.py b/win_app/input_injector.py
index 655f0f3..22c124d 100644
--- a/win_app/input_injector.py
+++ b/win_app/input_injector.py
@@ -7,6 +7,7 @@ import subprocess
import sys
import threading
import time
+import unicodedata
from typing import Callable, Dict, List, Optional, Set, Tuple
@@ -21,6 +22,12 @@ if _IS_WINDOWS:
user32.VkKeyScanW.argtypes = [ctypes.c_wchar]
user32.MapVirtualKeyW.restype = ctypes.c_uint
user32.MapVirtualKeyW.argtypes = [ctypes.c_uint, ctypes.c_uint]
+ user32.GetKeyboardLayout.restype = ctypes.c_void_p
+ user32.GetKeyboardLayout.argtypes = [ctypes.c_ulong]
+ user32.ToUnicodeEx.restype = ctypes.c_int
+ user32.ToUnicodeEx.argtypes = [
+ ctypes.c_uint, ctypes.c_uint, ctypes.c_char * 256, ctypes.c_wchar_p, ctypes.c_int, ctypes.c_uint, ctypes.c_void_p,
+ ]
user32.GetForegroundWindow.restype = ctypes.c_void_p
user32.GetClassNameW.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_int]
@@ -34,6 +41,16 @@ INPUT_KEYBOARD = 1
# the only thing that tells the two apart.
INJECTED_MARK = 0xBEA3
+# What each key types on a US keyboard, by scan code, which names the key's place rather than its
+# label. Key messages carry it as `us` both ways, and a character this layout cannot type lands on
+# its place when it has to (_by_place).
+SCAN_TO_US: Dict[int, str] = dict(zip(range(0x02, 0x0E), "1234567890-="))
+SCAN_TO_US.update(zip(range(0x10, 0x1C), "qwertyuiop[]"))
+SCAN_TO_US.update(zip(range(0x1E, 0x2A), "asdfghjkl;'`"))
+SCAN_TO_US.update(zip(range(0x2B, 0x36), "\\zxcvbnm,./"))
+US_SCAN_CODES = {char: scan for scan, char in SCAN_TO_US.items()}
+MAPVK_VSC_TO_VK = 1
+
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
@@ -114,6 +131,25 @@ def _map_virtual_key(vk: int) -> int:
return user32.MapVirtualKeyW(vk, MAPVK_VK_TO_VSC)
+def _key_at(us: str) -> Optional[Tuple[int, Optional[str]]]:
+ """The virtual key at `us`'s place on this layout and the character it types there
+ unshifted (None for a dead key or none), or None for a place with no scan code."""
+ if user32 is None:
+ raise RuntimeError("MapVirtualKeyW is only available on Windows")
+ scan = US_SCAN_CODES.get(us)
+ if scan is None:
+ return None
+ vk = user32.MapVirtualKeyW(scan, MAPVK_VSC_TO_VK)
+ if not vk:
+ return None
+ # Not MapVirtualKeyW's MAPVK_VK_TO_CHAR, which answers A to Z for the letter keys on every
+ # layout: on Russian the C key would read as "c" and a letter from an English Mac stay text.
+ # Flag 1<<2 leaves the keyboard state alone, so a dead key here eats no accent being typed.
+ buffer = ctypes.create_unicode_buffer(8)
+ count = user32.ToUnicodeEx(vk, scan, (ctypes.c_char * 256)(), buffer, len(buffer), 1 << 2, user32.GetKeyboardLayout(0))
+ return vk, buffer.value[:count] if count == 1 else None
+
+
VK_MAP = {
"enter": 0x0D,
"return": 0x0D,
@@ -201,8 +237,11 @@ MODIFIER_KEYS = {"ctrl", "ctrl_r", "alt", "alt_r", "cmd", "cmd_r", "shift", "shi
# character from.
SHIFT_KEYS = {"shift", "shift_r"}
+CHORD_KEYS = MODIFIER_KEYS - SHIFT_KEYS
+
VkLookup = Callable[[str], int]
ScanLookup = Callable[[int], int]
+PlaceLookup = Callable[[str], Optional[Tuple[int, Optional[str]]]]
# Characters we've already logged an unsupported-combo warning for, so we
# don't spam the log for every repeat keypress.
@@ -214,6 +253,30 @@ def _keybd_input(vk: int, scan: int, flags: int) -> INPUT:
return INPUT(type=INPUT_KEYBOARD, union=_INPUTUNION(ki=key_input))
+def _script(character: str) -> str:
+ return unicodedata.name(character, "").split(" ")[0]
+
+
+def _by_place(ch: str, us: Optional[str], mods_down: Set[str], place_lookup: Optional[PlaceLookup]) -> Optional[Tuple[int, Optional[str]]]:
+ """The virtual key at `us`, the place the sender's key sits on a US keyboard, and what it
+ types here, for a character this layout cannot type: under a chord, which has to reach the
+ app as a shortcut, and for a letter of another script than the one this layout types there,
+ so a Mac on Russian types this PC's English and one on English its Russian, as the PC's own
+ keyboard would. Anything else, an é from a French Mac say, stays text: the key it sits on
+ types something unrelated here. The twin of input_injector_mac._by_place."""
+ if us is None or place_lookup is None:
+ return None
+ place = place_lookup(us)
+ if place is None:
+ return None
+ here = place[1]
+ if mods_down & CHORD_KEYS:
+ return place
+ if ch.isalpha() and here and here.isalpha() and _script(ch) != _script(here):
+ return place
+ return None
+
+
def plan_key_inputs(
name: str,
down: bool,
@@ -221,12 +284,16 @@ def plan_key_inputs(
char_vk_down: Dict[str, int],
vk_lookup: VkLookup,
scan_lookup: ScanLookup,
+ us: Optional[str] = None,
+ place_lookup: Optional[PlaceLookup] = None,
) -> List[Tuple[int, int, int]]:
"""Pure planning logic: decide which (wVk, wScan, flags) tuples to send.
Mutates mods_down / char_vk_down to track state across calls, exactly
like inject_key does, so callers (tests) can inspect the bookkeeping.
- Returns an empty list if the key should be dropped.
+ Returns an empty list if the key should be dropped. `us` is where the
+ key sits on a US keyboard, sent by a physical keyboard and absent from
+ a phone's.
"""
keyup_flag = 0 if down else KEYEVENTF_KEYUP
lowered = name.lower()
@@ -257,15 +324,25 @@ def plan_key_inputs(
# fallback for whatever the VK path can't safely cover.
if len(name) == 1:
ch = name
+ if ch in char_vk_down:
+ # A repeat stays on the key the press went down on, even if the layout changed since,
+ # so its release lets go of the key that is actually down.
+ vk = char_vk_down[ch] if down else char_vk_down.pop(ch)
+ return [(vk, scan_lookup(vk), keyup_flag)]
+
+ result = vk_lookup(ch) if down else -1
+ place = _by_place(ch, us, mods_down, place_lookup) if result == -1 else None
+ if place is not None and ch.isupper() and not mods_down & (SHIFT_KEYS | CHORD_KEYS):
+ # A capital from caps lock: the key would type this layout's small letter. The
+ # release is worked out the same way, so it lets go of the same character.
+ return [(0, ord(place[1].upper()), KEYEVENTF_UNICODE | keyup_flag)]
if not down:
- if ch in char_vk_down:
- vk = char_vk_down.pop(ch)
- scan = scan_lookup(vk)
- return [(vk, scan, KEYEVENTF_KEYUP)]
return [(0, ord(ch), KEYEVENTF_UNICODE | KEYEVENTF_KEYUP)]
-
- result = vk_lookup(ch)
if result == -1:
+ if place is not None:
+ vk = place[0]
+ char_vk_down[ch] = vk
+ return [(vk, scan_lookup(vk), keyup_flag)]
if ch not in _warned_chars:
LOGGER.warning("Unsupported character for this keyboard layout: %r", ch)
_warned_chars.add(ch)
@@ -314,9 +391,9 @@ def _locked(function):
@_locked
-def inject_key(name: str, down: bool) -> None:
+def inject_key(name: str, down: bool, us: Optional[str] = None) -> None:
LOGGER.debug("key %s %s (mods held: %s)", name, "down" if down else "up", sorted(_mods_down))
- plan = plan_key_inputs(name, down, _mods_down, _char_vk_down, _vk_key_scan, _map_virtual_key)
+ plan = plan_key_inputs(name, down, _mods_down, _char_vk_down, _vk_key_scan, _map_virtual_key, us, _key_at)
if not plan:
return
_send_input(*(_keybd_input(vk, scan, flags) for vk, scan, flags in plan))
diff --git a/win_app/kvm_bridge_win.py b/win_app/kvm_bridge_win.py
index 28c7e4d..6c12fc8 100644
--- a/win_app/kvm_bridge_win.py
+++ b/win_app/kvm_bridge_win.py
@@ -2060,7 +2060,7 @@ class WindowsApplication(QWidget):
return
self.sender.start(config)
- def _on_hook_key(self, name: str, down: bool, vk: Optional[int] = None) -> bool:
+ def _on_hook_key(self, name: str, down: bool, vk: Optional[int] = None, us: Optional[str] = None) -> bool:
"""Every key, on the hook thread. The trigger is swallowed as it
switches; everything else goes to the Mac only while the Mac has
input, and a key on the ignored list not even then."""
@@ -2077,7 +2077,7 @@ class WindowsApplication(QWidget):
return True
if self._trigger.claims(name, down, self.sender.redirecting):
return True
- return self.sender.on_key(name, down, vk)
+ return self.sender.on_key(name, down, vk, us)
def _on_focus(self, target: str) -> None:
"""The Mac took input on this PC, or gave it back. Either way the
diff --git a/win_app/receiver.py b/win_app/receiver.py
index cc7b14a..4b063e8 100644
--- a/win_app/receiver.py
+++ b/win_app/receiver.py
@@ -118,9 +118,9 @@ def handle_message(message: dict, injector=None, scale: Optional[InputScale] = N
if not isinstance(data, dict):
raise protocol.ProtocolError("message data must be a JSON object")
if message_type == protocol.MSG_KEYDOWN:
- injector.inject_key(data["key"], down=True)
+ injector.inject_key(data["key"], down=True, us=data.get("us"))
elif message_type == protocol.MSG_KEYUP:
- injector.inject_key(data["key"], down=False)
+ injector.inject_key(data["key"], down=False, us=data.get("us"))
elif message_type == protocol.MSG_MOUSEMOVE:
dx, dy = int(data["dx"]), int(data["dy"])
if scale is not None and (dx or dy):
diff --git a/win_app/sender.py b/win_app/sender.py
index 735baa9..db95afd 100644
--- a/win_app/sender.py
+++ b/win_app/sender.py
@@ -349,7 +349,7 @@ class MacSender:
last = max(self._last_ack_at, self._connected_at)
return (self._clock() - last) <= ACK_TIMEOUT_SECONDS
- def on_key(self, name: str, down: bool, vk: Optional[int] = None) -> bool:
+ def on_key(self, name: str, down: bool, vk: Optional[int] = None, us: Optional[str] = None) -> bool:
# Held keys are known by their virtual key where there is one: the name is the character
# with Shift applied, so Shift let go before A made A's release arrive as "a" and miss.
held = vk if vk is not None else name
@@ -371,16 +371,24 @@ class MacSender:
if down:
# The name it went down under is kept, so its release matches even if Shift or the
# modifier style changes while it is held.
- wire = self._keys_down.get(held) or self._wire_name(name)
- self._keys_down[held] = wire
- self._enqueue({"type": protocol.MSG_KEYDOWN, "data": {"key": wire}})
+ data = self._keys_down.get(held) or self._key_data(name, us)
+ self._keys_down[held] = data
+ self._enqueue({"type": protocol.MSG_KEYDOWN, "data": data})
else:
- wire = self._keys_down.pop(held, None) or self._wire_name(name)
+ data = self._keys_down.pop(held, None) or self._key_data(name, us)
# Marked, so a release still queued when input comes home is sent
# rather than dropped by the gate: the Mac has the key-down.
- self._enqueue({"type": protocol.MSG_KEYUP, "data": {"key": wire}, _RELEASE_MARK: True})
+ self._enqueue({"type": protocol.MSG_KEYUP, "data": data, _RELEASE_MARK: True})
return True
+ def _key_data(self, name: str, us: Optional[str]) -> dict:
+ """A key message's data. `us` is the key's place on a US keyboard, which the Mac types on
+ when its layout cannot type the character: a PC on Russian sends C as "с"."""
+ data = {"key": self._wire_name(name)}
+ if us is not None:
+ data["us"] = us
+ return data
+
def _wire_name(self, name: str) -> str:
"""The capture names Ctrl "cmd" and the Windows key "ctrl", the Semantic style;
Positional swaps them back, so each key arrives as the Mac key in its place."""
@@ -575,8 +583,8 @@ class MacSender:
# The entries stay, so the physical releases here are swallowed.
self.redirecting = False
self._ignore_gate.reset()
- for wire in list(self._keys_down.values()):
- self._enqueue_control({"type": protocol.MSG_KEYUP, "data": {"key": wire}, _RELEASE_MARK: True})
+ for data in list(self._keys_down.values()):
+ self._enqueue_control({"type": protocol.MSG_KEYUP, "data": data, _RELEASE_MARK: True})
self._pin_point = None
self._last_point = None
# A fresh model, not a reset one: the old one disarmed itself at
diff --git a/win_app/tests/fakes.py b/win_app/tests/fakes.py
index 6b477f9..104211f 100644
--- a/win_app/tests/fakes.py
+++ b/win_app/tests/fakes.py
@@ -22,7 +22,7 @@ class FakeInjector:
def __init__(self):
self.calls: List[Tuple[str, tuple]] = []
- def inject_key(self, key, down):
+ def inject_key(self, key, down, us=None):
self.calls.append(("key", (key, down)))
def inject_mouse_move(self, dx, dy):
diff --git a/win_app/tests/test_replay.py b/win_app/tests/test_replay.py
index c2ec3a0..50e5e8c 100644
--- a/win_app/tests/test_replay.py
+++ b/win_app/tests/test_replay.py
@@ -50,10 +50,23 @@ class Recording:
def replay(port, data):
"""Send `data` as a fresh connection and return every byte the responder sent back before it
closed."""
+ data = bytes(data)
with socket.create_connection(("127.0.0.1", port), timeout=3.0) as attacker:
- attacker.sendall(bytes(data))
- attacker.shutdown(socket.SHUT_WR)
reply = bytearray()
+ # The responder's preamble is read before the frames go: on Windows a socket closed with
+ # bytes still unread resets the connection, and the reset throws away whatever this end
+ # had not read yet, so sending everything at once lost the preamble about a run in five.
+ attacker.sendall(data[:protocol.PREAMBLE_SIZE])
+ while len(reply) < protocol.PREAMBLE_SIZE:
+ chunk = attacker.recv(protocol.PREAMBLE_SIZE - len(reply))
+ if not chunk:
+ return bytes(reply)
+ reply += chunk
+ try:
+ attacker.sendall(data[protocol.PREAMBLE_SIZE:])
+ attacker.shutdown(socket.SHUT_WR)
+ except (ConnectionResetError, ConnectionAbortedError):
+ return bytes(reply)
while True:
try:
chunk = attacker.recv(65536)
diff --git a/win_app/tests/test_stuck_modifier_on_drop.py b/win_app/tests/test_stuck_modifier_on_drop.py
index eda60af..eb75de5 100644
--- a/win_app/tests/test_stuck_modifier_on_drop.py
+++ b/win_app/tests/test_stuck_modifier_on_drop.py
@@ -9,7 +9,7 @@ class FakeInjector:
def __init__(self):
self.calls = []
- def inject_key(self, name, down):
+ def inject_key(self, name, down, us=None):
self.calls.append((name, down))
def release_all(self):
diff --git a/win_app/tests/test_two_scripts.py b/win_app/tests/test_two_scripts.py
new file mode 100644
index 0000000..a20ba7d
--- /dev/null
+++ b/win_app/tests/test_two_scripts.py
@@ -0,0 +1,140 @@
+"""GitHub issue 3: a keyboard on one script driving a machine on another. A PC switched to Russian
+sent its C key as "с", which a Mac on English cannot type, so it arrived as text with Ctrl
+cleared and Ctrl+C typed "с"; a Mac on Russian did the same to a PC on English. Keys now carry
+their place on a US keyboard (`us`), from the scan code here and the key code on the Mac, and a
+character the receiving layout cannot type lands on that place when a shortcut needs it or its
+letter is from another script than the one typed there."""
+
+import time
+import unittest
+
+import capture_win
+import protocol
+import receiver
+import sender
+from fakes import FakeClipboard, FakeDesktop
+from input_injector import KEYEVENTF_KEYUP, KEYEVENTF_UNICODE, plan_key_inputs
+from test_key_up_after_switch_back import MONITORS, make_config
+
+VK_C, VK_2 = 0x43, 0x32
+SCANS = {VK_C: 0x2E, VK_2: 0x03}
+# VkKeyScanW on each layout, and what the key at each US place is and types there.
+RUSSIAN = {"с": VK_C, "С": 0x100 | VK_C, "2": VK_2}
+ENGLISH = {"c": VK_C, "C": 0x100 | VK_C, "2": VK_2}
+RUSSIAN_PLACES = {"c": (VK_C, "с"), "2": (VK_2, "2")}
+ENGLISH_PLACES = {"c": (VK_C, "c"), "2": (VK_2, "2")}
+
+
+def plan(name, layout, places, mods=(), us=None, down=True, held=None):
+ return plan_key_inputs(
+ name, down, set(mods), {} if held is None else held,
+ lambda ch: layout.get(ch, -1), lambda vk: SCANS.get(vk, 0), us, places.get,
+ )
+
+
+class PlaceTests(unittest.TestCase):
+ def test_cmd_c_from_a_mac_on_english_is_ctrl_c_on_a_pc_on_russian(self):
+ self.assertEqual(plan("c", RUSSIAN, RUSSIAN_PLACES, {"cmd"}, us="c"), [(VK_C, 0x2E, 0)])
+
+ def test_cmd_c_from_a_mac_on_russian_is_ctrl_c_on_a_pc_on_english(self):
+ self.assertEqual(plan("с", ENGLISH, ENGLISH_PLACES, {"cmd"}, us="c"), [(VK_C, 0x2E, 0)])
+
+ def test_a_letter_from_the_other_script_types_this_pcs_letter(self):
+ self.assertEqual(plan("с", ENGLISH, ENGLISH_PLACES, us="c"), [(VK_C, 0x2E, 0)])
+ self.assertEqual(plan("c", RUSSIAN, RUSSIAN_PLACES, us="c"), [(VK_C, 0x2E, 0)])
+ self.assertEqual(plan("С", ENGLISH, ENGLISH_PLACES, {"shift"}, us="c"), [(VK_C, 0x2E, 0)])
+
+ def test_a_capital_without_shift_is_this_pcs_capital(self):
+ self.assertEqual(plan("С", ENGLISH, ENGLISH_PLACES, us="c"), [(0, ord("C"), KEYEVENTF_UNICODE)])
+
+ def test_a_capital_without_shift_lets_go_of_the_character_it_typed(self):
+ self.assertEqual(plan("С", ENGLISH, ENGLISH_PLACES, us="c", down=False), [(0, ord("C"), KEYEVENTF_UNICODE | KEYEVENTF_KEYUP)])
+
+ def test_a_repeat_after_a_layout_switch_stays_on_the_key_that_is_down(self):
+ held = {}
+ plan("с", ENGLISH, ENGLISH_PLACES, us="c", held=held)
+ self.assertEqual(plan("с", RUSSIAN, {"c": (0x99, "с")}, us="c", held=held), [(VK_C, 0x2E, 0)])
+ self.assertEqual(plan("с", RUSSIAN, {"c": (0x99, "с")}, us="c", down=False, held=held), [(VK_C, 0x2E, KEYEVENTF_KEYUP)])
+
+ def test_the_release_lets_go_of_the_key_the_press_went_down_on(self):
+ held = {}
+ plan("с", ENGLISH, ENGLISH_PLACES, us="c", held=held)
+ self.assertEqual(plan("с", ENGLISH, ENGLISH_PLACES, us="c", down=False, held=held), [(VK_C, 0x2E, KEYEVENTF_KEYUP)])
+ self.assertEqual(held, {})
+
+ def test_an_accent_from_the_same_script_is_still_typed_as_text(self):
+ # French é sits on the US 2 key; an English PC has no é, and 2 is not what was meant.
+ self.assertEqual(plan("é", ENGLISH, ENGLISH_PLACES, us="2"), [(0, ord("é"), KEYEVENTF_UNICODE)])
+
+ def test_a_shortcut_on_an_accented_key_lands_on_its_place(self):
+ self.assertEqual(plan("é", ENGLISH, ENGLISH_PLACES, {"cmd"}, us="2"), [(VK_2, 0x03, 0)])
+
+ def test_a_key_with_no_place_keeps_the_old_answer(self):
+ # A phone's keyboard, or a Mac on an older Beamer, sends no place.
+ self.assertEqual(plan("с", ENGLISH, ENGLISH_PLACES, {"cmd"}), [(0, ord("с"), KEYEVENTF_UNICODE)])
+
+ def test_the_receiver_hands_the_place_on(self):
+ class Injector:
+ def __init__(self):
+ self.calls = []
+
+ def inject_key(self, name, down, us=None):
+ self.calls.append((name, down, us))
+
+ injector = Injector()
+ receiver.handle_message({"type": protocol.MSG_KEYDOWN, "data": {"key": "с", "us": "c"}}, injector=injector)
+ receiver.handle_message({"type": protocol.MSG_KEYUP, "data": {"key": "с"}}, injector=injector)
+ self.assertEqual(injector.calls, [("с", True, "c"), ("с", False, None)])
+
+
+class CaptureTests(unittest.TestCase):
+ def test_scan_codes_name_the_us_place(self):
+ self.assertEqual(capture_win.SCAN_TO_US[0x2E], "c")
+ self.assertEqual(capture_win.SCAN_TO_US[0x10], "q")
+ self.assertEqual(capture_win.SCAN_TO_US[0x02], "1")
+ self.assertEqual(capture_win.SCAN_TO_US[0x29], "`")
+ self.assertEqual(capture_win.SCAN_TO_US[0x2B], "\\")
+ self.assertEqual(capture_win.SCAN_TO_US[0x35], "/")
+ self.assertEqual(len(capture_win.SCAN_TO_US), 47)
+
+
+class SenderTests(unittest.TestCase):
+ def setUp(self):
+ self.sender = sender.MacSender(desktop=FakeDesktop(MONITORS, cursor=(0, 500)), clipboard=FakeClipboard(), is_local=lambda host: False)
+ self.sender.update_config(make_config())
+ self.sender._sock = object()
+ self.sender._connected_at = time.monotonic()
+ self.sender._last_ack_at = time.monotonic()
+ self.sender.set_redirecting(True, arrival_edge="right", offset=0.5)
+
+ def tearDown(self):
+ self.sender._sock = None
+
+ def keys(self):
+ sent = []
+ while not self.sender._outbound.empty():
+ message = self.sender._outbound.get_nowait()
+ if message.get("type") in (protocol.MSG_KEYDOWN, protocol.MSG_KEYUP):
+ sent.append((message["type"], message["data"]))
+ return sent
+
+ def test_a_key_carries_its_place_both_ways(self):
+ self.sender.on_key("с", True, VK_C, "c")
+ self.sender.on_key("с", False, VK_C, "c")
+ self.assertEqual(self.keys(), [
+ (protocol.MSG_KEYDOWN, {"key": "с", "us": "c"}),
+ (protocol.MSG_KEYUP, {"key": "с", "us": "c"}),
+ ])
+
+ def test_the_release_sent_as_input_comes_home_carries_it_too(self):
+ self.sender.on_key("с", True, VK_C, "c")
+ self.sender.set_redirecting(False)
+ self.assertEqual(self.keys()[-1], (protocol.MSG_KEYUP, {"key": "с", "us": "c"}))
+
+ def test_a_named_key_has_no_place(self):
+ self.sender.on_key("enter", True, 0x0D, None)
+ self.assertEqual(self.keys(), [(protocol.MSG_KEYDOWN, {"key": "enter"})])
+
+
+if __name__ == "__main__":
+ unittest.main()