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 bracciata · Sep 21, 2015 at 02:12 AM · c#keykeypress

Checking if any key is up?

My code for key down

 if (Input.anyKeyDown() ){
             KeyDownMethod();}

but is there any way to check if any keys are released so an anykeyup. Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Danielori · Sep 21, 2015 at 04:57 AM

This may help you.

 bool holdingDown;
 
 void Update () {
 
         if (Input.anyKey) {
             Debug.Log("A key is being pressed");
             holdingDown = true;
         }
 
         if (!Input.anyKey && holdingDown) {
             Debug.Log("A key was released");
             holdingDown = false;
         }
 
 }

It doesn't work for individual keys, but close enough.

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 ellioman · Sep 15, 2016 at 02:48 PM

Could use the UnityGUI event calls in OnGUI()...

 private void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.KeyDown)
     {
         if (Input.GetKeyDown(e.keyCode))
         {
             Debug.Log("Down: " + e.keyCode);
         }
     }
     else if (e.type == EventType.keyUp)
     {
         if (Input.GetKeyUp(e.keyCode))
         {
             Debug.Log("Up: " + e.keyCode);
         }
     }
 }

More info: https://docs.unity3d.com/ScriptReference/Event.html

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
1

Answer by chrisrwingate · Feb 21, 2018 at 02:39 AM

Since this is the first thing that comes up in a search, I figured I'd offer a solution since one doesn't seem to be provided by Unity.

The example will detect any key when it is pressed or held as well as detect when one of those buttons is released. This code also detects what specific keys are being pressed/released.

 public class GameInput : MonoBehaviour
 {
     protected List<KeyCode> m_activeInputs = new List<KeyCode>();
 
     public void Update()
     {
         List<KeyCode> pressedInput = new List<KeyCode>();
 
         if (Input.anyKeyDown || Input.anyKey)
         {
             foreach(KeyCode code in System.Enum.GetValues(typeof( KeyCode )) )
             {
                 if (Input.GetKey(code))
                 {
                     m_activeInputs.Remove( code );
                     m_activeInputs.Add( code );
                     pressedInput.Add( code );
 
                     Debug.Log( code +  " was pressed" );
                 }
             }
         }
 
         List<KeyCode> releasedInput = new List<KeyCode>();
 
         foreach(KeyCode code in m_activeInputs)
         {
             releasedInput.Add( code );
 
             if(!pressedInput.Contains(code))
             {
                 releasedInput.Remove( code );
 
                 Debug.Log( code +  " was released" );
             }
         }
 
         m_activeInputs = releasedInput;
     }
 }
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 pixilestudios · Sep 22, 2018 at 05:47 AM 0
Share

Wow I just want to thank you for this code snippet. I have been dealing with keybinding pains and this saved me. Everyone says to use OnGui() "Event.current" but Event doesn't catch $$anonymous$$ouse3+. So until I saw your post I almost gave up haha.

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

32 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

Related Questions

How to detect a keyboard key held down? 2 Answers

Disable Key Using Javascript 0 Answers

Why does this condition keep being true even if its not? 0 Answers

Keypresses blocking each other. 0 Answers

How to use a key as trigger? 2 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