mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
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:
@@ -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;
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#import "cocos/bindings/event/EventDispatcher.h"
|
||||
#pragma once
|
||||
|
||||
extern int translateKeycode(int);
|
||||
extern void updateModifierKeyState (int keyCodeInWeb);
|
||||
extern cc::KeyboardEvent::Action getModifierKeyAction (int keyCodeInWeb);
|
||||
|
||||
@@ -124,6 +124,24 @@
|
||||
cc::EventDispatcher::dispatchKeyboardEvent(_keyboardEvent);
|
||||
}
|
||||
|
||||
- (void)flagsChanged:(NSEvent *)event {
|
||||
int keyCode = translateKeycode(event.keyCode);
|
||||
updateModifierKeyState(keyCode);
|
||||
auto action = getModifierKeyAction(keyCode);
|
||||
|
||||
// NOTE: in some cases, flagsChanged event may return some wrong keyCodes
|
||||
// For example:
|
||||
// - when you long press the capslock key, you may get the keyCode -1
|
||||
// - when you press ctrl + space, you may get the keyCode 65
|
||||
if (action == cc::KeyboardEvent::Action::UNKNOWN) {
|
||||
return;
|
||||
}
|
||||
_keyboardEvent.key = keyCode;
|
||||
_keyboardEvent.action = action;
|
||||
[self setModifierFlags:event];
|
||||
cc::EventDispatcher::dispatchKeyboardEvent(_keyboardEvent);
|
||||
}
|
||||
|
||||
- (void)setModifierFlags:(NSEvent *)event {
|
||||
NSEventModifierFlags modifierFlags = event.modifierFlags;
|
||||
if (modifierFlags & NSEventModifierFlagShift)
|
||||
|
||||
Reference in New Issue
Block a user