Quantcast
Channel: Questions in topic: "keyboard"
Viewing all articles
Browse latest Browse all 605

Unable to catch KeyCode.LeftShift Down AND Up

$
0
0
Hey everyone. This maybe something stupid in the end but I have spent several *days* trying to find a solution to this problem and have come up with nothing at all. Every search yields results for `Input.GetKeyDown(KeyCode.LeftShift)`, which appears to work just fine, but I can find no information about why `Input.GetKeyUp(KeyCode.LeftShift)` simply does not appear to work .

Here is my experimental Update function void Update () { bool isShiftKeyDown = false; if( Input.GetKeyDown(KeyCode.LeftShift) ){ isShiftKeyDown = true; } else if( Input.GetKeyUp(KeyCode.LeftShift) ){ isShiftKeyDown = false; } Debug.Log(isShiftKeyDown); } At this stage I have tried many, many permutations of this code with the same results: isShiftKeyDown will be false until I press the Shift key, at which point it changes to true as expected. However, releasing the key seems to do nothing.

Bizarrely it will work if I only try to catch `Input.GetKeyUp`, or even reverse the order of the Up/Down checks, but in either case the second check is never fired. I have now spent so long on this simple problem that I am no longer sure what is going on and I'm half convinced that I'm doing something strange, but there is nothing else in the project, just this code dropped on an empty game object.

Can anyone suggest how I can get the shift key to toggle the boolean using up and down key presses? BTW: I have of course tried removing the `GetKeyUpand` just putting an `Else` in its place. No difference.

**Update** Okay. So. Lots of discussion going on.
*The boolean in the Update function*: Yes, I know. I put it there just to show what was going on. I tried with it, without it, no difference.

However...

Having tried this code (and many permutations of it) in multiple projects, all with the same quite bizarre results I have now re-installed Unity (2018 again) and re-loaded 3 of those projects in turn, and in *every single case the code works as expected*!

I have no ideas or answers for this. To my knowledge every other aspect of Unity seemed to be fine, with this strange exception, caused only because I wanted to use the shift key! I have now checked 3 projects that previously failed, and created another two fresh ones, and everything works fine.

Not sure what to say about this.

**Update** (Again)

Okay, so. I uninstalled Unity entirely and re-installed (Still the latest version), and I have now been using it for development (rather than just projects to test code) and so far everything has continued to work fine.

Bunny83
Yes, I understand what GetKeyDown and GetKeyUp actually do, thanks, and as already mentioned I am fully aware that the bool is in Update in the code above. Perhaps you should not post an answer when you don't answer the question. If you have a comment it may be an idea to use the comment function.

@JVene
I'm still not sure why this problem occurred and have now tried many permutations of the debug code in order to catch changes without running Debug.Log in the Update (Yes, it does slow things down quite considerably) and I consistently got the same strange results.

Yes, Sticky keys. Hideous thing but we have to live with it. I have it disabled entirely (Holding any of those keys does not 'do' anything) and if it does trigger it makes an unpleasant noise so I think we can rule that out.

As for the OS and the queued button presses, I agree that this could cause a problem, however that does not explain why identical code (the same test projects) now work in a fresh installation of Unity, so I suspect that something, somewhere along the line got broken or corrupted in the Unity installation.

I cant see *what* could have had such a bizarre effect on Unity, but in any case this problem appears to be resolved, although without any satisfactory answer.

Many thanks to all who contributed ideas. A re-install of Unity fixed it, I just wish we had a concrete reason for the problem in the first place.


Viewing all articles
Browse latest Browse all 605

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>