Search Unity

Bug Input.GetKeyDown occurs multiple times in low frame rate

Discussion in 'Editor & General Support' started by modernator24, Jun 15, 2020.

  1. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    Here's the simple code that log message "Pressed" when start pressing Q Button:

    Code (CSharp):
    1. void Update() {
    2.     if(Input.GetKeyDown(KeyCode.Q)) {
    3.         print("Pressed");
    4.     }
    5. }
    In the low frame rate, I can see Pressed keep wrote in the console even I never release the button. According to the doc, InputGetKeyDown must return true when the user start pressed a button.

    Here's the video to demonstrate:


    This is a very critical issue in my game. What causes this and how do I fix it?
     
    Last edited: Jun 15, 2020
  2. Yes, and it is working for me. in 2019.LTS. Are you absolutely sure you haven't written one GetKey accidentally somewhere?
     
  3. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    @Lurking-Ninja Yep, absolutely sure. I also tried different keys(which is not used totally in my game) but all the same.
     
    Last edited: Jun 15, 2020
  4. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    Oh well, there's a typo. It should be "In the low frame rate," not "In the high frame rate".
     
  5. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    I just tested with mouse input:

    Code (CSharp):
    1. void Update() {
    2.     if(Input.GetKeyDown(KeyCode.Mouse0)) {
    3.         print("Pressed");
    4.     }
    5. }
    And looks like it's working fine. It only happens with keyboard input.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Is it possible this is an OS-level thing, not a Unity thing? If you go to a keyboard event tester like: https://unixpapa.com/js/testkey.html does it happen there? Is this just key repeat?
     
  7. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    This is what I get:

    Code (CSharp):
    1. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    2. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    3. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    4. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    5. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    6. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    7. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    8. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    9. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    10. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    11. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    12. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    13. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    14. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    15. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    16. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    17. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    18. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    19. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    20. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    21. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0      
    22. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0      
    23. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    24. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0        
    Is there any problem with it?
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Were you just holding the key down without releasing it? If so, something's up with your keyboard. For me I just get this:
    Code (CSharp):
    1. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    2. keypress keyCode=113 (q)   which=113 (q)   charCode=113 (q)
    3. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    4. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    5. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    6. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    7. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    8. ...
    9. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    10. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    11. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    12. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    13. keydown  keyCode=81  (Q)   which=81  (Q)   charCode=0    
    14. keyup    keyCode=81  (Q)   which=81  (Q)   charCode=0    
    Not sure why you're getting KeyUp events
     
  9. If you have low framerate during this, it is possible that it is the OS's keyboard puffer coming in.
     
  10. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    202
    Then is it impossible to fix this problem?
     
  11. It is outside of my knowledge, sorry. Maybe @Rene-Damm could tell you something clever about it.
     
  12. DarkSlashHero

    DarkSlashHero

    Joined:
    Apr 22, 2019
    Posts:
    1
    [x_x] - Same trouble