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.

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;
    }
}

private int Xbox_One_Controller = 0;
private int PS4_Controller = 0;
void Update()
{
string names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
print(names.Length);
if (names.Length == 19)
{
print(“PS4 CONTROLLER IS CONNECTED”);
PS4_Controller = 1;
Xbox_One_Controller = 0;
}
if (names.Length == 33)
{
print(“XBOX ONE CONTROLLER IS CONNECTED”);
//set a controller bool to true
PS4_Controller = 0;
Xbox_One_Controller = 1;

             }
         }
 
 
 if(Xbox_One_Controller == 1)
 {
 //do something
 }
 else if(PS4_Controller == 1)
 {
 //do something
 }
 else
 {
 // there is no controllers
 }
 }

I expanded off hursty90’s here and basically made it a bit more efficient.

It’s in update so it should only happen once a frame as opposed to potentially more than once with OnGUI (which is supposedly slow to begin with). I mainly swapped out the Event.current calls as they only work in OnGUI. I also checked how the mouse buttons are checked (because there isn’t an Input.anyMouseButton function) and added the mouse scroll wheel as one of the checks.

EDIT/IMPORTANT: I just realized there’s a slight bug in this by calling Input.anyKey as it will catch controller events. You can bypass this by checking for whatever keys you want to check for on the keyboard being pressed instead of Input.anyKey

public class InputChecker : MonoBehaviour 
{
    private static InputChecker _instance;

    public static InputChecker Instance { get { return _instance; } }

    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        } else {
            _instance = this;
        }
    }

   public enum InputType
    {
        MouseKeyboard,
        Controller
    };

    private InputType InputState = InputType.MouseKeyboard;

    void Update()
    {
        switch(InputState)
        {
           case InputType.MouseKeyboard:
                if(isControllerInput())
                {
                    InputState = InputType.Controller;
                    Debug.Log("Switched Input to Controller");
                }
                break;
            case InputType.Controller:
                if (isMouseKeyboard())
                {
                    InputState = InputType.MouseKeyboard;
                    Debug.Log("Switched Input to Mouse/Keyboard");
                }
                break;
        }
    }
  
    public InputType GetInputType()
    {
        return InputState;
    }

    public bool UsingController(){
        return InputState == InputType.Controller;
    }

    private bool isMouseKeyboard()
    {
        // mouse & keyboard buttons and mouse movement
        if (Input.anyKey ||
            Input.GetMouseButton(0) || 
            Input.GetMouseButton(1) || 
            Input.GetMouseButton(2) ||
            Input.GetAxis("Mouse ScrollWheel") != 0.0f)
        {
            return true;
        }
        return false;
    }

    private bool isControllerInput()
    {
        // joystick buttons
        // check if we're not using a key for the axis' at the end 
        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) ||
           Input.GetAxis("Horizontal") != 0.0f     ||
           Input.GetAxis("Vertical") != 0.0f)
        {
            return true;
        }
        
        return false;
    }

hello, using the scripts shown, I made my own along with the new input system.Remembering that you already have to know about the new input system

using UnityEngine;
public class InputChecker : MonoBehaviour
{
    public enum InputDevice { controller = 0, keyboard = 1 };
    public InputDevice inputDevice;
    public string nameDevice;
    private InputMasterAction inputActions;
    private void Awake()
    {
        inputActions = new InputMasterAction();
    }
    private void Update()
    {
        Inputs();
        //to find out the name of the controller
        if (inputActions.InputsChecker.Inputs.activeControl != null)
        {
            nameDevice = inputActions.InputsChecker.Inputs.activeControl.device.name;
        }
    }
    private void Inputs()
    {
        #region CONTROLLER
        //checks if any button on the controller has been pressed
        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))
        {
            inputDevice = InputDevice.controller;
        }
        if (inputActions.InputsChecker.Inputs.activeControl != null)
        {
            //checks if it is a control
            //if you use another controller other than xbox put below
            if (inputActions.InputsChecker.Inputs.activeControl.device.name == "XInputControllerWindows")
            {
                inputDevice = InputDevice.controller;
            }
        }
        #endregion
        #region KEYBOARD
        //checks if any key on the keyboard has been pressed
        if (Input.anyKey &&
 !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))
        {
            inputDevice = InputDevice.keyboard;
        }
        //checks if it's a keyboard or mouse
        if (inputActions.InputsChecker.Inputs.activeControl != null)
        {
            if (inputActions.InputsChecker.Inputs.activeControl.device.name == "Mouse")
            {
                inputDevice = InputDevice.keyboard;
            }
        }
        #endregion
        #region INPUTCHECKER
        if (inputDevice == InputDevice.controller)
        {
            //GameManager.controller = true;
            Debug.Log("Controller");
        }
        else
        {
           //GameManager.controller = false;
            Debug.Log("keyboard");
        }
        #endregion

    }
    private void OnEnable()
    {
        inputActions.Enable();
    }
    private void OnDisable()
    {
        inputActions.Disable();
    }
}

![alt text][1]
![alt text][2]