Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hursty90 · Jun 21, 2011 at 12:24 PM · inputdetectiondevice

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hursty90 · Jun 21, 2011 at 12:50 PM 0
Share

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 :)

avatar image hursty90 · Jun 21, 2011 at 12:53 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges