Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to overwrite system's Input delay for held down key?

Discussion in 'Scripting' started by guitarxe, Jul 16, 2014.

  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    I want to shoot some bullets while holding down spacebar. On Windows, there is a configurable option for a "Repeat Delay" when holding down a key.
    So the signal for the input goes something like this:
    Input > delayyyyyyy> Input> Input> Input> Input> Input> etc

    For my game I want there to be no delay, and instead work like this:
    Input> Input> Input> Input> Input> Input> etc

    Since this is something controlled by the operating system, I don't suppose there is any way to overwrite this behaviour while the game is running?

    I know in many games this system behaviour is ignored, and I'm guessing they use a more direct route to the hardware keyboard. But is there a way to do this in Unity?
     
  2. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If you use GetButtonDown or GetKeyDown, they return true as long as the key is held down, so the system delay or lack thereof isn't relevant. Actually, I'm not sure what sort of input code you could even do in Unity where the system delay would have any effect.

    --Eric
     
    Joe-Censored likes this.
  4. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Well, that is what I'm using, and I see the effect of the system's repeat delay.

    Try it:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class temptest : MonoBehaviour
    5. {
    6.     private float time;
    7.     void Update()
    8.     {
    9.         time += Time.deltaTime;
    10.         if (Input.GetKey(KeyCode.Space))
    11.         {
    12.             Debug.Log(time);
    13.         }
    14.     }
    15. }
    Run and hold down space - watch the console:
    Code (csharp):
    1. 0.5369185
    2. 1.033918
    3. 1.057661
    4. 1.091631
    Notice the delay between the first and second, and then after the second the delay is miniscule.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No, GetKey returns true every frame as long as the key is held down. It doesn't return true/false/true/false or anything. Or if it does, then something is quite strange on your system.

    Code (csharp):
    1.         if (Input.GetKey(KeyCode.Space))
    2.         {
    3.             Debug.Log(time);
    4.         }
    5.         else
    6.         {
    7.             Debug.Log("no key");
    8.         }
    As long as the space key is held, it never prints "no key" here, only the time.

    In any case, I'd recommend GetKeyDown if you're implementing some kind of auto-fire.

    Code (csharp):
    1. if (Input.GetKeyDown (KeyCode.Space)) {
    2.     InvokeRepeating ("Fire", .01, repeatRate);
    3. }
    4. else if (Input.GetKeyUp (KeyCode.Space)) {
    5.     CancelInvoke ("Fire");
    6. }
    --Eric
     
    SnowFireBlues likes this.
  6. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Hm, it looks like I only experience this problem when I use Teamviewer on my Laptop to connect to my PC at home. Doing this directly on the PC does not have this problem. Oops. My bad! :D
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I would guess in that case it's actually sending key up/down events repeatedly when you hold a key down. In which case the GetKeyDown/Up code wouldn't work either. Actually nothing would work. ;)

    --Eric
     
  8. Battenberg-Software

    Battenberg-Software

    Joined:
    Nov 1, 2013
    Posts:
    15
    I've also come across this problem in a Linux build of one of our games - a user has reported that while attempting to move the main character using the left/right arrow keys, the movement is slow. On further investigation, I can see that the character moves one frame to begin with, then there is a half-second delay, then movement is on/off after that. So basically identical to a key being held down when typing.

    Is it the case that the operating system is sending Unity key up/down events periodically, and Unity has no way of knowing that the key is being held down? Or is there anything that can be done about this?

    I'm using Unity 5.6.0 and Input.GetKey() only to detect keyboard input.
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    See if you can create a small reproducible test case (including the behavior with GetKeyUp and GetKeyDown, in case it behave as described above), and then report it as a bug to Unity. I mean, a big part of me wants to just blame Linux and call it a day, but Unity needs to know that this happens, and I'm pretty sure they have less testing resources for input-related stuff dedicated to Linux than other platforms (it's largely expected that Unity for Linux is largely running game servers, I think).
     
  10. Battenberg-Software

    Battenberg-Software

    Joined:
    Nov 1, 2013
    Posts:
    15
    Sadly I can't, because it doesn't happen on our test machines. Or on 99% of systems, Linux or otherwise by the sound of it. It's literally happened for one user out of hundreds, and I only detected it because he was kind enough to send me a video where I could see this occurring.

    It may not even be a Unity problem, although he did say other games worked fine.

    I'm fairly new to forums/bug reporting so I'm not sure if this description alone will be enough to report a bug - obviously under normal circumstances I would happily take the time to create a project to reproduce it. I'll read up on bug reporting and see what I can do.
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Well, a bug report with anything about that user's Linux setup that he's willing to share (hardware, Linux distro/version, etc) would be helpful to narrowing down the problem, at least.
     
  12. Battenberg-Software

    Battenberg-Software

    Joined:
    Nov 1, 2013
    Posts:
    15
    I agree, and I've reported it and attached a sample project that works on my system but won't work on an affected one, much like guitarxe's example above. We'll see what happens.
     
  13. offmonreal

    offmonreal

    Joined:
    Mar 23, 2018
    Posts:
    10
    Please see if I'm doing the right thing. My task is to show the canvas while the key is pressed.
    Code (CSharp):
    1.  
    2. private bool show_canvas = false;
    3.     void Update()
    4.     {
    5.         if (!show_canvas && Input.GetKeyDown("tab"))
    6.         {
    7.             Debug.LogError("DOWN");
    8.              chageMode();
    9.         }
    10.        
    11.         if (show_canvas && Input.GetKeyUp("tab"))
    12.         {
    13.            Debug.LogError("UP");
    14.             chageMode();
    15.         }
    16.          
    17.     }
    18.  
    19.     void chageMode()
    20.     {
    21.       show_canvas = !show_canvas;
    22.       Debug.LogError("Canvas status: " + show_canvas);
    23.     }
    This code works well only in Unity Editor but not in the final build. I use Linux.
     
  14. SnowFireBlues

    SnowFireBlues

    Joined:
    Feb 6, 2017
    Posts:
    1
    I had been digging around for something like you just posted. I didn't know "InvokeRepeating" and "CancelInvoke" were a thing. Thank you soo much