Skip to content

Commit

Permalink
fix: usb hid modifier key won't trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
DriftKingTW committed Feb 4, 2024
1 parent 9a4184a commit 2f71ede
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
80 changes: 76 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,78 @@ void updateKeymaps() {
configUpdated = true;
}

/**
* Press USB HID keys. (Fix for modifier keys)
*
* @param {uint8_t} keyStroke the key stroke to be pressed
*/
void usbKeyboardPress(uint8_t keyStroke) {
switch (keyStroke) {
case 128: // KEY_LEFT_CTRL
usbKeyboard.pressRaw(0xe0);
break;
case 129: // KEY_LEFT_SHIFT
usbKeyboard.pressRaw(0xe1);
break;
case 130: // KEY_LEFT_ALT
usbKeyboard.pressRaw(0xe2);
break;
case 131: // KEY_LEFT_GUI
usbKeyboard.pressRaw(0xe3);
break;
case 132: // KEY_RIGHT_CTRL
usbKeyboard.pressRaw(0xe4);
break;
case 133: // KEY_RIGHT_SHIFT
usbKeyboard.pressRaw(0xe5);
break;
case 134: // KEY_RIGHT_ALT
usbKeyboard.pressRaw(0xe6);
break;
case 135: // KEY_RIGHT_GUI
usbKeyboard.pressRaw(0xe7);
break;
default:
usbKeyboard.press(keyStroke);
}
}

/**
* Release USB HID keys. (Fix for modifier keys)
*
* @param {uint8_t} keyStroke the key stroke to be pressed
*/
void usbKeyboardRelease(uint8_t keyStroke) {
switch (keyStroke) {
case 128: // KEY_LEFT_CTRL
usbKeyboard.releaseRaw(0xe0);
break;
case 129: // KEY_LEFT_SHIFT
usbKeyboard.releaseRaw(0xe1);
break;
case 130: // KEY_LEFT_ALT
usbKeyboard.releaseRaw(0xe2);
break;
case 131: // KEY_LEFT_GUI
usbKeyboard.releaseRaw(0xe3);
break;
case 132: // KEY_RIGHT_CTRL
usbKeyboard.releaseRaw(0xe4);
break;
case 133: // KEY_RIGHT_SHIFT
usbKeyboard.releaseRaw(0xe5);
break;
case 134: // KEY_RIGHT_ALT
usbKeyboard.releaseRaw(0xe6);
break;
case 135: // KEY_RIGHT_GUI
usbKeyboard.releaseRaw(0xe7);
break;
default:
usbKeyboard.release(keyStroke);
}
}

/**
* Press key
*
Expand All @@ -1111,7 +1183,7 @@ void keyPress(Key &key) {
}
if (key.state == false && !isOutputLocked) {
if (isUsbMode) {
usbKeyboard.press(key.keyStroke);
usbKeyboardPress(key.keyStroke);
} else {
bleKeyboard.press(key.keyStroke);
}
Expand All @@ -1127,7 +1199,7 @@ bool keyPress(uint8_t keyStroke, String keyInfo, bool keyState) {
}
if (keyState == false && !isOutputLocked) {
if (isUsbMode) {
usbKeyboard.press(keyStroke);
usbKeyboardPress(keyStroke);
} else {
bleKeyboard.press(keyStroke);
}
Expand All @@ -1149,7 +1221,7 @@ void keyRelease(Key &key) {
}
if (key.state == true && !isOutputLocked) {
if (isUsbMode) {
usbKeyboard.release(key.keyStroke);
usbKeyboardRelease(key.keyStroke);
} else {
bleKeyboard.release(key.keyStroke);
}
Expand All @@ -1164,7 +1236,7 @@ bool keyRelease(uint8_t keyStroke, String keyInfo, bool keyState) {
}
if (keyState == true && !isOutputLocked) {
if (isUsbMode) {
usbKeyboard.release(keyStroke);
usbKeyboardRelease(keyStroke);
} else {
bleKeyboard.release(keyStroke);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void i2cTask(void *);
void initKeys();
void initMacros();
void updateKeymaps();
void usbKeyboardPress(uint8_t keyStroke);
void usbKeyboardRelease(uint8_t keyStroke);
void keyPress(Key &key);
bool keyPress(uint8_t keyStroke, String keyInfo, bool keyState);
void keyRelease(Key &key);
Expand Down

0 comments on commit 2f71ede

Please sign in to comment.