feat(usb): add ALT escape input for USB HID keyboard

This commit is contained in:
LiPeng
2025-07-04 18:12:21 +08:00
committed by Li Peng
parent 6069bdcdca
commit 4366f118ed
2 changed files with 104 additions and 0 deletions

View File

@@ -37,6 +37,13 @@ typedef struct {
#define KEYBOARD_ENTER_MAIN_CHAR '\r' #define KEYBOARD_ENTER_MAIN_CHAR '\r'
/* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */ /* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */
#define KEYBOARD_ENTER_LF_EXTEND 1 #define KEYBOARD_ENTER_LF_EXTEND 1
/* When set to 1, numbers entered from the numeric keypad while ALT is pressed will be escaped */
#define KEYBOARD_ENTER_ALT_ESCAPE 1
#if KEYBOARD_ENTER_ALT_ESCAPE
static bool escaping = false;
static unsigned char escap_hex = 0;
#endif
/** /**
* @brief Scancode to ascii table * @brief Scancode to ascii table
@@ -140,6 +147,25 @@ static inline bool hid_keyboard_is_modifier_shift(uint8_t modifier)
return false; return false;
} }
#if KEYBOARD_ENTER_ALT_ESCAPE
/**
* @brief HID Keyboard modifier verification for capitalization application (right or left alt)
*
* @param[in] modifier
* @return true Modifier was pressed (left or right alt)
* @return false Modifier was not pressed (left or right alt)
*
*/
static inline bool hid_keyboard_is_modifier_alt(uint8_t modifier)
{
if (((modifier & HID_MODIFIER_LALT) == HID_MODIFIER_LALT) ||
((modifier & HID_MODIFIER_RALT) == HID_MODIFIER_RALT)) {
return true;
}
return false;
}
#endif
/** /**
* @brief HID Keyboard get char symbol from key code * @brief HID Keyboard get char symbol from key code
* *
@@ -156,6 +182,18 @@ static inline bool hid_keyboard_get_char(uint8_t modifier,
{ {
uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0; uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0;
#if KEYBOARD_ENTER_ALT_ESCAPE
if (escaping) {
if ((key_code >= HID_KBD_USAGE_KPD1) && (key_code <= HID_KBD_USAGE_KPD0)) {
if (key_code == HID_KBD_USAGE_KPD0) {
key_code = HID_KBD_USAGE_KPD1 - 1;
}
escap_hex = escap_hex * 10 + (key_code - (HID_KBD_USAGE_KPD1 - 1));
}
return false;
}
#endif
if ((key_code >= HID_KBD_USAGE_A) && (key_code <= HID_KBD_USAGE_QUESTION)) { if ((key_code >= HID_KBD_USAGE_A) && (key_code <= HID_KBD_USAGE_QUESTION)) {
*key_char = keycode2ascii[key_code][mod]; *key_char = keycode2ascii[key_code][mod];
} else { } else {
@@ -233,6 +271,20 @@ static void usbh_hid_keyboard_report_callback(void *arg, int nbytes)
static uint8_t prev_keys[sizeof(kb_report->key)] = { 0 }; static uint8_t prev_keys[sizeof(kb_report->key)] = { 0 };
key_event_t key_event; key_event_t key_event;
#if KEYBOARD_ENTER_ALT_ESCAPE
if (hid_keyboard_is_modifier_alt(kb_report->modifier)) {
if (escaping == false) {
escaping = true;
escap_hex = 0;
}
} else {
if (escaping && escap_hex > 0) {
escaping = false;
hid_keyboard_print_char(escap_hex);
}
}
#endif
for (int i = 0; i < sizeof(kb_report->key); i++) { for (int i = 0; i < sizeof(kb_report->key); i++) {
// key has been released verification // key has been released verification

View File

@@ -83,6 +83,13 @@ typedef struct {
#define KEYBOARD_ENTER_MAIN_CHAR '\r' #define KEYBOARD_ENTER_MAIN_CHAR '\r'
/* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */ /* When set to 1 pressing ENTER will be extending with LineFeed during serial debug output */
#define KEYBOARD_ENTER_LF_EXTEND 1 #define KEYBOARD_ENTER_LF_EXTEND 1
/* When set to 1, numbers entered from the numeric keypad while ALT is pressed will be escaped */
#define KEYBOARD_ENTER_ALT_ESCAPE 1
#if KEYBOARD_ENTER_ALT_ESCAPE
static bool escaping = false;
static unsigned char escap_hex = 0;
#endif
/** /**
* @brief Scancode to ascii table * @brief Scancode to ascii table
@@ -187,6 +194,25 @@ static inline bool hid_keyboard_is_modifier_shift(uint8_t modifier)
return false; return false;
} }
#if KEYBOARD_ENTER_ALT_ESCAPE
/**
* @brief HID Keyboard modifier verification for capitalization application (right or left alt)
*
* @param[in] modifier
* @return true Modifier was pressed (left or right alt)
* @return false Modifier was not pressed (left or right alt)
*
*/
static inline bool hid_keyboard_is_modifier_alt(uint8_t modifier)
{
if (((modifier & HID_LEFT_ALT) == HID_LEFT_ALT) ||
((modifier & HID_RIGHT_ALT) == HID_RIGHT_ALT)) {
return true;
}
return false;
}
#endif
/** /**
* @brief HID Keyboard get char symbol from key code * @brief HID Keyboard get char symbol from key code
* *
@@ -203,6 +229,18 @@ static inline bool hid_keyboard_get_char(uint8_t modifier,
{ {
uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0; uint8_t mod = (hid_keyboard_is_modifier_shift(modifier)) ? 1 : 0;
#if KEYBOARD_ENTER_ALT_ESCAPE
if (escaping) {
if ((key_code >= HID_KEY_KEYPAD_1) && (key_code <= HID_KEY_KEYPAD_0)) {
if (key_code == HID_KEY_KEYPAD_0) {
key_code = HID_KEY_KEYPAD_1 - 1;
}
escap_hex = escap_hex * 10 + (key_code - (HID_KEY_KEYPAD_1 - 1));
}
return false;
}
#endif
if ((key_code >= HID_KEY_A) && (key_code <= HID_KEY_SLASH)) { if ((key_code >= HID_KEY_A) && (key_code <= HID_KEY_SLASH)) {
*key_char = keycode2ascii[key_code][mod]; *key_char = keycode2ascii[key_code][mod];
} else { } else {
@@ -289,6 +327,20 @@ static void hid_host_keyboard_report_callback(const uint8_t *const data, const i
static uint8_t prev_keys[HID_KEYBOARD_KEY_MAX] = { 0 }; static uint8_t prev_keys[HID_KEYBOARD_KEY_MAX] = { 0 };
key_event_t key_event; key_event_t key_event;
#if KEYBOARD_ENTER_ALT_ESCAPE
if (hid_keyboard_is_modifier_alt(kb_report->modifier.val)) {
if (escaping == false) {
escaping = true;
escap_hex = 0;
}
} else {
if (escaping && escap_hex > 0) {
escaping = false;
hid_keyboard_print_char(escap_hex);
}
}
#endif
for (int i = 0; i < HID_KEYBOARD_KEY_MAX; i++) { for (int i = 0; i < HID_KEYBOARD_KEY_MAX; i++) {
// key has been released verification // key has been released verification