Search Unity

Detect if using Gamepad or Keyboard

Discussion in 'Input System' started by Mars91, Feb 12, 2020.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    As the title says is there a way to detect if the player is using the controller or mouse/keyboard?
     
  2. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hi,
    Yes, actually there is.
    I wrote it that way, I'm not sure if it's the best method but it works!

    Code (CSharp):
    1. public void OnJoin(InputAction.CallbackContext ctx)
    2. {
    3.     if (ctx.control.device is Keyboard)
    4.     {
    5.         // Do for keyboard...
    6.     }
    7.     else if (ctx.control.device is Gamepad)
    8.     {
    9.         // Do for gamepad...
    10.     }
    11. }
     
    Kaldrin likes this.
  3. Perry_lets

    Perry_lets

    Joined:
    Jul 18, 2020
    Posts:
    1
    What if I am using a action that uses a value instead of a action with a type of button?
     
  4. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    46
    Thanks this works well and is simple enough !