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
0
Question by smerriam · Jul 24, 2020 at 03:34 PM · inputmouseclickkeypressoptions

How can I detect which key or mouse button was pressed?

I'm working on a keybindings menu and struggling with detecting whether the player presses a key or a mouse button. Currently my code looks like this:

         Event keyEvent;
         keyEvent = new Event();
         keyEvent = Event.current;
 
         if (recordingAttack)
         {
             if (keyEvent.isKey)
             {
                 Settings.Attack = keyEvent.keyCode;
             }
             if (keyEvent.isMouse)
             {
                 if (keyEvent.button == 0)
                     Settings.Attack = KeyCode.Mouse0;
                 else if (keyEvent.button == 1)
                     Settings.Attack = KeyCode.Mouse1;
                 else if (keyEvent.button == 2)
                     Settings.Attack = KeyCode.Mouse2;
                 else if (keyEvent.button == 3)
                     Settings.Attack = KeyCode.Mouse3;
                 else if (keyEvent.button == 4)
                     Settings.Attack = KeyCode.Mouse4;
                 else if (keyEvent.button == 5)
                     Settings.Attack = KeyCode.Mouse5;
                 else if (keyEvent.button == 6)
                     Settings.Attack = KeyCode.Mouse6;
             }
 
             recordingAttack = false;
             Attack.text = Settings.Attack.ToString();
         }

This code is running in update and there is one of these if statements for each keybinding, I know it is not a very efficient way but I'm rather new to code and wanted to try it myself. Any help is much appreciated :)

Comment
Add comment · Show 2
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 smark12007 · Jul 24, 2020 at 06:09 PM 0
Share
 Input.GetKeyDown(KeyCode.$$anonymous$$ouse0);
 Input.GetKey(KeyCode.$$anonymous$$ouse0);
 Input.GetKeyUp(KeyCode.$$anonymous$$ouse0);

Is it what you're looking for?

avatar image smerriam smark12007 · Jul 24, 2020 at 10:04 PM 0
Share

I don't believe so, I'm trying to figure out why my code for if (keyEvent.isKey) isn't working. In the editor it gives the following error: "NullReferenceException: Object reference not set to an instance of an object" I've found it sends this error on the if statement I specified above and I'm not sure why...

1 Reply

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

Answer by tuinal · Jul 24, 2020 at 10:55 PM

 if (Input.GetKeyDown(KeyCode.W) // returns true on only the single frame the W key was pressed
 if (Input.GetKey(KeyCode.W) // returns true every frame W is pressed
 if(Input.GetKeyUp(KeyCode.W) // returns true only on the frame W was released

 if(Input.GetButtonDown("Fire1") /returns true on the single frame whatever is bound to "Fire1" in edit>project settings>input.
 if(Input.GetAxis("Horizontal") returns a float in the range -1 to 1 based on the axis defined as "Horizontal" in edit>project settings>input.

Obviously KeyCode.W can be changed for any key. Input.GetMouseButtonDown(0) is also LMB, (1) is RMB. It's generally better to use GetButtonDown or GetAxis since they can be rebound easily or mapped to multiple devices, whereas GetKey is hardcoded to that key.

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

162 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 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 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

Differentiate left, right and both mouse inputs 0 Answers

Touch and MouseClick - One Game for Mobile and Desktop 3 Answers

Why is this script showing all keycodes except shift? 2 Answers

Anyway to get what key is down? 1 Answer

Create GUIText by pressing key 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