x


How do I check what input device is currently beeing used?

I am making tutorial prompts for our game that will show textures of the input buttons that you need to press in order to execute certain moves. But, since players can use different joysticks and/or controllers, I need a way to check what joystick has been used last. (xbox controller/ps3 controller/keyboard/mouse) etc..

I know I can use Input.GetJoystickNames to see what joysticks are connected, but I am wondering how I can use this to figure out excactly which one of them are being used at any given moment.

more ▼

asked Jun 19 '11 at 09:12 AM

Skjalg gravatar image

Skjalg
1.4k 24 33 50

Hey Skjalg, sorry to ask an off-topic question in a comment but I don't know how else to contact you. I've been following your "visual logic editor" and I'm wondering if you can point me to the resources you used to write that thing. It looks incredible! Or.. could I possibly have the source? I want to make a custom editor tool and I'm having a hell of a time getting good resources.

Thanks

Jun 21 '11 at 04:19 PM flaviusxvii
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Hi there,

I've been having the same problem as yourself today, I've written the below code to solve the problem, its pretty inefficiency but until there is a better way ...

So if you want to tell which joystick is being used you could manipulate this code to detect the other joystick input and add more states ...

using UnityEngine;
using System.Collections;

public class InputControl : MonoBehaviour 
{
    //*********************//
    // Public member data  //
    //*********************//


    //*********************//
    // Private member data //
    //*********************//

    public enum eInputState
    {
        MouseKeyboard,
        Controler
    };
    private eInputState m_State = eInputState.MouseKeyboard;

    //*************************//
    // Unity member methods    //
    //*************************//

    void OnGUI()
    {
        switch( m_State )
        {
            case eInputState.MouseKeyboard:
                if(isControlerInput())
                {
                    m_State = eInputState.Controler;
                    Debug.Log("DREAM - JoyStick being used");
                }
                break;
            case eInputState.Controler:
                if (isMouseKeyboard())
                {
                    m_State = eInputState.MouseKeyboard;
                    Debug.Log("DREAM - Mouse & Keyboard being used");
                }
                break;
        }
    }

    //***************************//
    // Public member methods     //
    //***************************//

    public eInputState GetInputState()
    {
        return m_State;
    }

    //****************************//
    // Private member methods     //
    //****************************//

    private bool isMouseKeyboard()
    {
        // mouse & keyboard buttons
        if (Event.current.isKey ||
            Event.current.isMouse)
        {
            return true;
        }
        // mouse movement
        if( Input.GetAxis("Mouse X") != 0.0f ||
            Input.GetAxis("Mouse Y") != 0.0f )
        {
            return true;
        }
        return false;
    }

    private bool isControlerInput()
    {
        // joystick buttons
        if(Input.GetKey(KeyCode.Joystick1Button0)  ||
           Input.GetKey(KeyCode.Joystick1Button1)  ||
           Input.GetKey(KeyCode.Joystick1Button2)  ||
           Input.GetKey(KeyCode.Joystick1Button3)  ||
           Input.GetKey(KeyCode.Joystick1Button4)  ||
           Input.GetKey(KeyCode.Joystick1Button5)  ||
           Input.GetKey(KeyCode.Joystick1Button6)  ||
           Input.GetKey(KeyCode.Joystick1Button7)  ||
           Input.GetKey(KeyCode.Joystick1Button8)  ||
           Input.GetKey(KeyCode.Joystick1Button9)  ||
           Input.GetKey(KeyCode.Joystick1Button10) ||
           Input.GetKey(KeyCode.Joystick1Button11) ||
           Input.GetKey(KeyCode.Joystick1Button12) ||
           Input.GetKey(KeyCode.Joystick1Button13) ||
           Input.GetKey(KeyCode.Joystick1Button14) ||
           Input.GetKey(KeyCode.Joystick1Button15) ||
           Input.GetKey(KeyCode.Joystick1Button16) ||
           Input.GetKey(KeyCode.Joystick1Button17) ||
           Input.GetKey(KeyCode.Joystick1Button18) ||
           Input.GetKey(KeyCode.Joystick1Button19) )
        {
            return true;
        }

        // joystick axis
        if(Input.GetAxis("XC Left Stick X") != 0.0f ||
           Input.GetAxis("XC Left Stick Y") != 0.0f ||
           Input.GetAxis("XC Triggers") != 0.0f ||
           Input.GetAxis("XC Right Stick X") != 0.0f ||
           Input.GetAxis("XC Right Stick Y") != 0.0f )
        {
            return true;
        }

        return false;
    }
}
more ▼

answered Jun 21 '11 at 02:18 PM

hursty90 gravatar image

hursty90
31 7 7 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x981
x549
x223
x2

asked: Jun 19 '11 at 09:12 AM

Seen: 1767 times

Last Updated: Jun 21 '11 at 04:19 PM