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 /
  • Help Room /
avatar image
1
Question by Nesocodon · Jul 16, 2016 at 04:16 PM · buttonclickkeykeycodepress

Get keycode of pressed button

What and why? I'm making a settings menu where player can change the controls by pressing the keys they want to use for specific actions. Very basic feature.

Problem? How do I get the keycode of the buttom the player presses? To store for actual use in game.

I've tried to search for the solution...

1) Input.inputString method: Seems to work only with characters. Nope.

2) System.Enum.GetNames(typeof(KeyCode)).Length method: Excludes mouse and I'd assume the same is with controllers. Nope.

3) OnGUI method: It's 2016. Is anything "GUI" related even supported anymore?

Any other methods to do the thing?

And please, don't mention the configuration window. There is no way I'm going to enable that thing.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nesocodon · Jul 19, 2016 at 12:25 AM

And yes, it is doable with the OnGUI() method. Below is my solution which works. But as you can see, it looks really bad and isn't 100% perfect. Is there any proper way to do this?

Can't we just have something like:

KeyCode[] keys = Input.GetKeys();

or

KeyCode[] keys = Input.GetKeys(PressMode); //PressMode = down/held/up/drag etc.

?

Or something similar to mobile touches?

  void OnGUI()
     {
         KeyCode pressed = GetKeyCode(Event.current);
         if (pressed != KeyCode.None) { print("You pressed: " + pressed); }
     }
     public KeyCode GetKeyCode(Event e)
     {
         if (e.isKey && e.keyCode != KeyCode.None) { return e.keyCode; }
         if (e.isMouse) { return KeyCode.Mouse0 + e.button; }
         if (e.shift)
         {
             if (Input.GetKey(KeyCode.RightShift)) { return KeyCode.RightShift; }
             else { return KeyCode.LeftShift; }
         }
         if (e.alt)
         {
             if (Input.GetKey(KeyCode.RightAlt)) { return KeyCode.RightAlt; }
             else if (Input.GetKey(KeyCode.LeftAlt)) { return KeyCode.LeftAlt; }
             else { return KeyCode.AltGr; }
         }
         if (e.control)
         {
             if (Input.GetKey(KeyCode.RightControl)) { return KeyCode.RightControl; }
             else { return KeyCode.LeftControl; }
         }
         for (int i = 0; i < 20; i++)
         {
             for (int j = 0; j < 9; j++)
             {
                 if (j > 0)
                 {
                     if (Input.GetKey("joystick " + j + " button " + i))
                     { return KeyCode.Joystick1Button0 + i + (j * 20); }
                 }
                 else
                 {
                     if (Input.GetKey("joystick button " + i))
                     { return KeyCode.Joystick1Button0 + i + (j * 20); }
                 }
             }
         }
         return KeyCode.None;
     }

I don't guarantee the code works 100% with all keys...

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

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

64 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

About button press sprint 1 Answer

How can I change the UI button color when clicked 1 Answer

if (Input.GetKey (KeyCode.Escape)) Won't Work. 0 Answers

Check if animation is playing on button click 0 Answers

Input.GetKeyUp wont work! 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