Search Unity

Can I Loop Through Keys[] to Check If All Key Are Pressed Down?

Discussion in 'Scripting' started by joeyxiong1994, Jul 29, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    So I have a skill system, and each skill has different key combination to press in order to trigger that skill.

    For example, Dash() skill, I want to trigger it by press W key and LeftShift Key. Can I do things like below image. I loop through all keys to check all key got entered.


    upload_2019-7-29_15-15-27.png
    Explanation for this image: skillInterruptKeys are KeyCode[][], two dimensions array. Each time, I retrieve one KeyCode[] to Check if this combination is pressed all. And if there's a key in this key combination not pressed, it would not show all keys pressed.

    As so far, I have no test case, I wonder if this way is working from you guys' experience.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    From a quick look, it looks fine to me. Just so long as you expect all the keys to be held down at the same time. If you need keys to be pressed in order rather than all at the same time, then you'd need to design a different system.

    Note that if you're using the legacy input system and shift keys there is some weirdness to be aware of.
    https://forum.unity.com/threads/unity-3-5-7f6-dual-shift-key-bug.178942/#post-3890950
     
    joeyxiong1994 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Also beware that every different model of keyboard has different characteristics about how many keys can be simultaneously chorded. Sometimes it will jam (drop keys) and other times it might ghost keys.

    Here is a discussion:

    https://en.wikipedia.org/wiki/Rollover_(key)

    Short answer based on experience: don't rely on more than two keys at once being pressed.
     
    joeyxiong1994 likes this.