1

I am exporting a windows game on mac using unity 3D and I am encountering some issues with the key binding: CTRL, ALT, CMD and SHIFT key are not recognized at all (no errors).

I used the following code to see if anything was detected, and it worked for all keys except CTRL, ALT, CMD and SHIFT.

void OnGUI() 
    {
        Event e = Event.current;

            //vérifier si une touche est bien pressée
            if (e.isKey)
            {
                Debug.Log("Detected key code: " + e.keyCode);
                txt.text = e.keyCode.ToString();
                txt2.text = "normal";
            }

            if(e.modifiers == EventModifiers.Alt)
            {
                txt.text = e.keyCode.ToString();
                txt2.text = "modif";
            }
    }

Is there a way to make this work ? Thanks in advance.

  • 1
    cmd/alt/ctrl are key modifiers. Look for event modifier flags – Marek H Aug 02 '21 at 11:27
  • @MarekH I tried your idea, but still not recognized. I made a test code using: e.modifiers == EventModifiers.Alt just to see if alt was detected and no, it's not (it is on windows but not on macOS). – Infamous Jox Aug 02 '21 at 12:00
  • Are you pressing key together with modifier? Can you update the code how you check for modifiers? – Marek H Aug 02 '21 at 17:10
  • @MarekH no, only pressing one key... Updated the code. – Infamous Jox Aug 02 '21 at 18:23
  • I am not sure if event will be fired/delivered if you press only modifier key – Marek H Aug 02 '21 at 18:50
  • @MarekH well it does on windows… Is there any other way to detect those keys individually on Mac? – Infamous Jox Aug 03 '21 at 08:40
  • detect with flagsChanged https://stackoverflow.com/questions/4101669/get-modifierflags-in-keydown-event-without-pressing-non-modifier-key-along-with or override sendEvent: and check for modifierflags there. I understand this is Cocoa but it's a recipe for you what information to look for – Marek H Aug 03 '21 at 12:19

1 Answers1

0

Okay, after testing a few things, I think I noticed something. Modifier keys don’t seem to produce any keycode on macOs (with the previous method), but can be detected by searching the very same keycode.

The solution I ended up with is obviously not optimal, but works : I had to test every keycode of modifiers to see if they were pressed and manually assign them the corresponding keycode (yeah that’s dumb)…

This is a succession of the code below :

if(Input.GetKeyDown(Keycode.LeftAlt))
{
newKey = Keycode.LeftAlt;
}

I know this is really stupid to do it this way, but that is the only one that worked so far. Thanks for your help.