Skip to content

Commit 2822fc6

Browse files
committed
fix: rebuild the key layout map when the keyboard type changes
A layout's keys produce different characters on ANSI, ISO and JIS keyboards, so the map is keyed by the keyboard type as well as the input source, and the backend interface now says that a typed character's event names its key.
1 parent e6d25d9 commit 2822fc6

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

‎include/robot/backend/IKeyboardBackend.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ namespace robot::backend {
2323
// intended. On Windows and X11 the separately-injected modifier key-down is
2424
// usually sufficient, but tracking state keeps behaviour uniform.
2525
//
26-
// typeUnicode is a distinct path that injects a character directly, bypassing
27-
// keycodes and layout entirely (CGEventKeyboardSetUnicodeString, KEYEVENTF_
28-
// UNICODE, XTest with a temporarily remapped keysym). It is the only correct way
29-
// to emit arbitrary characters and must not be emulated with keyDown/keyUp.
26+
// typeUnicode is a distinct path that delivers a character by value
27+
// (CGEventKeyboardSetUnicodeString, KEYEVENTF_UNICODE, XTest with a temporarily
28+
// remapped keysym), whatever the layout. It is the only correct way to emit
29+
// arbitrary characters and must not be emulated with keyDown/keyUp. Where the
30+
// event also names a key (macOS), it names the key of the current layout that
31+
// types the character, so a keyup names the same key as its keydown.
3032
class IKeyboardBackend {
3133
public:
3234
virtual ~IKeyboardBackend() = default;

‎src/platform/macos/MacKeyLayout.cpp‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ constexpr std::array<Layer, 4> kLayers{{
2929
// block's, so a digit or a period maps to its main key.
3030
constexpr UInt16 kKeycodeCount = 128;
3131

32+
// A layout's keys produce different characters on ANSI, ISO and JIS keyboards,
33+
// so the map belongs to the pair.
3234
struct LayoutMap {
3335
std::string sourceId;
36+
UInt32 keyboardType = 0;
3437
std::unordered_map<char32_t, KeyStroke> strokes;
3538
};
3639

@@ -69,16 +72,16 @@ std::optional<char32_t> singleScalar(
6972

7073
// A key that starts a dead-key sequence leaves a dead state and types nothing
7174
// yet, so it is not mapped: its character takes the text-only key instead.
72-
LayoutMap buildMap(TISInputSourceRef source, std::string sourceId) {
73-
LayoutMap map{std::move(sourceId), {}};
75+
LayoutMap buildMap(
76+
TISInputSourceRef source, std::string sourceId, const UInt32 keyboardType
77+
) {
78+
LayoutMap map{std::move(sourceId), keyboardType, {}};
7479
const auto data = static_cast<CFDataRef>(
7580
TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData)
7681
);
7782
if (data == nullptr) return map;
7883
const auto* layout =
7984
reinterpret_cast<const UCKeyboardLayout*>(CFDataGetBytePtr(data));
80-
const UInt32 keyboardType = LMGetKbdType();
81-
8285
for (const Layer& layer : kLayers) {
8386
for (UInt16 keycode = 0; keycode < kKeycodeCount; ++keycode) {
8487
UInt32 deadState = 0;
@@ -106,12 +109,14 @@ std::optional<KeyStroke> keyStrokeForCharacter(const char32_t codepoint) {
106109
TISInputSourceRef source = TISCopyCurrentKeyboardLayoutInputSource();
107110
if (source == nullptr) return std::nullopt;
108111
std::string sourceId = inputSourceId(source);
112+
const UInt32 keyboardType = LMGetKbdType();
109113

110114
const std::scoped_lock lock(mutex);
111115
// The user can switch layouts between two calls, so the map follows the
112116
// current one.
113-
if (cached.sourceId != sourceId || sourceId.empty()) {
114-
cached = buildMap(source, std::move(sourceId));
117+
if (cached.sourceId != sourceId || cached.keyboardType != keyboardType ||
118+
sourceId.empty()) {
119+
cached = buildMap(source, std::move(sourceId), keyboardType);
115120
}
116121
CFRelease(source);
117122

0 commit comments

Comments
 (0)