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 Braindrift · Dec 08, 2012 at 06:09 PM · inputgetkeydown

Why is GetKeyDown returning false when provided with string from .inputString?

I'm coding a key tracker function in the purpose to trace if any key was double-klicked. For now, I want it to check what key was last pressed and if that key was pressed this frame.

When using the approach below, GetKeyDown seems to return 'false' even though keyDown are providing a string. Any hints on why this approach doesn't work?

 var keyDown : String = Input.inputString;     
 if(keyDown != String.Empty && Input.GetKeyDown(keyDown)){
      Debug.Log(keyDown);
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Dec 08, 2012 at 11:12 PM

Apparently, internally Input.inputString is updated after GetKeyDown is executed, thus keyDown is empty when you read Input.GetKeyDown. For what do you need this? Tell us, and maybe someone can suggest you another approach.
NOTE: Ooops! You've already said what's your intention! Let me think for a while and soon I'll return with something.

EDITED: inputString is only updated one frame after the key is actually pressed, thus using GetKey functions is somewhat complicated. But there's a simple solution for this case: when inputString isn't empty, compare its value to the last key pressed: if it's the same, check the time elapsed since last key - if it's lower than some predefined limit, you have a double click:

 var tDoubleKey: float = 0.25;
 private var lastKey: String;
 private var timer: float;

 function Update(){
     if (timer > 0){ // if timer > 0, decrement it
         timer -= Time.deltaTime;
     }
     var key = Input.inputString; 
     if (key != ""){ // if any key pressed check double click:
         // if key = last key and double click time not elapsed...
         if (key == lastKey && timer > 0){
             // we've got a double click!
             print(key+" double-pressed");
         } else { // otherwise update lastKey and restart timer
             lastKey = key;
             timer = tDoubleKey;
         }
     }
 }

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 Bunny83 · Dec 08, 2012 at 11:58 PM

You could use OnGUI to detect keyinput, however, like i said somewhere here, OnGUI wraps the OS input and when you hold down a key it will be repeated automatically at the repeat-rate configured in your OS. So you have to detect both, KeyDown and KeyUp

 var key : KeyCode;
 var releasedOnce = false;
 
 function OnGUI()
 {
     var e = Event.current;
     if (e.type == EventType.KeyDown)
     {
         if (!releasedOnce)
         {
             key = e.keyCode;
         }
         else if (key == e.keyCode)
         {
             releasedOnce = false;
             Debug.Log("You pressed " + key + " two times in a row");
         }
         else
         {
             releasedOnce = false;
             key = e.keyCode;
         }
     }
     else if (e.type == EventType.KeyUp && e.keyCode == key)
     {
         releasedOnce = true;
     }
 }

Can't guarantee that there are no syntax errors. I wrote this from scratch and usually i don't use UnityScript.

edit
This does also work:

 var key : String = "";
 
 function Update()
 {
     var keyDown = Input.inputString;   
     if (keyDown != String.Empty)
     {
         key = keyDown;
     }
     if(key != "" && Input.GetKeyDown(key))
     {
         Debug.Log(key+ " pressed twice ");
         key = "";
     }
 }

The problem with InputString is that it is filled one frame after the actual keypress occurrs AND is only set for one frame. It's automatically cleared after that frame.

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 Fattie · May 14, 2013 at 10:30 AM 0
Share

it's a great pity you cannot get unique EventType.$$anonymous$$eyDown in OnGUI ... typically when writing for a finger device, you have stupid #if UNITY_EDITOR code so that it works to some extent on the bench. It's annoying that you cannot just simply get "the key just pressed, once" in OnGUI. As far as I know there's no simple way to say in OnGUI "key X was just pressed once"

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

12 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

Related Questions

how can i use getkey and getkeydown for two diffrent functions. 2 Answers

Play half of Animation on KeyDown/Other Half on KeyUp 1 Answer

When holding down shift cant use the "a" key, an extremely weird bug! 2 Answers

Canvas is preventing Input.GetKeyDown from working. 0 Answers

Simple Quick Question 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