Skip to content

Commit 53a98df

Browse files
committed
reuse keycode map from fcitx5-js
1 parent 6891403 commit 53a98df

File tree

4 files changed

+240
-2
lines changed

4 files changed

+240
-2
lines changed

keyboard/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_executable(keyboard MACOSX_BUNDLE
22
KeyboardViewController.swift
33
util.swift
44
fcitx.cpp
5+
keycode.cpp
56
)
67

78
set_target_properties(keyboard PROPERTIES

keyboard/fcitx.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../common/common.h"
44
#include "../common/util.h"
55
#include "fcitx.h"
6+
#include "keycode.h"
67

78
#include <fcitx/inputmethodmanager.h>
89

@@ -28,8 +29,10 @@ void focusOut(id client) {
2829
}
2930

3031
bool processKey(const char *key, const char *code) {
31-
return with_fcitx(
32-
[key] { return frontend->keyEvent(fcitx::Key{key}, false); });
32+
return with_fcitx([=] {
33+
return frontend->keyEvent(fcitx::js_key_to_fcitx_key(key, code, 0),
34+
false);
35+
});
3336
}
3437

3538
void reload() {

keyboard/keycode.cpp

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#include "keycode.h"
2+
#include <fcitx-utils/log.h>
3+
4+
namespace fcitx {
5+
static struct {
6+
std::string jsCode;
7+
KeySym sym;
8+
} sym_mappings[] = {
9+
// modifiers
10+
{"ControlLeft", FcitxKey_Control_L},
11+
{"ControlRight", FcitxKey_Control_R},
12+
{"ShiftLeft", FcitxKey_Shift_L},
13+
{"ShiftRight", FcitxKey_Shift_R},
14+
{"CapsLock", FcitxKey_Caps_Lock},
15+
{"AltLeft", FcitxKey_Alt_L},
16+
{"AltRight", FcitxKey_Alt_R},
17+
{"MetaLeft", FcitxKey_Super_L},
18+
{"MetaRight", FcitxKey_Super_R},
19+
20+
// keypad
21+
{"Numpad0", FcitxKey_KP_0},
22+
{"Numpad1", FcitxKey_KP_1},
23+
{"Numpad2", FcitxKey_KP_2},
24+
{"Numpad3", FcitxKey_KP_3},
25+
{"Numpad4", FcitxKey_KP_4},
26+
{"Numpad5", FcitxKey_KP_5},
27+
{"Numpad6", FcitxKey_KP_6},
28+
{"Numpad7", FcitxKey_KP_7},
29+
{"Numpad8", FcitxKey_KP_8},
30+
{"Numpad9", FcitxKey_KP_9},
31+
{"NumpadComma", FcitxKey_KP_Separator},
32+
{"NumpadDecimal", FcitxKey_KP_Decimal},
33+
{"NumpadEqual", FcitxKey_KP_Equal},
34+
{"NumpadSubtract", FcitxKey_KP_Subtract},
35+
{"NumpadMultiply", FcitxKey_KP_Multiply},
36+
{"NumpadAdd", FcitxKey_KP_Add},
37+
{"NumpadDivide", FcitxKey_KP_Divide},
38+
39+
// special
40+
{"Backspace", FcitxKey_BackSpace},
41+
{"NumpadEnter", FcitxKey_KP_Enter},
42+
{"Enter", FcitxKey_Return},
43+
{"Space", FcitxKey_space},
44+
{"Tab", FcitxKey_Tab},
45+
{"Escape", FcitxKey_Escape},
46+
{"Delete", FcitxKey_Delete},
47+
{"Insert", FcitxKey_Insert},
48+
{"PageUp", FcitxKey_Page_Up},
49+
{"PageDown", FcitxKey_Page_Down},
50+
{"Home", FcitxKey_Home},
51+
{"End", FcitxKey_End},
52+
53+
// arrow keys
54+
{"ArrowUp", FcitxKey_Up},
55+
{"ArrowDown", FcitxKey_Down},
56+
{"ArrowLeft", FcitxKey_Left},
57+
{"ArrowRight", FcitxKey_Right},
58+
59+
// function keys
60+
{"F1", FcitxKey_F1},
61+
{"F2", FcitxKey_F2},
62+
{"F3", FcitxKey_F3},
63+
{"F4", FcitxKey_F4},
64+
{"F5", FcitxKey_F5},
65+
{"F6", FcitxKey_F6},
66+
{"F7", FcitxKey_F7},
67+
{"F8", FcitxKey_F8},
68+
{"F9", FcitxKey_F9},
69+
{"F10", FcitxKey_F10},
70+
{"F11", FcitxKey_F11},
71+
{"F12", FcitxKey_F12},
72+
};
73+
74+
static struct {
75+
std::string jsKeycode;
76+
uint16_t linuxKeycode;
77+
} code_mappings[] = {
78+
// alphabet
79+
{"KeyA", 30},
80+
{"KeyB", 48},
81+
{"KeyC", 46},
82+
{"KeyD", 32},
83+
{"KeyE", 18},
84+
{"KeyF", 33},
85+
{"KeyG", 34},
86+
{"KeyH", 35},
87+
{"KeyI", 23},
88+
{"KeyJ", 36},
89+
{"KeyK", 37},
90+
{"KeyL", 38},
91+
{"KeyM", 50},
92+
{"KeyN", 49},
93+
{"KeyO", 24},
94+
{"KeyP", 25},
95+
{"KeyQ", 16},
96+
{"KeyR", 19},
97+
{"KeyS", 31},
98+
{"KeyT", 20},
99+
{"KeyU", 22},
100+
{"KeyV", 47},
101+
{"KeyW", 17},
102+
{"KeyX", 45},
103+
{"KeyY", 21},
104+
{"KeyZ", 44},
105+
106+
// number
107+
{"Digit0", 11},
108+
{"Digit1", 2},
109+
{"Digit2", 3},
110+
{"Digit3", 4},
111+
{"Digit4", 5},
112+
{"Digit5", 6},
113+
{"Digit6", 7},
114+
{"Digit7", 8},
115+
{"Digit8", 9},
116+
{"Digit9", 10},
117+
118+
// symbol
119+
{"Backquote", 41},
120+
{"Backslash", 43},
121+
{"BracketLeft", 26},
122+
{"BracketRight", 27},
123+
{"Comma", 51},
124+
{"Period", 52},
125+
{"Equal", 13},
126+
{"Minus", 12},
127+
{"Quote", 40},
128+
{"Semicolon", 39},
129+
{"Slash", 53},
130+
131+
// keypad
132+
{"Numpad0", 82},
133+
{"Numpad1", 79},
134+
{"Numpad2", 80},
135+
{"Numpad3", 81},
136+
{"Numpad4", 75},
137+
{"Numpad5", 76},
138+
{"Numpad6", 77},
139+
{"Numpad7", 71},
140+
{"Numpad8", 72},
141+
{"Numpad9", 73},
142+
// {OSX_VK_KEYPAD_CLEAR, }, XXX: not sure map to what
143+
{"NumpadComma", 121},
144+
{"NumpadDecimal", 83},
145+
{"NumpadEqual", 117},
146+
{"NumpadSubtract", 74},
147+
{"NumpadMultiply", 55},
148+
{"NumpadAdd", 78},
149+
{"NumpadDivide", 98},
150+
151+
// special
152+
{"Backspace", 14},
153+
{"NumpadEnter", 96},
154+
// {OSX_VK_ENTER_POWERBOOK, }, XXX: not sure map to what
155+
{"Escape", 1},
156+
{"Delete", 111},
157+
// {OSX_VK_HELP, }, XXX: not sure map to what
158+
{"Enter", 28},
159+
{"Space", 57},
160+
{"Tab", 15},
161+
162+
// function
163+
{"F1", 59},
164+
{"F2", 60},
165+
{"F3", 61},
166+
{"F4", 62},
167+
{"F5", 63},
168+
{"F6", 64},
169+
{"F7", 65},
170+
{"F8", 66},
171+
{"F9", 67},
172+
{"F10", 68},
173+
{"F11", 87},
174+
{"F12", 88},
175+
176+
// cursor
177+
{"ArrowUp", 103},
178+
{"ArrowDown", 108},
179+
{"ArrowLeft", 105},
180+
{"ArrowRight", 106},
181+
182+
{"PageUp", 104},
183+
{"PageDown", 109},
184+
{"Home", 102},
185+
{"End", 107},
186+
187+
// modifiers
188+
{"CapsLock", 58},
189+
{"MetaLeft", 125},
190+
{"MetaRight", 126},
191+
{"ControlLeft", 29},
192+
{"ControlRight", 97},
193+
{"Fn", 0x1d0},
194+
{"AltLeft", 56},
195+
{"AltRight", 100},
196+
{"ShiftLeft", 42},
197+
{"ShiftRight", 54},
198+
};
199+
200+
KeySym js_key_to_fcitx_keysym(const std::string &key, const std::string &code) {
201+
for (const auto &pair : sym_mappings) {
202+
if (pair.jsCode == code) {
203+
return pair.sym;
204+
}
205+
}
206+
if (key.size() == 1) {
207+
return Key::keySymFromUnicode(key[0]);
208+
}
209+
FCITX_ERROR() << "Unrecognized key " << key << " " << code;
210+
return {};
211+
}
212+
213+
uint16_t js_keycode_to_fcitx_keycode(const std::string &code) {
214+
for (const auto &pair : code_mappings) {
215+
if (pair.jsKeycode == code) {
216+
return pair.linuxKeycode + 8 /* evdev offset */;
217+
}
218+
}
219+
return 0;
220+
}
221+
222+
Key js_key_to_fcitx_key(const std::string &key, const std::string &code,
223+
uint32_t modifiers) {
224+
return Key{js_key_to_fcitx_keysym(key, code), KeyStates{modifiers},
225+
js_keycode_to_fcitx_keycode(code)};
226+
}
227+
} // namespace fcitx

keyboard/keycode.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <fcitx-utils/key.h>
3+
4+
namespace fcitx {
5+
Key js_key_to_fcitx_key(const std::string &key, const std::string &code,
6+
uint32_t modifiers);
7+
}

0 commit comments

Comments
 (0)