fix keys SHIFT, CTRL and ALT on MacOS platform (#3587)

* fix keys SHIFT, CTRL and ALT on MacOS platform

* remove header
This commit is contained in:
PP
2021-06-02 09:40:41 +08:00
committed by GitHub
parent 5f8c114818
commit 441e739c8e
3 changed files with 86 additions and 0 deletions
@@ -158,6 +158,14 @@
#define GLFW_KEY_LAST GLFW_KEY_MENU
namespace {
// Modifier Key State
bool stateShiftLeft = false;
bool stateShiftRight = false;
bool stateControlLeft = false;
bool stateControlRight = false;
bool stateOptionLeft = false;
bool stateOptionRight = false;
// Refer to https://github.com/glfw/glfw/blob/master/src/cocoa_window.m.
int keyCodes[0xff + 1] = {-1};
@@ -279,6 +287,63 @@ void init() {
}
} // namespace
void updateModifierKeyState (int keyCodeInWeb) {
if (keyCodeInWeb == 16) { // shift left
stateShiftLeft = !stateShiftLeft;
} else if (keyCodeInWeb == 20016) { // shift right
stateShiftRight = !stateShiftRight;
} else if (keyCodeInWeb == 17) { // ctrl left
stateControlLeft = !stateControlLeft;
} else if (keyCodeInWeb == 20017) { // ctrl right
stateControlRight = !stateControlRight;
} else if (keyCodeInWeb == 18) { // alt left
stateOptionLeft = !stateOptionLeft;
} else if (keyCodeInWeb == 20018) { // alt right
stateOptionRight = !stateOptionRight;
}
}
cc::KeyboardEvent::Action getModifierKeyAction (int keyCodeInWeb) {
if (keyCodeInWeb == 16) { // shift left
if (stateShiftLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20016) { // shift right
if (stateShiftRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 17) { // ctrl left
if (stateControlLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20017) { // ctrl right
if (stateControlRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 18) { // alt left
if (stateOptionLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20018) { // alt right
if (stateOptionRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
}
return cc::KeyboardEvent::Action::UNKNOWN;
}
// Refer to: https://github.com/cocos-creator/cocos2d-x-lite/blob/v2.4.0/cocos/platform/desktop/CCGLView-desktop.cpp.
int translateKeycode(int keyCode) {
std::once_flag flag;