1515#include " types/TouchMessageType.hpp"
1616#include " utils.hpp"
1717
18- namespace BI ::platform
18+ namespace BI
1919{
20- inline bool keyDown (PlatformKey key, NSEvent * event)
20+ namespace platform
2121 {
22- switch ( key)
22+ inline bool keyDown (PlatformKey key, NSEvent * event )
2323 {
24- case BI::PlatformKey::LEFT_CONTROL:
25- return [event modifierFlags ] & NSCommandKeyMask ;
26- case BI::PlatformKey::LEFT_SHIFT:
27- return [event modifierFlags ] & NSShiftKeyMask ;
24+ switch (key)
25+ {
26+ case BI::PlatformKey::LEFT_CONTROL:
27+ return [event modifierFlags ] & NSCommandKeyMask ;
28+ case BI::PlatformKey::LEFT_SHIFT:
29+ return [event modifierFlags ] & NSShiftKeyMask ;
30+ case BI::PlatformKey::LEFT_ALT:
31+ return [event modifierFlags ] & NSAlternateKeyMask ;
32+ }
33+
34+ return false ;
2835 }
36+ }
37+
38+ namespace cocos
39+ {
40+ inline cocos2d::CCPoint getMousePosition (NSEvent * event)
41+ {
42+ auto windowFrame = [[event window ] frame ];
43+ auto viewFrame = [[[event window ] contentView ] frame ];
44+ auto winSize = cocos2d::CCDirector::get ()->getWinSize ();
45+ auto scaleFactor = cocos2d::CCPoint (winSize) / ccp (viewFrame.size .width , viewFrame.size .height );
46+ auto mouse = [event locationInView ];
2947
30- return false ;
48+ return ccp (mouse.x - windowFrame.origin .x , winSize.height - (mouse.y - windowFrame.origin .y )) * scaleFactor;
49+ }
3150 }
3251}
3352
3453#define HOOK_OBJC_METHOD (klass, type, cleanFuncName, funcName ) \
3554 do { \
36- auto cleanFuncName ## Method = class_getInstanceMethod (klass, @selector (funcName )); \
55+ auto cleanFuncName ## Method = class_getInstanceMethod (objc_getClass ( klass) , @selector (funcName )); \
3756 cleanFuncName ## OIMP = reinterpret_cast <type>(method_getImplementation (cleanFuncName ## Method )); \
3857 method_setImplementation (cleanFuncName ## Method , reinterpret_cast <IMP >(&cleanFuncName)); \
39- geode::log::debug (" Hooked Objective C Method '{}' " , # funcName); \
58+ geode::log::debug (" Hooked Objective C Method '" #klass " " # funcName " ' " ); \
4059 } while (0 )
4160
4261using key_event_t = void (*)(EAGLView*, SEL , NSEvent *);
4362
44-
4563static key_event_t keyDownExecOIMP;
4664void keyDownExec (EAGLView* self, SEL sel, NSEvent * event)
4765{
@@ -71,13 +89,13 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event)
7189 {
7290 case kVK_RightArrow :
7391 return g_selectedInput->onRightArrowKey (
74- BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL , event),
92+ BI::platform::keyDown (BI::PlatformKey::LEFT_ALT , event),
7593 BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
7694 );
7795
7896 case kVK_LeftArrow :
7997 return g_selectedInput->onLeftArrowKey (
80- BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL , event),
98+ BI::platform::keyDown (BI::PlatformKey::LEFT_ALT , event),
8199 BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
82100 );
83101
@@ -91,6 +109,7 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event)
91109 !BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
92110 ) {
93111 // https://github.com/WebKit/WebKit/blob/5c8281f146cfbf4b6189b435b80c527f138b829f/Source/WebCore/platform/mac/PlatformEventFactoryMac.mm#L559
112+ // we use this instead of [event keyCode] because the returned value of keyCode for letters is keyboard locale-specific
94113 int code = [[event characters ] length ] > 0
95114 ? [[event characters ] characterAtIndex: 0 ]
96115 : [[event charactersIgnoringModifiers ] length ] > 0
@@ -147,6 +166,21 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event)
147166 break ;
148167 }
149168 }
169+
170+ if (BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event))
171+ {
172+ switch ([event keyCode ])
173+ {
174+ case kVK_LeftArrow :
175+ return g_selectedInput->onHomeKey (false );
176+
177+ case kVK_RightArrow :
178+ return g_selectedInput->onEndKey (false );
179+
180+ default :
181+ break ;
182+ }
183+ }
150184 }
151185
152186 // key is probably a regular character, allow CCIMEDispatcher to pick up the event
@@ -169,13 +203,12 @@ void mouseDownExec(EAGLView* self, SEL sel, NSEvent* event)
169203 if (!g_selectedInput)
170204 return mouseDownExecOIMP (self, sel, event);
171205
172- cocos2d::CCSize winSize = cocos2d::CCDirector::sharedDirector ()->getWinSize ();
173- cocos2d::CCPoint mousePos = BI::cocos::getMousePosition ();
206+ cocos2d::CCPoint mousePos = BI::cocos::getMousePosition (event);
174207
175208 // NSWindow's mouse origin is the bottom left
176209 // CCTouch's mouse origin is top left (because of course it is)
177210 cocos2d::CCTouch touch{};
178- touch.setTouchInfo (0 , mousePos.x , winSize. height - mousePos.y );
211+ touch.setTouchInfo (0 , mousePos.x , mousePos.y );
179212
180213 g_selectedInput->useUpdateBlinkPos (true );
181214
@@ -196,11 +229,9 @@ void mouseUpExec(EAGLView* self, SEL sel, NSEvent* event)
196229// https://github.com/qimiko/click-on-steps/blob/d8a87e93b5407e5f2113a9715363a5255724c901/src/macos.mm#L101
197230$on_mod(Loaded)
198231{
199- auto eaglView = objc_getClass (" EAGLView" );
200-
201- HOOK_OBJC_METHOD (eaglView, key_event_t , keyDownExec, keyDownExec:);
202- HOOK_OBJC_METHOD (eaglView, key_event_t , keyUpExec, keyUpExec:);
232+ HOOK_OBJC_METHOD (EAGLView, key_event_t , keyDownExec, keyDownExec:);
233+ HOOK_OBJC_METHOD (EAGLView, key_event_t , keyUpExec, keyUpExec:);
203234
204- HOOK_OBJC_METHOD (eaglView , key_event_t , mouseDownExec, mouseDownExec:);
205- HOOK_OBJC_METHOD (eaglView , key_event_t , mouseUpExec, mouseUpExec:);
235+ HOOK_OBJC_METHOD (EAGLView , key_event_t , mouseDownExec, mouseDownExec:);
236+ HOOK_OBJC_METHOD (EAGLView , key_event_t , mouseUpExec, mouseUpExec:);
206237}
0 commit comments