How to detect whether a given key is down, even when another is.

edited January 2018 in How To...

Anyone know the answer to this?

It was asked before https://forum.processing.org/one/topic/detect-what-keys-are-currently-held-down-multiple-keys-held-down-at-once.html but the response did not help.

Tagged:

Answers

  • An example I posted in this thread:

    https://forum.processing.org/two/discussion/25919/hello-i-want-my-object-to-be-mobile-using-the-keyboard#latest

    Also demonstrates a way to detect multiple key pressed.

  • TfGuy44, that works only with a small subset of keys.

    GoToLoop, that fails on W A S and D keys.

  • edited January 2018 Answer ✓

    ... that fails on W A S and D keys.

    • Only when running it online due to the strictly way JS' switch () / case: checks each value. :-\"
    • Copy, paste & try it out under PDE's Java Mode and everything's gonna work. :bz
    • Or replace switch () / case: w/ some regular if () else block instead.
    • So it works 100% both offline (Java Mode) & online (Pjs)! *-:)
  • Only when running it online

    Ah. You tricked me! :)

    Copy, paste & try it out under PDE's Java Mode and everything's gonna work.

    It does. Thanks!

  • Thanks Jeremy. I've gone with GTL's code but if I find problems will try yours.

  • They are basically the same approach -- track each key with a Boolean.

    If you are going to do a LOT of different keys, then the map just saves you from having to create a different boolean variable for each one -- it accepts any key input and maps that to a unique Boolean.

  • Thanks. BTW, given the docs do not say the range of values for keyCode https://processing.org/reference/keyCode.html , can you tell me? I assume it is an int.

  • Well, keyCode is an int. As to the range / mapping of values, I think it is in the Java documentation?

  • I think it is in the Java documentation?

    You'd think! :)

  • The documentation lists virtual key code constants -- and everything else is (potentially) pass-through or left undefined / up to the specific platform / OS. If you are trying to do something that isn't mappable to a unicode character and isn't in the virtual key constants list, then what happens may be MacOS / Windows specific -- or nothing.

  • everything else is (potentially) pass-through or left undefined / up to the specific platform / OS.

    Then it would be nice to have the range made available to the program as manifests.

    If you are trying to do something that isn't mappable to a unicode character and isn't in the virtual key constants list

    Well, I don't know what values are in the virtual constants list... without printing them all ! :)

    I was planning myarray[keyCode] and seeking the value to put in 'new boolean[?]'.

    But I guess that's unavailable. Thanks.

  • That program uses 0500 ... without citing a source.

  • edited January 2018

    It seems like you think there is an official range which is a secret. I don't think there is; there may happen to be a range in the implementation, but it is an implementation detail and has been intentionally left undefined in the API documentation.

    WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants. Sun reserves the right to change these values as needed to accommodate a wider range of keyboards in the future.

    That's pretty clear. You can hack together something int-range-based if you want, but it is a hack, and could break at any time -- what you should probably do is just build an array / map / dictionary / whatever of the known constants (built with the constant keywords, not a list of ints) as listed plus anything else you want, and then draw from that list.

  • It seems like you think there is an official range which is a secret.

    I think there's a range that's its not secret but not easily determined.

    do not rely on the values of the VK_ constants.

    It is to avoid relying on the values that I'd like a constant that provides the range.

  • edited January 2018

    a wider range

  • edited January 2018

    Referring to

    Sun reserves the right to change these values as needed to accommodate a wider range of keyboards in the future.

    ?

    Well then they would just up the value of the range-top constant.

  • edited January 2018

    Not necessarily. You would like them to do that, and they probably should do that -- but they reserve the right not to do that. That's (one reason) why they haven't written down an official range. If you treat those values as a range, they reserve the right to break your code in the future. Then they will say "we told you not to do that."

    If you understand this, then you understand why people who don't want their code to break in the future are giving you good advice when they see situations like this and say: don't do that.

  • If they provided an accurate top-range constant then a program that used it would not break when they changed the range.

Sign In or Register to comment.