We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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.
http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
TfGuy44, that works only with a small subset of keys.
GoToLoop, that fails on W A S and D keys.
switch () / case:
checks each value. :-\"switch () / case:
w/ some regularif () else
block instead.Ah. You tricked me! :)
It does. Thanks!
Related, see also the generalized hashmap method:
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?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.
Then it would be nice to have the range made available to the program as manifests.
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.
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G
That program uses 0500 ... without citing a source.
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.
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.
I think there's a range that's its not secret but not easily determined.
It is to avoid relying on the values that I'd like a constant that provides the range.
Referring to
?
Well then they would just up the value of the range-top constant.
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.