Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
8
Question by Multiplayer · Mar 06, 2013 at 06:48 PM · inputbuttonjoystickcontrolsgamepad

Find out if any Button on any Gamepad has been pressed and which one

Hi

The title pretty much says it all. I want my players to be able to assign custom gamepad buttons for the controls. So imagine you click on "jump" and a message says "press the key for jump", after which you press that key, the game remembers which one it was and you can use the new setting from then on.

To do that for a keyboard is easy, since I only have to call

 public void OnGUI ()
     {        
         if (Event.current.type == EventType.KeyDown) {
             KeyCode code = Event.current.keyCode;
             [...]

to find out what was pressed and it is very easy to work with that KeyCode.

For gamepads however there does not seem to be such an event. The only option I have found so far is to poll

 if (Input.GetKeyDown ("joystick 1 button 0"))

for each button on each gamepad during each update(), which seems very bloated and certainly not right.

Does anyone know an easier way to catch any button that has been pressed?

Comment
Add comment · Show 3
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 Wyldcard · Mar 21, 2014 at 03:54 PM 0
Share

I know it's an old question but you can set up the buttons in the "input" menu and call the input name ins$$anonymous$$d of the "joystick 1..."

avatar image Endless_Aftermath · Jul 24, 2014 at 03:54 AM 0
Share

Did you ever figure out how to do this? I'm curious, myself.

http://answers.unity3d.com/questions/755304/how-do-you-read-or-get-info-from-an-input-from-a-j.html

avatar image drudiverse · Jul 24, 2014 at 10:13 AM 0
Share

it doesnt take much processor to monitor all changes in all axes and buttons from joypad using:

 if (button1value != lastbutton1vaulue){button1Changed == True; lastButtonValue = button1value}else{button1changed = false;}

a slight variation of that is to make a variable anybuttonHasChanged... and any time any button changes, that value becomes true for a frame. you also have to make small exceptions in case 2-3 buttons change in a single frame, i.e. pad1 and axis1,

it's fine to scan all buttons on gamepad, it wouldnt be much different from a magic function which scans all buttons on gamepad./

6 Replies

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

Answer by drudiverse · Jul 24, 2014 at 10:25 AM

No there isnt an any button option, but if you need to find which one, you have to monitor them all anyway...

writing IF is actually the most efficient way of writing an multiplex for scanning IO values, it seems abit long but it's very efficient, IF statements are less taxing than even a single + arithmetic to the processor. so it's like every frame you are adding 20 plus arithmetics it's pretty cool.

Comment
Add comment · Show 3 · 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 Multiplayer · Jul 31, 2014 at 10:04 AM 0
Share

In the end I did exactly this, details here http://answers.unity3d.com/questions/755304/how-do-you-read-or-get-info-from-an-input-from-a-j.html#answer-760740 so for future readers; this seem to be correct unfortunately.

avatar image DESTRUKTORR · Sep 10, 2015 at 12:28 PM 4
Share

FIrstly, I'd like to see you back up your statement that flow control statements are somehow "faster" than addition operations.

The .NET compiler tends to optimize things better if there is LESS flow control, and smaller function sizes. What's more, a single if statement can easily (and often does) turn into a branch, which can be mispredicted by the CPU, resulting in a much longer time waiting than a single addition statement.

While, to the best of my knowledge, most CPUs and popular language implementations (such as .NET C#) get somewhere in the order of 99%+ correct branch prediction, that 1% is so costly it's still well worth it to just NOT use if statements where you don't need to.

That being said, replacing addition operations with if statements should not be done for optimization (even if it did somehow make the operation faster... which in the VAST majority of cases it will certainly NOT). This would be a micro-optimization, and wouldn't stand to improve overall performance by any amount significant enough that a human could even notice it short of spitting out the microseconds per frame.

TLDR:

If statements are not faster than addition, and are often $$anonymous$$UCH slower. Don't micro-optimize. Follow the 3-step hierarchy of code writing -- Functionality comes first, then readability, then performance.

Only if performance becomes such a problem that it actually begins to affect functionality should you EVER consider altering code to improve its performance at the cost of readability.

avatar image canine828 · May 27, 2016 at 01:00 PM 0
Share

The best option is to write a library for this, so everyone who needs to use it doesn't have to write the code themselves. (it would return a $$anonymous$$eyCode corresponding to the pressed key)

avatar image
10

Answer by ayrrik · Sep 30, 2015 at 09:05 AM

 for (int i = 0;i < 20; i++) {
             if(Input.GetKeyDown("joystick 1 button "+i)){
                 print("joystick 1 button "+i);
             }
         }

Comment
Add comment · Show 5 · 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 dmi3iy · Jan 08, 2016 at 11:47 PM 0
Share

This code working for me, thank you, ayrrik! ( USB Joystic Esperanza EG102 )

avatar image canine828 · Feb 25, 2016 at 10:22 PM 0
Share

I can't comment on @ayrrik's post, but I noticed an error in his answer code. The problem is that you can't directly concatenate strings and integers. You have to use integer_variable_name.toString() to convert it to a string. So here's the fixed snippet:


for (int i = 0; i < 20; i++) {
    if (Input.Get$$anonymous$$eyDown("joystick 1 button "+i.toString())) {
        // do something
    }
}

avatar image Lohoris2 · May 27, 2016 at 07:49 AM 2
Share

@canine828 what are you talking about? Of course you can!

avatar image canine828 Lohoris2 · May 27, 2016 at 12:57 PM 0
Share

Huh. I wasn't able to before... $$anonymous$$aybe the sysad$$anonymous$$ moved it? Or... OH! It must have been that I wasn't signed in when I first clicked it, then I signed, in, then I pressed the Back button to get back here, and then I wasn't able to, and then I refreshed it, and then I posted! Or something like that...

avatar image Dave-Carlile canine828 · May 27, 2016 at 01:11 PM 0
Share

I think @Lohoris2 was referring to the fact that you can indeed concatenate a string and integer without calling ToString.

avatar image
4

Answer by Frosting · Mar 25, 2016 at 05:51 PM

Hello,

I may help someone. I am using Logitech Wireless GamePad F710.

Here detection pressed button:

 void Update ()    {
         if (Input.GetKeyDown(KeyCode.JoystickButton0)) {
             Debug.Log("wohoo!!");            
         }
  }

Here description of buttons of this Gamepad:

 JoystickButton0 - X
 JoystickButton1 - A
 JoystickButton2 - B
 JoystickButton3 - Y
 JoystickButton4  - LB
 JoystickButton5  - RB
 JoystickButton6  - LT
 JoystickButton7  - RT
 JoystickButton8 - back
 JoystickButton9 - start
 JoystickButton10 - left stick[not direction, button]
 JoystickButton11 - right stick[not direction, button]
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
3

Answer by varumora · Apr 08, 2016 at 06:42 AM

Actually there is an "anyKey" option, but you still have to iterate for every keyCode:

 if (Input.anyKey)
 {
         foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
         {
                              //do something with kcode
         }
 }

Comment
Add comment · Show 1 · 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 Marrt · Apr 18, 2016 at 08:32 PM 2
Share

i did this too, this tests all possible keycodes in the enum "$$anonymous$$eyCode" and prints if it has been pressed. This can be used to assign inputs as $$anonymous$$eycodes, or you can cast it to strings inputs if you know how.

 System.Array values = System.Enum.GetValues(typeof($$anonymous$$eyCode));
 foreach($$anonymous$$eyCode code in values){
     if(Input.Get$$anonymous$$eyDown(code)){ print(System.Enum.GetName(typeof($$anonymous$$eyCode), code)); }                    
 }

but i am still searching how to parse the axis...

avatar image
0

Answer by MelonHeadStudios · May 16, 2015 at 10:18 PM

So after Extensive thinking, I believe I have found the answer to this long standing problem. here's the code--- You need a string variable Called CONFIGBUTTONS2 for this instance to work.

 var Numbers1 = new Array (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
 for (var go1 : float in Numbers1) {
 if(Input.GetKeyDown("joystick 1 button "+go1)){CONFIGBUTTONS2 = "joystick 1 button "+go1;}
 if(Input.GetKeyDown("joystick 2 button "+go1)){CONFIGBUTTONS2 = "joystick 2 button "+go1;}
 if(Input.GetKeyDown("joystick 3 button "+go1)){CONFIGBUTTONS2 = "joystick 3 button "+go1;}
 if(Input.GetKeyDown("joystick 4 button "+go1)){CONFIGBUTTONS2 = "joystick 4 button "+go1;}
 if(Input.GetKeyDown("joystick 5 button "+go1)){CONFIGBUTTONS2 = "joystick 5 button "+go1;}
 if(Input.GetKeyDown("joystick 6 button "+go1)){CONFIGBUTTONS2 = "joystick 6 button "+go1;}
 if(Input.GetKeyDown("joystick 7 button "+go1)){CONFIGBUTTONS2 = "joystick 7 button "+go1;}
 if(Input.GetKeyDown("joystick 8 button "+go1)){CONFIGBUTTONS2 = "joystick 8 button "+go1;}
 }
 var Numbers2 = new Array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
 for (var go2 : String in Numbers2) {
 if(Input.GetKeyDown(go2)){CONFIGBUTTONS2 = go2;}
 }

So this script checks through all the buttons on 8 separate joysticks and returns it in the form of the String CONFIGBUTTONS2. So if you want to assign that to an input button, just assign the value of CONFIGBUTTONS2 to another variable like ABUTTON. then use that ABUTTON in...

 if(Input.GetKey(ABUTTON)){
 //do something
 }

I have yet to find a way to easily find all the Input Axis's, but those aren't nearly as complicated as the 150+ joystick buttons. Hope this Helps!

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
  • 1
  • 2
  • ›

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add input for two different local players? 1 Answer

How to get stick == neutral using new Input System 0 Answers

gamepad input sensitivity 0 Answers

Using UI Button to trigger button down?? 0 Answers

Rebiding of keyboard/mouse/Joystick/Pad controls in the game 1 Answer


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