From 441e739c8e505776281e090f873221e50858f2d6 Mon Sep 17 00:00:00 2001 From: PP Date: Wed, 2 Jun 2021 09:40:41 +0800 Subject: [PATCH] fix keys SHIFT, CTRL and ALT on MacOS platform (#3587) * fix keys SHIFT, CTRL and ALT on MacOS platform * remove header --- native/cocos/platform/mac/KeyCodeHelper.cpp | 65 +++++++++++++++++++++ native/cocos/platform/mac/KeyCodeHelper.h | 3 + native/cocos/platform/mac/View.mm | 18 ++++++ 3 files changed, 86 insertions(+) diff --git a/native/cocos/platform/mac/KeyCodeHelper.cpp b/native/cocos/platform/mac/KeyCodeHelper.cpp index 0a5add1c34..5b8dd6e45a 100644 --- a/native/cocos/platform/mac/KeyCodeHelper.cpp +++ b/native/cocos/platform/mac/KeyCodeHelper.cpp @@ -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; diff --git a/native/cocos/platform/mac/KeyCodeHelper.h b/native/cocos/platform/mac/KeyCodeHelper.h index 2dca010a66..cfee0c5a09 100644 --- a/native/cocos/platform/mac/KeyCodeHelper.h +++ b/native/cocos/platform/mac/KeyCodeHelper.h @@ -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); diff --git a/native/cocos/platform/mac/View.mm b/native/cocos/platform/mac/View.mm index 863930bf64..8280a9f797 100644 --- a/native/cocos/platform/mac/View.mm +++ b/native/cocos/platform/mac/View.mm @@ -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)