Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect if the player is using mouse and keyboard?(no controller connected)

I’m trying to detect the device the player is using when playing the game. if it is (PS controller, X box Controller, or playing with mouse and keyboard(no controller is connected)…

void Start()
    {
        var gamepad = Gamepad.current;
        if (gamepad == null)
            return;
        if (gamepad is DualShockGamepad)
        {
          
            print("Playstation gamepad");

        }
        else if (gamepad is XInputController)
        {
                   
            print("Xbox gamepad");
        }
          
    }

I was able to detect the controllers, but how can I add a condition for the keyboard/mouse situation? please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

trying below my code example

using UnityEngine;
using UnityEngine.InputSystem;

public class InputDetection : MonoBehaviour
{
    void Start()
    {
        var gamepad = Gamepad.current;
        if (gamepad != null)
        {
            if (gamepad is DualShockGamepad)
            {
                print("PlayStation gamepad");
            }
            else if (gamepad is XInputController)
            {
                print("Xbox gamepad");
            }
        }
        else if (Keyboard.current != null || Mouse.current != null)
        {
            print("Mouse and keyboard");
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading