88#include < Geode/modify/CCEGLView.hpp>
99#include < Geode/cocos/robtop/glfw/glfw3.h>
1010#elif defined(GEODE_IS_MACOS)
11- #include < Geode/modify/CCKeyboardDispatcher.hpp>
12- #include < Geode/modify/CCTouchDispatcher.hpp>
11+ #define CommentType CommentTypeDummy
12+ #import < Foundation/Foundation.h>
13+ #import < Cocoa/Cocoa.h>
14+ #import < CoreGraphics/CoreGraphics.h>
15+ #undef CommentType
16+
17+ #include < Geode/cocos/platform/mac/CCEventDispatcher.h>
18+ #import < Geode/cocos/platform/mac/EAGLView.h>
19+ #import < objc/runtime.h>
1320
1421#include " types/TouchMessageType.hpp"
1522#endif
@@ -1427,72 +1434,121 @@ struct BetterCCEGLView : Modify<BetterCCEGLView, CCEGLView>
14271434
14281435#elif defined(GEODE_IS_MACOS)
14291436
1430- // handles ctrl and shift
1431- struct ModifierKeysState
1432- {
1433- bool ctrlDown;
1434- bool shiftDown;
1435- };
1437+ #define HOOK_OBJC_METHOD (klass, cleanFuncName, funcName ) \
1438+ auto cleanFuncName ## Method = class_getInstanceMethod(objc_getClass(#klass), @selector(funcName)); \
1439+ cleanFuncName ## OIMP = method_getImplementation(cleanFuncName ## Method); \
1440+ method_setImplementation (cleanFuncName ## Method, (EventType<klass>)&funcName);
14361441
1437- static ModifierKeysState g_modifier_keys_state;
1442+ # define CALL_OIMP ( funcName ) reinterpret_cast < decltype (&funcName)>(funcName ## OIMP)(self, sel, event)
14381443
1439- // doesnt work. lol!
1440- struct BetterKeyboardDispatcher : Modify<BetterKeyboardDispatcher, CCKeyboardDispatcher>
1441- {
1442- bool dispatchKeyboardMSG (cocos2d::enumKeyCodes key, bool isKeyDown, bool isKeyRepeat)
1443- {
1444- if (!g_selectedInput)
1445- return CCKeyboardDispatcher::dispatchKeyboardMSG (key, isKeyDown, isKeyRepeat);
1444+ template <typename T>
1445+ using EventType = void (*)(T*, SEL, NSEvent*);
14461446
1447- if (key == enumKeyCodes::KEY_LeftControl)
1447+
1448+ static EventType<EAGLView> keyDownExecOIMP;
1449+ void keyDownExec (EAGLView* self, SEL sel, NSEvent* event) {
1450+ if (!g_selectedInput)
1451+ CALL_OIMP (keyDownExec);
1452+
1453+ // on click, can be held
1454+ if (
1455+ !BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event) &&
1456+ !BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
1457+ ) {
1458+ switch ([event keyCode])
14481459 {
1449- if (isKeyDown || isKeyRepeat)
1450- g_modifier_keys_state.ctrlDown = true ;
1451- else
1452- g_modifier_keys_state.ctrlDown = false ;
1460+ case kVK_Escape :
1461+ g_selectedInput->deselectInput ();
1462+ break ;
1463+
1464+ case kVK_Delete :
1465+ case kVK_ForwardDelete :
1466+ g_selectedInput->onDelete (false , [event keyCode] == kVK_ForwardDelete );
1467+ break ;
1468+
1469+ default :
1470+ break ;
14531471 }
1472+ }
1473+
1474+ switch ([event keyCode])
1475+ {
1476+ case kVK_RightArrow :
1477+ g_selectedInput->onRightArrowKey (
1478+ BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event),
1479+ BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
1480+ );
1481+ break ;
1482+
1483+ case kVK_LeftArrow :
1484+ g_selectedInput->onLeftArrowKey (
1485+ BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event),
1486+ BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
1487+ );
1488+ break ;
14541489
1455- if (key == enumKeyCodes::KEY_LeftShift)
1490+ default :
1491+ break ;
1492+ }
1493+
1494+ if (
1495+ ![event isARepeat] &&
1496+ !BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event) &&
1497+ BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
1498+ ) {
1499+ int code = [[event characters] length] > 0
1500+ ? [[event characters] characterAtIndex:0 ];
1501+ : [[event charactersIgnoringModifiers] characterAtIndex:0 ];
1502+
1503+ switch ([event keyCode])
14561504 {
1457- if (isKeyDown || isKeyRepeat)
1458- g_modifier_keys_state.shiftDown = true ;
1459- else
1460- g_modifier_keys_state.shiftDown = false ;
1505+ case kVK_Delete :
1506+ case kVK_ForwardDelete :
1507+ g_selectedInput->onDelete (true , [event keyCode] == kVK_ForwardDelete );
1508+ break ;
1509+
1510+ default :
1511+ break ;
14611512 }
14621513
1463- if (isKeyDown || isKeyRepeat )
1514+ switch (code )
14641515 {
1465- if (!g_modifier_keys_state.ctrlDown && !g_modifier_keys_state.shiftDown )
1466- {
1467- switch (key)
1468- {
1469- case enumKeyCodes::KEY_Escape:
1470- g_selectedInput->deselectInput ();
1471- break ;
1516+ case ' a' : case ' A' :
1517+ g_selectedInput->highlightFromToPos (0 , -1 );
1518+ break ;
14721519
1473- case enumKeyCodes::KEY_Backspace:
1474- case enumKeyCodes::KEY_Delete:
1475- g_selectedInput->onDelete (false , key == enumKeyCodes::KEY_Delete);
1476- break ;
1520+ case ' c' : case ' C' :
1521+ g_selectedInput->onCopy ();
1522+ break ;
14771523
1478- default :
1479- break ;
1480- }
1481- }
1524+ case ' v' : case ' V' :
1525+ g_selectedInput->onPaste ();
1526+ break ;
14821527
1483- switch (key)
1528+ case ' x' : case ' X' :
1529+ g_selectedInput->onCut ();
1530+ break ;
1531+
1532+ default :
1533+ break ;
1534+ }
1535+ }
1536+
1537+ if (![event isARepeat])
1538+ {
1539+ if (!BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event))
1540+ {
1541+ switch ([event keyCode])
14841542 {
1485- case enumKeyCodes::KEY_Right:
1486- g_selectedInput->onRightArrowKey (
1487- g_modifier_keys_state.ctrlDown ,
1488- g_modifier_keys_state.shiftDown
1543+ case kVK_Home :
1544+ g_selectedInput->onHomeKey (
1545+ BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event)
14891546 );
14901547 break ;
14911548
1492- case enumKeyCodes::KEY_Left:
1493- g_selectedInput->onLeftArrowKey (
1494- g_modifier_keys_state.ctrlDown ,
1495- g_modifier_keys_state.shiftDown
1549+ case kVK_End :
1550+ g_selectedInput->onEndKey (
1551+ BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT)
14961552 );
14971553 break ;
14981554
@@ -1502,80 +1558,23 @@ struct BetterKeyboardDispatcher : Modify<BetterKeyboardDispatcher, CCKeyboardDis
15021558 }
15031559
15041560 if (
1505- isKeyDown &&
1506- g_modifier_keys_state. ctrlDown &&
1507- !g_modifier_keys_state. shiftDown
1561+ ! BI::platform::keyDown (BI::PlatformKey::LEFT_SHIFT, event) &&
1562+ ! BI::platform::keyDown (BI::PlatformKey::LEFT_CONTROL, event) &&
1563+ [event keyCode] == kVK_Return
15081564 ) {
1509- switch (key)
1510- {
1511- case enumKeyCodes::KEY_A:
1512- g_selectedInput->highlightFromToPos (0 , -1 );
1513- break ;
1514-
1515- case enumKeyCodes::KEY_Insert:
1516- case enumKeyCodes::KEY_C:
1517- g_selectedInput->onCopy ();
1518- break ;
1519-
1520- case enumKeyCodes::KEY_V:
1521- g_selectedInput->onPaste ();
1522- break ;
1523-
1524- case enumKeyCodes::KEY_X:
1525- g_selectedInput->onCut ();
1526- break ;
1527-
1528- case enumKeyCodes::KEY_Backspace:
1529- case enumKeyCodes::KEY_Delete:
1530- g_selectedInput->onDelete (true , key == enumKeyCodes::KEY_Delete);
1531- break ;
1532-
1533- default :
1534- break ;
1535- }
1565+ CALL_OIMP (keyDownExec);
15361566 }
1567+ }
1568+ }
15371569
1538- if (isKeyDown)
1539- {
1540- if (!g_modifier_keys_state.ctrlDown )
1541- {
1542- switch (key)
1543- {
1544- case enumKeyCodes::KEY_Home:
1545- g_selectedInput->onHomeKey (
1546- g_modifier_keys_state.shiftDown
1547- );
1548- break ;
1549-
1550- case enumKeyCodes::KEY_End:
1551- g_selectedInput->onEndKey (
1552- g_modifier_keys_state.ctrlDown
1553- );
1554- break ;
1555-
1556- default :
1557- break ;
1558- }
1559- }
1560-
1561- if (g_modifier_keys_state.shiftDown && !g_modifier_keys_state.ctrlDown )
1562- {
1563- switch (key)
1564- {
1565- case enumKeyCodes::KEY_Insert:
1566- g_selectedInput->onPaste ();
1567- break ;
1568-
1569- default :
1570- break ;
1571- }
1572- }
1573- }
1570+ static EventType<EAGLView> keyUpExecOIMP;
1571+ void keyUpExec (EAGLView* self, SEL sel, NSEvent* event) {
1572+ if (!g_selectedInput)
1573+ CALL_OIMP (keyUpExec);
1574+ }
15741575
1575- return false ;
1576- }
1577- };
15781576
1577+ // TODO: move to hooking mouseDownExec
15791578// handles mouse clicks
15801579struct BetterTouchDispatcher : Modify<BetterTouchDispatcher, CCTouchDispatcher>
15811580{
@@ -1606,4 +1605,11 @@ struct BetterTouchDispatcher : Modify<BetterTouchDispatcher, CCTouchDispatcher>
16061605 }
16071606};
16081607
1608+
1609+ $on_mod(Loaded)
1610+ {
1611+ HOOK_OBJC_METHOD (EAGLView, keyDownExec, keyDownExec:);
1612+ HOOK_OBJC_METHOD (EAGLView, keyUpExec, keyUpExec:);
1613+ }
1614+
16091615#endif
0 commit comments