Search Unity

Getting last used device

Discussion in 'Input System' started by jesta, Oct 8, 2019.

  1. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    How would I retrieve the last used device, as in the last device for which input was processed?

    This is for the typical Mouse & keyboard + plugged in gamepad scenario.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1. Mouse.current
    2. Keyboard.current
    3. Gamepad.current
    To get rid of noisy gamepads (such as PS4) push themselves to the front, flip on "Filter Noise on Current" in the input system settings (found under project settings).
     
  3. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    Thanks for the reply, but I'm not sure I follow. When I have all three devices connected but I want to show control icons depending on which device is currently used, how would I go about that?

    For example:
    User clicks mouse button -> show kb&m icons.
    User moves gamepad stick -> show gamepad icons.
     
    valentin56610 likes this.
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    For that, the recommended way is to use PlayerInput. See here.

    Add one control scheme for keyboard&mouse and one for gamepad. Listen to InputUser.onChange to get notified when the control scheme switches. PlayerInput.currentControlScheme is the active control scheme.
     
    jesta likes this.
  5. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    I am currently in the same situation, I want to let the user switch from keyboard / mouse to controller and back (especially when typing messages in multiplayer chat)

    I do not want to use the PlayerInput component, is there any other solution to this?

    Checking for current active controlscheme doesn’t solve this because my keyboard+mouse AND controller scheme are both active at the same time to allow for this hot swap (having all devices connected, but not necessarily used, and never both used at the same time)
     
    Khud0 likes this.