Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skjalg · Jun 19, 2011 at 09:12 AM · inputcontrollerjoystick

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 15, 2015 at 06:31 AM 0
Share

Note http://answers.unity3d.com/questions/465202/trying-to-find-a-way-to-detect-if-a-mouse-is-plugg.html

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by hursty90 · Jun 21, 2011 at 02:18 PM

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;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ali-Nagori · Jan 21, 2014 at 02:18 AM 0
Share

thanks a lot this was very helpful :).

avatar image ZoraMikau · Apr 28, 2020 at 04:26 PM 0
Share

Just wanted to add that the check here has to be in the OnGUI function, if you change it to an Update() call you'll have issues finding the Event.current variable.

avatar image
0

Answer by robpuk38 · Nov 28, 2016 at 02:32 PM

 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[x].Length);
              if (names[x].Length == 19)
              {
                  print("PS4 CONTROLLER IS CONNECTED");
                  PS4_Controller = 1;
                  Xbox_One_Controller = 0;
              }
              if (names[x].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
  }
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ZoraMikau · May 12, 2020 at 10:28 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Diego-plds · Jan 11, 2021 at 02:14 PM

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]


back-in-time-joejam-preloader-universal-windows-pl.png (23.1 kB)
back-in-time-joejam-preloader-universal-windows-pl.png (42.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Permanent unintentional Input on Axis 1 Answer

Analog stick control 1 Answer

360 Trigger Pressing both Triggers at the same time 1 Answer

How to have multiple types of controller input using the input manager 0 Answers

Gamecube Controller joystick axis does not work? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges