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
1
Question by Leroterr · Aug 30, 2014 at 01:47 PM · movementinputcontrollerkeykeycode

Check the latest key pressed?

How can I check the latest key that the user pressed?

For example, when the user presses A, it will write "A" Then when the user presses B, it will only write "B", and will stop writing "A".

Here's an example of what I'm trying to say:

 if(Input.GetKey(KeyCode.A))
     {
         Debug.Log ("A");
     }
 
 if(Input.GetKeyUp(KeyCode.B))
     {
         Debug.Log ("B");
     }
 
 //What I want to happen is:
 //When the user presses A first, then B next, it will only write B, and not A.
 //When the user presses B first, then A afterwards, it will only write A, and not B. It will just completely stop executing Debug.Log("B")

// BUT, with my current code, it will still execute both A and B even though I pressed A first, then B next, or vice versa. I only want one to be executed even though I'm pressing both keys, and the one the should be executed is the latest key I've pressed.

Comment
Add comment · Show 6
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 screenname_taken · Aug 30, 2014 at 01:49 PM 0
Share

Write where? debug console? in game view? I'm guessing you know how to check for user input already.

avatar image Leroterr · Aug 30, 2014 at 01:53 PM 0
Share

Doesn't really matter, but yeah I guess in this case, the debug console.

$$anonymous$$eaning, it will stop writing A once it press B. It will only write the latest key that was pressed, even if the user is still pressing both A and B.

avatar image screenname_taken · Aug 30, 2014 at 02:29 PM 0
Share

It's Debug.Log. What will be written depends on which button you are checking for in the update loop i guess. Debug will always show stuff when it's called, so you could have a private variable that is assigned to the buttons during your checks, and then fed to debug at the very end.

avatar image Leroterr · Aug 30, 2014 at 02:38 PM 0
Share

I know that. But how can I make it so that it will only write "B" if the B key is the latest key I've pressed, even though I'm still pressing A and B at the same time?

avatar image frogsbo · Aug 30, 2014 at 05:10 PM 0
Share

var ab = 4;

if keydown a, ab = 0;

if keyup a and key = 0, ab = 4;

if keydown b , ab = 1;

if keyup b and key = 1, ab = 4;

if ab = 0 print a if ab = 1 print b

Show more comments

2 Replies

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

Answer by kacyesp · Aug 30, 2014 at 05:32 PM

This works. In the case where you hold down A, then hold down B, but then let go of B, if you do NOT want A to be printed out, just remove the last else statement with the while loop. You can also remove the stack variable. I used a stack to give you a more flexible implementation in case you want to add more inputs.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class InputProcessor 
 {
  
     private Keycode latestKey;
     private Stack<KeyCode> stack = new Stack<KeyCode>();
     
     void Update() 
     {
         if( Input.GetKeyDown(KeyCode.A) )
             latestKey = KeyCode.A;
 
         else if( Input.GetKeyDown(KeyCode.B)  )
             latestKey = KeyCode.B;
         
     
         if ( Input.GetKey(latestKey) )
             Debug.Log( latestKey );
         else
             while (stack.Count != 0)
             {
                 latestKey = stack.Pop();
                 if (Input.GetKey(latestKey)) 
                 { 
                     Debug.Log( latestKey );
                     break;
                 }
             }
 
     } //end of function Update
 
 } //end of class InputProcessor

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 frogsbo · Aug 30, 2014 at 05:11 PM

its technically known as radio buttons.

 this should work:
 
 var ab = 4;
 
 if keydown a, ab = 0;
 
 if keyup a and ab = 0, ab = 4;
 
 if keydown b , ab = 1;
 
 if keyup b and ab = 1, ab = 4;
 
 if ab = 0 print a 
 if ab = 1 print b
 
 
 you can also use a ab++ to count frames that it is pressed.

perhaps try more like this:

 laskeydown int = 0;

 if keydown a, lastkeydown = 1
 if keydown b, laskeydown = 2
 
 if keyup a and lastkeydown = 2, do nothing
 if keyup a and lastkeydown = 1, lastkeydown = 0
 
 if keyup b and lastkeydown = 1, do nothing
 if keyup b and lastkeydown = 2, lastkeydown = 1





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

25 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Speed pickup 1 Answer

Controller menu keylist 1 Answer

My Players speed differs depending on the input method (gamepad or keyboard) 1 Answer

New Input System for Controller is not able to work with autogenerated C# class file Unity 2018.4 0 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