Skip to content

Commit 6af1a02

Browse files
committed
action map remap for mac
Mac now automatically remaps keys and modifies the incoming string to reflect it automatically converts: Ctrl -> Cmd Alt -> Option if you absolutely need a specific set on macos you will still need to check the platform in script and set the required key otherwise this will allow menus to be built with just providing the windows key or the mac key and it will display correclty for the platform.
1 parent 5dd01fc commit 6af1a02

3 files changed

Lines changed: 89 additions & 27 deletions

File tree

Engine/source/platformSDL/sdlInput.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ void Input::init()
7777
smLastJoystickActivated = true;
7878

7979
SDL_InitSubSystem( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS );
80+
81+
#ifdef TORQUE_OS_MAC
82+
// Disable Ctrl+Click being treated as right-click
83+
SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, "0");
84+
85+
// Optionally, handle function keys as standard F1–F12
86+
SDL_SetHint(SDL_HINT_MAC_BACKGROUND_APP, "0");
87+
#endif
8088

8189
// Init the current modifier keys
8290
setModifierKeys(0);

Engine/source/platformSDL/sdlPlatformGL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace PlatformGL
1919
#ifdef TORQUE_DEBUG
2020
debugFlag |= SDL_GL_CONTEXT_DEBUG_FLAG;
2121
#endif
22+
23+
#if SDL_VERSION_ATLEAST(2, 24, 0)
24+
SDL_SetHint(SDL_HINT_MAC_OPENGL_ASYNC_DISPATCH, "1");
25+
#endif
2226
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
2327
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, debugFlag);
2428
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);

Engine/source/sim/actionMap.cpp

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -466,23 +466,41 @@ bool ActionMap::createEventDescriptor(const char* pEventString, EventDescriptor*
466466
pObjectString = pSpace + 1;
467467
pSpace[0] = '\0';
468468

469-
char* pModifier = dStrtok(copyBuffer, "-");
470-
while (pModifier != NULL) {
471-
if (dStricmp(pModifier, "shift") == 0) {
472-
pDescriptor->flags |= SI_SHIFT;
473-
} else if (dStricmp(pModifier, "ctrl") == 0) {
474-
pDescriptor->flags |= SI_CTRL;
475-
} else if (dStricmp(pModifier, "alt") == 0) {
476-
pDescriptor->flags |= SI_ALT;
477-
} else if (dStricmp(pModifier, "cmd") == 0) {
478-
pDescriptor->flags |= SI_ALT;
479-
} else if (dStricmp(pModifier, "opt") == 0) {
480-
pDescriptor->flags |= SI_MAC_OPT;
481-
}
482-
483-
pModifier = dStrtok(NULL, "-");
484-
}
485-
} else {
469+
char* pModifier = dStrtok(copyBuffer, "-");
470+
while (pModifier != NULL)
471+
{
472+
#if defined(TORQUE_OS_MAC)
473+
if (dStricmp(pModifier, "ctrl") == 0 || dStricmp(pModifier, "cmd") == 0)
474+
{
475+
pDescriptor->flags |= SI_CTRL; // On Mac, map Ctrl/Cmd to the standard Mac command key
476+
}
477+
else if (dStricmp(pModifier, "shift") == 0)
478+
{
479+
pDescriptor->flags |= SI_SHIFT;
480+
}
481+
else if (dStricmp(pModifier, "alt") == 0 || dStricmp(pModifier, "opt") == 0)
482+
{
483+
pDescriptor->flags |= SI_MAC_OPT;
484+
}
485+
#else
486+
if (dStricmp(pModifier, "ctrl") == 0 || (dStricmp(pModifier, "cmd") == 0)
487+
{
488+
pDescriptor->flags |= SI_CTRL;
489+
}
490+
else if (dStricmp(pModifier, "shift") == 0)
491+
{
492+
pDescriptor->flags |= SI_SHIFT;
493+
}
494+
else if (dStricmp(pModifier, "alt") == 0 || dStricmp(pModifier, "opt") == 0)
495+
{
496+
pDescriptor->flags |= SI_ALT;
497+
}
498+
#endif
499+
pModifier = dStrtok(NULL, "-");
500+
}
501+
}
502+
else
503+
{
486504
// No.
487505
pDescriptor->flags = 0;
488506
pObjectString = copyBuffer;
@@ -492,6 +510,47 @@ bool ActionMap::createEventDescriptor(const char* pEventString, EventDescriptor*
492510
//
493511
AssertFatal(dStrlen(pObjectString) != 0, "Error, no key was specified!");
494512

513+
// rebuild normalized modifier string from parsed flags
514+
String newString;
515+
516+
#if defined(TORQUE_OS_MAC)
517+
518+
if (pDescriptor->flags & SI_ALT || pDescriptor->flags & SI_CTRL)
519+
newString += "Cmd";
520+
521+
if (pDescriptor->flags & SI_MAC_OPT)
522+
{
523+
if (newString.length()) newString += "-";
524+
newString += "Opt";
525+
}
526+
527+
#else
528+
529+
if (pDescriptor->flags & SI_CTRL)
530+
newString += "Ctrl";
531+
532+
if (pDescriptor->flags & SI_ALT)
533+
{
534+
if (newString.length()) newString += "-";
535+
newString += "Alt";
536+
}
537+
538+
#endif
539+
540+
if (pDescriptor->flags & SI_SHIFT)
541+
{
542+
if (newString.length()) newString += "-";
543+
newString += "Shift";
544+
}
545+
546+
// rebuild full event string
547+
if (newString.length())
548+
newString = String::ToString("%s %s", newString.c_str(), pObjectString).c_str();
549+
else
550+
newString = String::ToString("%s", pObjectString).c_str();
551+
552+
dStrcpy(const_cast<char*>(pEventString), newString.c_str(), newString.length()+1);
553+
495554
if (dStrlen(pObjectString) == 1)
496555
{
497556
if (dIsDecentChar(*pObjectString)) // includes foreign chars
@@ -544,16 +603,7 @@ bool ActionMap::createEventDescriptor(const char* pEventString, EventDescriptor*
544603
}
545604
}
546605
}
547-
// Didn't find an ascii match. Check the virtual map table
548-
//for (U32 j = 0; gVirtualMap[j].code != 0xFFFFFFFF; j++)
549-
//{
550-
// if (dStricmp(pObjectString, gVirtualMap[j].pDescription) == 0)
551-
// {
552-
// pDescriptor->eventType = gVirtualMap[j].type;
553-
// pDescriptor->eventCode = gVirtualMap[j].code;
554-
// return true;
555-
// }
556-
//}
606+
557607
InputEventManager::VirtualMapData* data = INPUTMGR->findVirtualMap(pObjectString);
558608
if(data)
559609
{

0 commit comments

Comments
 (0)