- Home /
Input Detection
Hi all,
Basically, we have a game and there are two types of input; key board & mouse or controller. I'm currently making a singleton to detect whether the user is using one or the other input type, so here's the question, is there a way of detecting certain input from the difference devices?
Thank you (in advance)
Answer by hursty90 · Jun 21, 2011 at 02:14 PM
So this is what I've come up with ... (pretty inefficient but until there's a better way) ...
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;
}
}
Answer by homeros · Jun 21, 2011 at 12:37 PM
I haven't tried it before but maybe you can use
if(Input.GetButtonDown(KeyCode.JoystickButton0))
and
if(Input.anyKeyDown)
second one should be detecting only key and mouse clicks and first one should be detecting one joystick button.
It's not exactly what you wanted but it partially works :)
Thanks Speed, I'm pretty sure that the first one would only return true if the 0 button was pressed on the controller (ie the A button on xbox). I'll check if I can get controller input from the second one, if it doesn't return controller input I could try checking if an event has happened for input but not Input.any$$anonymous$$eyDown.
I'll report back soon :)
Unfortunately, my prediction was correct. Input.any$$anonymous$$eyDown detects any input including controller and Input.GetButtonDown($$anonymous$$eyCode.JoystickButton0) detects if button 0 on the controller has been hit
Your answer
Follow this Question
Related Questions
how to detect a mouse click that happened but wasnt on the gameobject? 2 Answers
unity device simulator use WASD to control 0 Answers
Can't get multiple microphone channels 3 Answers
new input device 0 Answers
Keep current rotation? 0 Answers