Keycode.Alpha not working for MacBook Pro?

Dears,
I am working on a new laptop 15" MacBook Pro and also using a separate apple keyboard with numpad when at home. I don’t get errors, but when using the Alpha1,2,3 as keycode, nothing happens. I also tried using the shift button with the alphanumeric keys. When using the keypad keycode, all works perfect. Also other Alpha keys like A,b, etc… work perfect.
Here’s my code

void Update () 
    {
        ManageState();
    }

    private void ManageState()
    {
        var nextStates = state.GetNextStates();

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            state = nextStates[0];
            print("You pressed 1");
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            state = nextStates[1];
            print("You pressed 2");
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            state = nextStates[2];
            print("You pressed wrong key");
        }

        textComponent.text = state.GetStateStory();
    }

Somebody has the same behaviour or a solutions to this? Should I go into the setup of the input of Unity and if yes, what do I do there?

Enjoy the New-Year full of good health and love!!

I don’t have an exact solution for you, but something you might try that will lead you in the right direction.

The issue here is clearly the right Keycode. I actually did something a little different than the class suggested in order to customize what keys changed the state, as I wanted to be able to do things like: Would you like to take the ( c)ar or ride the (b)oat?

You could use a similar method to find the correct Keycode for what you want.

So I created another Serialized field of string array called commands- then, in the same order as the serialized state array - I entered each command I wanted. This is really easy to see visually in Unity, but written it’s unclear, so what I mean is:

state[] = { state Car, state Boat }
command[] = {'C','B'}

(in the array, these do need to be upper case in the case I have here - I’m sure you could overcome that with a bit more code - the user input can still be lower case, just the entry in the array needs to be upper case)

Then the real magic happens in ManageStates, where each key in the command array is checked, and then the state is set as desired when it is pressed. This works no matter what alpha numeric key is in the array as long as the order of state objects in the state array is the same as the strings in the commands array:

    private void ManageStates()
    {
        var nextStates = state.GetNextStates();
        var nextCommands = state.GetNextCommands();
        var i = 0;
        foreach (string nextCommand in nextCommands)
        {
            //Debug.Log("nextCommand is " + nextCommand);
            KeyCode thisKeyCode = (KeyCode)System.Enum.Parse(typeof(KeyCode), nextCommand);
            //Debug.Log(thisKeyCode);
            if (Input.GetKeyDown(thisKeyCode))
            {
                Debug.Log("Key Pressed");
                state = nextStates[i];
            }
            i++;
        }
        textComponent.text = state.GetStateStory();
    }

You can use System.Enum.Parse to find the correct KeyCode of the characters that you want. You can even use an or (||) statement within the If to cover both the Apple keyboard 1 and the keypad 1.

Hope this helps!

What I did was I added

void OnGUI()
{
        Event e = Event.current;
        if (e.isKey)
        {
            Debug.Log("Detected key code: " + e.keyCode);
        }
}

This allowed me to see which keycodes where pressed.
Then I updated my ManageStates to the following.

    private void MangeState()
    {
        var nextStates = state.GetNextStates();

        if ((Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Ampersand))
            && nextStates.Length >= 1
        ) {
            state = nextStates[0];
            return;
        }

        if ((Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.None + 160))
            && nextStates.Length >= 2
        ) {
            state = nextStates[1];
            return;
        }

        if ((Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.DoubleQuote))
            && nextStates.Length >= 3
        ) {
            state = nextStates[2];
            return;
        }
 }

Since KeyCode 160 doesn’t exist I used KeyCode.None + 160.

Hope this helps

Privacy & Terms