Skip to content

Commit e023280

Browse files
authored
Merge pull request #13 from Forairaaaaa/fix-modifier-handle-order
Fix modifier handle order
2 parents 25f1642 + fae0457 commit e023280

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/m5stack/M5Cardputer.git"
1212
},
13-
"version": "1.1.0",
13+
"version": "1.1.1",
1414
"frameworks": [
1515
"arduino"
1616
],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5Cardputer
2-
version=1.1.0
2+
version=1.1.1
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=Library for M5Stack M5Cardputer and M5Cardputer-ADV Board

src/utility/Keyboard/Keyboard.cpp

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ bool Keyboard_Class::isKeyPressed(char c)
8989

9090
void Keyboard_Class::updateKeysState()
9191
{
92+
// printf("-------------------\n");
93+
9294
_keys_state_buffer.reset();
9395
_key_pos_print_keys.clear();
9496
_key_pos_hid_keys.clear();
9597
_key_pos_modifier_keys.clear();
9698

97-
const auto& keys = keyList();
98-
99-
// 预分配容器大小以避免重复分配
99+
const auto& keys = keyList();
100100
const size_t key_count = keys.size();
101+
102+
// Pre-allocate containers to avoid re-allocation
101103
if (key_count > 0) {
102104
_key_pos_print_keys.reserve(key_count);
103105
_key_pos_hid_keys.reserve(key_count);
@@ -107,84 +109,101 @@ void Keyboard_Class::updateKeysState()
107109
_keys_state_buffer.word.reserve(key_count);
108110
}
109111

112+
// =================================================================
113+
// PASS 1: Identify all active modifier keys first.
114+
// This ensures their state is known before processing other keys.
115+
// =================================================================
110116
for (const auto& key_pos : keys) {
111-
const KeyValue_t key_value = getKeyValue(key_pos);
112-
const uint8_t key_code = key_value.value_first;
113-
117+
const uint8_t key_code = getKeyValue(key_pos).value_first;
114118
switch (key_code) {
115-
// 修饰键处理
116119
case KEY_FN:
117120
_keys_state_buffer.fn = true;
118-
continue;
119-
121+
break;
120122
case KEY_OPT:
121123
_keys_state_buffer.opt = true;
122-
continue;
123-
124+
break;
124125
case KEY_LEFT_CTRL:
125126
_keys_state_buffer.ctrl = true;
127+
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
126128
_key_pos_modifier_keys.push_back(key_pos);
127129
_keys_state_buffer.modifier_keys.push_back(key_code);
128-
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
129-
continue;
130-
130+
break;
131131
case KEY_LEFT_SHIFT:
132132
_keys_state_buffer.shift = true;
133+
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
133134
_key_pos_modifier_keys.push_back(key_pos);
134135
_keys_state_buffer.modifier_keys.push_back(key_code);
135-
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
136-
continue;
137-
136+
break;
138137
case KEY_LEFT_ALT:
139138
_keys_state_buffer.alt = true;
139+
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
140140
_key_pos_modifier_keys.push_back(key_pos);
141141
_keys_state_buffer.modifier_keys.push_back(key_code);
142-
_keys_state_buffer.modifiers |= (1 << (key_code - 0x80));
143-
continue;
142+
break;
143+
// Note: We only handle modifiers in this pass.
144+
}
145+
}
144146

145-
// 功能键处理
147+
// =================================================================
148+
// PASS 2: Process all non-modifier keys.
149+
// Now the modifier state is correct, regardless of key order.
150+
// =================================================================
151+
for (const auto& key_pos : keys) {
152+
const KeyValue_t key_value = getKeyValue(key_pos);
153+
const uint8_t key_code = key_value.value_first;
154+
155+
// printf("%d\n", key_code);
156+
157+
// Skip modifier keys as they were handled in the first pass
158+
switch (key_code) {
159+
case KEY_FN:
160+
case KEY_OPT:
161+
case KEY_LEFT_CTRL:
162+
case KEY_LEFT_SHIFT:
163+
case KEY_LEFT_ALT:
164+
continue; // Already processed
165+
}
166+
167+
// Handle other special keys
168+
switch (key_code) {
146169
case KEY_TAB:
147170
_keys_state_buffer.tab = true;
148171
_key_pos_hid_keys.push_back(key_pos);
149172
_keys_state_buffer.hid_keys.push_back(key_code);
150-
continue;
151-
173+
continue; // Skip further processing for this key
152174
case KEY_BACKSPACE:
153175
_keys_state_buffer.del = true;
154176
_key_pos_hid_keys.push_back(key_pos);
155177
_keys_state_buffer.hid_keys.push_back(key_code);
156-
continue;
157-
178+
continue; // Skip further processing for this key
158179
case KEY_ENTER:
159180
_keys_state_buffer.enter = true;
160181
_key_pos_hid_keys.push_back(key_pos);
161182
_keys_state_buffer.hid_keys.push_back(key_code);
162-
continue;
163-
164-
case ' ':
165-
_keys_state_buffer.space = true;
166-
break; // 空格键需要继续后续处理
183+
continue; // Skip further processing for this key
184+
}
167185

168-
default:
169-
break; // 普通按键继续后续处理
186+
// Handle printable keys (including space)
187+
if (key_code == ' ') {
188+
_keys_state_buffer.space = true;
170189
}
171190

172-
// 处理普通按键和空格键
173191
_key_pos_hid_keys.push_back(key_pos);
174192
_key_pos_print_keys.push_back(key_pos);
175193

176-
// 直接处理HID键值转换
177-
if (key_code != ' ') { // 空格已在上面处理
178-
const uint8_t hid_key = _kb_asciimap[key_code];
179-
if (hid_key) {
180-
_keys_state_buffer.hid_keys.push_back(hid_key);
181-
}
194+
// Add HID key to the list
195+
const uint8_t hid_key = _kb_asciimap[key_code];
196+
if (hid_key) {
197+
_keys_state_buffer.hid_keys.push_back(hid_key);
182198
}
183199

184-
// 直接处理字符输出
200+
// Add character to the word buffer, now with the correct modifier state
201+
// printf("%d %d %d\n", _keys_state_buffer.ctrl, _keys_state_buffer.shift, _is_caps_locked);
185202
if (_keys_state_buffer.ctrl || _keys_state_buffer.shift || _is_caps_locked) {
203+
// printf("push_back %c\n", key_value.value_second);
186204
_keys_state_buffer.word.push_back(key_value.value_second);
187205
} else {
206+
// printf("push_back %c\n", key_value.value_first);
188207
_keys_state_buffer.word.push_back(key_value.value_first);
189208
}
190209
}

0 commit comments

Comments
 (0)