Search Unity

Check Whether Controller or Keyboard is being used via script

Discussion in 'Scripting' started by Leonard-Walker, Oct 7, 2015.

  1. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    Capture.PNG

    I am wondering how I can access this to Check the String and set a boolean, to tell whether a keyboard or controller is being used.
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well, a quick search suggests that there is no optimal way of doing things, yet (at least, not without some asset or plugin).
    http://answers.unity3d.com/questions/131899/how-do-i-check-what-input-device-is-currently-beei.html
    This is someone's answer to the problem.
    Seeing as it's quite old, I wonder if there's a solution by now, but I did not see it - briefly looking at the unity API for the Input and Input Manager.

    EDIT:
    Found these additional answers;
    1. Using Input.GetJoystickNames: http://answers.unity3d.com/questions/714610/detect-if-an-xbox-360-controller-is-plugged-in.html
    2. Using an external asset: http://forum.unity3d.com/threads/joystick-detection-and-direct-axis-detection.114993/
     
    Last edited: Oct 7, 2015
  3. Aspekt1024

    Aspekt1024

    Joined:
    Sep 29, 2016
    Posts:
    12
    For anyone else who comes across this, I found a solution that works really well.
    I use an InputHandler class to manage the different kinds of inputs, and ask each input (e.g. Keyboard, Controller) if an input was received based on my keybindings.

    Code (CSharp):
    1.     public class InputHandler
    2.     {
    3.         private enum InputMode
    4.         {
    5.             Keyboard, Controller
    6.         }
    7.         private InputMode mode;
    8.  
    9.         private GameManager gameManager;
    10.         private KeyboardInputHandler keyboardInput;
    11.         private ControllerInputHandler controllerInput;
    12.  
    13.         public InputHandler(GameManager manager)
    14.         {
    15.             gameManager = manager;
    16.             keyboardInput = new KeyboardInputHandler(manager);
    17.             controllerInput = new ControllerInputHandler(manager);
    18.         }
    19.  
    20.         public void ProcessInput()
    21.         {
    22.             bool receivedControllerInput = controllerInput.ProcessInput();
    23.             bool receivedKeyboardMouseInput = keyboardInput.ProcessInput();
    24.  
    25.             if (receivedControllerInput)
    26.             {
    27.                 mode = InputMode.Controller;
    28.             }
    29.             else if (receivedKeyboardMouseInput)
    30.             {
    31.                 mode = InputMode.Keyboard;
    32.             }
    33.             Debug.Log("Input mode: " + mode);
    34.         }

    Then I define the keybindings in the Keyboard or Controller input handler. Here's an example of the ControllerInputHandler:

    Code (CSharp):
    1.     public class ControllerInputHandler
    2.     {
    3.         // axis and button definitions here, e.g.
    4. #if UNITY_STANDALONE_WIN
    5.         private static KeyCode A_BUTTON = KeyCode.JoystickButton0;
    6.         private static KeyCode X_BUTTON = KeyCode.JoystickButton2;
    7.         private static KeyCode START_BUTTON = KeyCode.JoystickButton7;
    8. #else
    9.         // other platforms
    10.         // see http://wiki.unity3d.com/index.php?title=Xbox360Controller
    11. #endif
    12.  
    13.         private GameManager gameManager;
    14.  
    15.         private Dictionary<KeyCode, Action> getKeyDownBindings = new Dictionary<KeyCode, Action>();
    16.         private Dictionary<KeyCode, Action> getKeyUpBindings = new Dictionary<KeyCode, Action>();
    17.         private Dictionary<KeyCode, Action> getKeyBindings = new Dictionary<KeyCode, Action>();
    18.  
    19.         private bool movedByAxis;
    20.  
    21.         public ControllerInputHandler(GameManager manager)
    22.         {
    23.             gameManager = manager;
    24.  
    25.             getKeyDownBindings.Add(A_BUTTON, gameManager.JumpPressed);
    26.             getKeyDownBindings.Add(X_BUTTON, gameManager.MeleePressed);
    27.             getKeyDownBindings.Add(START_BUTTON, gameManager.ToggleMenu);
    28.  
    29.             getKeyUpBindings.Add(A_BUTTON, gameManager.JumpReleased);
    30.         }
    31.  
    32.         public bool ProcessInput()
    33.         {
    34.             bool inputReceived = false;
    35.             foreach (var binding in getKeyDownBindings)
    36.             {
    37.                 if (Input.GetKeyDown(binding.Key))
    38.                 {
    39.                     inputReceived = true;
    40.                     gameManager.DirectInput(binding.Value);
    41.                 }
    42.             }
    43.  
    44.             foreach (var binding in getKeyUpBindings)
    45.             {
    46.                 if (Input.GetKeyUp(binding.Key))
    47.                 {
    48.                     inputReceived = true;
    49.                     gameManager.DirectInput(binding.Value);
    50.                 }
    51.             }
    52.  
    53.             foreach (var binding in getKeyBindings)
    54.             {
    55.                 if (Input.GetKey(binding.Key))
    56.                 {
    57.                     inputReceived = true;
    58.                     gameManager.DirectInput(binding.Value);
    59.                 }
    60.             }
    61.  
    62.             // I could probably tidy the axis code up, but you get the idea!
    63.             if (Input.GetAxis(LEFT_STICK_H_AXIS) > 0.5f)
    64.             {
    65.                 inputReceived = true;
    66.                 movedByAxis = true;
    67.                 gameManager.DirectInput(gameManager.MoveRightPressed);
    68.             }
    69.             else if (Input.GetAxis(LEFT_STICK_H_AXIS) < -0.5f)
    70.             {
    71.                 inputReceived = true;
    72.                 movedByAxis = true;
    73.                 gameManager.DirectInput(gameManager.MoveLeftPressed);
    74.             }
    75.             else if (movedByAxis)
    76.             {
    77.                 movedByAxis = false;
    78.                 gameManager.DirectInput(gameManager.MoveReleased);
    79.             }
    80.  
    81.             return inputReceived;
    82.         }
    This will only pick up keybindings you've defined, but it works. What's great about this is you can serialize the key binding dictionaries for your own custom key bindings menu, if you wanted.
     
  4. PPLorux

    PPLorux

    Joined:
    Jul 29, 2017
    Posts:
    8
    @Aspekt1024 Just what I was looking for, thank you!
     
  5. DBarlok

    DBarlok

    Joined:
    Apr 24, 2013
    Posts:
    268
  6. Aspekt1024

    Aspekt1024

    Joined:
    Sep 29, 2016
    Posts:
    12
    Seeing as people are reading this thread again, it's worth updating with some new knowledge I have on the topic.

    Both Rewired and the newer Unity Input system have listeners for controller input, be it keyboard, mouse, gamepad or anything else, without having to define keys used for your game (which is what my previous answer details).

    For Unity's input system, for example, you can use the
    InputSystem.onEvent
    delegate to monitor for input, which will give you access to the device (controller) used. Checking which device will let you set the appropriate controller type for glyphs on-screen etc.

    For further info, check out:
    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Events.html#monitoring-events

    If you're still on the default bare-bones Unity input system, i'd strongly recommend switching to Unity's newer input package (free, inbuilt via package manager, very featured) or Rewired (not free, but also very good).
     
    Lilith_Aya likes this.