• 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
4

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
2

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 instantiate a game object every time when space key is pressed 2 Answers

Door Script Problem 1 Answer

Trigger when holding key 1 Answer

How to detect a keyboard key held down? 2 Answers

Trying to assign two functions to one key 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges