Skip to content

Commit 4366f11

Browse files
committed
feat(usb): add ALT escape input for USB HID keyboard
1 parent 6069bdc commit 4366f11

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

examples/peripherals/usb/host/cherryusb_host/main/hid.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ typedef struct {
3737
#define KEYBOARD_ENTER_MAIN_CHAR '\r'
3838
/* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */
3939
#define KEYBOARD_ENTER_LF_EXTEND 1
40+
/* When set to 1, numbers entered from the numeric keypad while ALT is pressed will be escaped */
41+
#define KEYBOARD_ENTER_ALT_ESCAPE 1
42+
43+
#if KEYBOARD_ENTER_ALT_ESCAPE
44+
static bool escaping = false;
45+
static unsigned char escap_hex = 0;
46+
#endif
4047

4148
/**
4249
* @brief Scancode to ascii table
@@ -140,6 +147,25 @@ static inline bool hid_keyboard_is_modifier_shift(uint8_t modifier)
140147
return false;
141148
}
142149

150+
#if KEYBOARD_ENTER_ALT_ESCAPE
151+
/**
152+
* @brief HID Keyboard modifier verification for capitalization application (right or left alt)
153+
*
154+
* @param[in] modifier
155+
* @return true Modifier was pressed (left or right alt)
156+
* @return false Modifier was not pressed (left or right alt)
157+
*
158+
*/
159+
static inline bool hid_keyboard_is_modifier_alt(uint8_t modifier)
160+
{
161+
if (((modifier & HID_MODIFIER_LALT) == HID_MODIFIER_LALT) ||
162+
((modifier & HID_MODIFIER_RALT) == HID_MODIFIER_RALT)) {
163+
return true;
164+
}
165+
return false;
166+
}
167+
#endif
168+
143169
/**
144170
* @brief HID Keyboard get char symbol from key code
145171
*
@@ -156,6 +182,18 @@ static inline bool hid_keyboard_get_char(uint8_t modifier,
156182
{
157183
uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0;
158184

185+
#if KEYBOARD_ENTER_ALT_ESCAPE
186+
if (escaping) {
187+
if ((key_code >= HID_KBD_USAGE_KPD1) && (key_code <= HID_KBD_USAGE_KPD0)) {
188+
if (key_code == HID_KBD_USAGE_KPD0) {
189+
key_code = HID_KBD_USAGE_KPD1 - 1;
190+
}
191+
escap_hex = escap_hex * 10 + (key_code - (HID_KBD_USAGE_KPD1 - 1));
192+
}
193+
return false;
194+
}
195+
#endif
196+
159197
if ((key_code >= HID_KBD_USAGE_A) && (key_code <= HID_KBD_USAGE_QUESTION)) {
160198
*key_char = keycode2ascii[key_code][mod];
161199
} else {
@@ -233,6 +271,20 @@ static void usbh_hid_keyboard_report_callback(void *arg, int nbytes)
233271
static uint8_t prev_keys[sizeof(kb_report->key)] = { 0 };
234272
key_event_t key_event;
235273

274+
#if KEYBOARD_ENTER_ALT_ESCAPE
275+
if (hid_keyboard_is_modifier_alt(kb_report->modifier)) {
276+
if (escaping == false) {
277+
escaping = true;
278+
escap_hex = 0;
279+
}
280+
} else {
281+
if (escaping && escap_hex > 0) {
282+
escaping = false;
283+
hid_keyboard_print_char(escap_hex);
284+
}
285+
}
286+
#endif
287+
236288
for (int i = 0; i < sizeof(kb_report->key); i++) {
237289

238290
// key has been released verification

examples/peripherals/usb/host/hid/main/hid_host_example.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ typedef struct {
8383
#define KEYBOARD_ENTER_MAIN_CHAR '\r'
8484
/* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */
8585
#define KEYBOARD_ENTER_LF_EXTEND 1
86+
/* When set to 1, numbers entered from the numeric keypad while ALT is pressed will be escaped */
87+
#define KEYBOARD_ENTER_ALT_ESCAPE 1
88+
89+
#if KEYBOARD_ENTER_ALT_ESCAPE
90+
static bool escaping = false;
91+
static unsigned char escap_hex = 0;
92+
#endif
8693

8794
/**
8895
* @brief Scancode to ascii table
@@ -187,6 +194,25 @@ static inline bool hid_keyboard_is_modifier_shift(uint8_t modifier)
187194
return false;
188195
}
189196

197+
#if KEYBOARD_ENTER_ALT_ESCAPE
198+
/**
199+
* @brief HID Keyboard modifier verification for capitalization application (right or left alt)
200+
*
201+
* @param[in] modifier
202+
* @return true Modifier was pressed (left or right alt)
203+
* @return false Modifier was not pressed (left or right alt)
204+
*
205+
*/
206+
static inline bool hid_keyboard_is_modifier_alt(uint8_t modifier)
207+
{
208+
if (((modifier & HID_LEFT_ALT) == HID_LEFT_ALT) ||
209+
((modifier & HID_RIGHT_ALT) == HID_RIGHT_ALT)) {
210+
return true;
211+
}
212+
return false;
213+
}
214+
#endif
215+
190216
/**
191217
* @brief HID Keyboard get char symbol from key code
192218
*
@@ -203,6 +229,18 @@ static inline bool hid_keyboard_get_char(uint8_t modifier,
203229
{
204230
uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0;
205231

232+
#if KEYBOARD_ENTER_ALT_ESCAPE
233+
if (escaping) {
234+
if ((key_code >= HID_KEY_KEYPAD_1) && (key_code <= HID_KEY_KEYPAD_0)) {
235+
if (key_code == HID_KEY_KEYPAD_0) {
236+
key_code = HID_KEY_KEYPAD_1 - 1;
237+
}
238+
escap_hex = escap_hex * 10 + (key_code - (HID_KEY_KEYPAD_1 - 1));
239+
}
240+
return false;
241+
}
242+
#endif
243+
206244
if ((key_code >= HID_KEY_A) && (key_code <= HID_KEY_SLASH)) {
207245
*key_char = keycode2ascii[key_code][mod];
208246
} else {
@@ -289,6 +327,20 @@ static void hid_host_keyboard_report_callback(const uint8_t *const data, const i
289327
static uint8_t prev_keys[HID_KEYBOARD_KEY_MAX] = { 0 };
290328
key_event_t key_event;
291329

330+
#if KEYBOARD_ENTER_ALT_ESCAPE
331+
if (hid_keyboard_is_modifier_alt(kb_report->modifier.val)) {
332+
if (escaping == false) {
333+
escaping = true;
334+
escap_hex = 0;
335+
}
336+
} else {
337+
if (escaping && escap_hex > 0) {
338+
escaping = false;
339+
hid_keyboard_print_char(escap_hex);
340+
}
341+
}
342+
#endif
343+
292344
for (int i = 0; i < HID_KEYBOARD_KEY_MAX; i++) {
293345

294346
// key has been released verification

0 commit comments

Comments
 (0)