Skip to content

Commit 6093ca6

Browse files
committed
faet(mac): Add some MacOS keybinds
1 parent 76b19ae commit 6093ca6

File tree

4 files changed

+70
-28
lines changed

4 files changed

+70
-28
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [v4.2.0-beta.12] - 2024-12-04
9+
10+
### Added
11+
12+
- MacOS keybinds: `Command + Left/Right Arrow`, `Option + Left/Right Arrow`
13+
- `ALT`/`Option` key detection
14+
815
## [v4.2.0-beta.11] - 2024-12-01
916

1017
### Changed

mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"win": "2.2074",
55
"mac": "2.2074"
66
},
7-
"version": "v4.2.0-beta.11",
7+
"version": "v4.2.0-beta.12",
88
"id": "spaghettdev.betterinputs",
99
"name": "BetterInputs",
1010
"developer": "SpaghettDev",

src/macos.mm

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,51 @@
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

4261
using key_event_t = void(*)(EAGLView*, SEL, NSEvent*);
4362

44-
4563
static key_event_t keyDownExecOIMP;
4664
void 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
}

src/utils.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,18 @@ namespace BI
9999

100100
namespace cocos
101101
{
102+
#ifdef GEODE_IS_WINDOWS
102103
inline cocos2d::CCPoint getMousePosition()
103104
{
104-
#ifdef GEODE_IS_WINDOWS
105105
auto* director = cocos2d::CCDirector::sharedDirector();
106106
auto* gl = director->getOpenGLView();
107107
auto winSize = director->getWinSize();
108108
auto frameSize = gl->getFrameSize();
109109
auto mouse = gl->getMousePosition() / frameSize;
110110

111111
return cocos2d::CCPoint{ mouse.x, 1.f - mouse.y } * winSize;
112-
#elif defined(GEODE_IS_MACOS)
113-
return geode::cocos::getMousePos();
114-
#endif
115112
}
113+
#endif
116114

117115
inline bool isPositionInNode(cocos2d::CCNode* node, const cocos2d::CCPoint& pos)
118116
{
@@ -132,8 +130,12 @@ namespace BI
132130

133131
enum class PlatformKey
134132
{
133+
// Windows Control key | MacOS Command key
135134
LEFT_CONTROL,
136-
LEFT_SHIFT
135+
// Shift key
136+
LEFT_SHIFT,
137+
// Window Alt key | MacOS Option key
138+
LEFT_ALT
137139
};
138140
namespace platform
139141
{
@@ -146,6 +148,8 @@ namespace BI
146148
return GetKeyState(VK_CONTROL) & 0x8000;
147149
case BI::PlatformKey::LEFT_SHIFT:
148150
return GetKeyState(VK_SHIFT) & 0x8000;
151+
case BI::PlatformKey::LEFT_ALT:
152+
return GetKeyState(VK_LMENU) & 0x8000;
149153
}
150154

151155
return false;

0 commit comments

Comments
 (0)