We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I'm working on my own multi-keystroke detection system but in order to clear out the cache I need to compare str(key) with an array list of items appended using the function str(key).
Unfortunately, I cannot figure out why it won't detect a match when comparing the two. Does anyone know how to solve this? To clarify, I am looking for a solution to my code or an explanation why it won't work if possible, thanks!
This is my first post with code, so let me know if I need to change anything, also I have a couple of other things in there from when was experimenting with different solutions, none worked and I'm out of options:
String[] keys = {};
String[] shortkeys = {};
int d = 0;
void setup(){
frameRate(60);
}
void keyPressed(){
keys = append(keys, str(key));
}
void keyReleased(){
for(int i = 0; i < keys.length; i++){
if(keys[i] == str(key)){
keys[i].replace(keys[i], null);
}
}
}
void draw(){
shortkeys = new String[0];
if(keyPressed == false){
keys = new String[0];
}
if(keys.length != 0){
for(int i = 0; i < keys.length; i++){
print(keys[i]);
print("-");
print(d);
print("-");
print(keys.length);
print("-");
}
}
}
Answers
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G
Thanks, used the code to fix my own and ended up with this, any feedback, warnings & other info would be appreciated!
For
boolean
types, there's no need to explicitly check for eithertrue
orfalse
: O:-)Just this is enough:
if (keysDown[i]) {
:ar!thanks, wasn't too sure about that one thanks for clearing it up :)
I noticed you are very good at compressing multiple parts/statements of code into one line & that's something I've always wanted to learn. Would you be able to give me any pointers to a tutorial that covers it well?
I don't think there are any tutorials for it. Just dive deep into a language's syntax. ^#(^
Will do. Ty for the help! :)
Or better don’t.
To compress code doesn’t give you more speed it just decreases readability and maintainability of the code.
readability and maintainability is the main point of good code. When somebody else or you in a few months needs to read or change your code, he (or your future self) will be grateful for clear, longer and explicit code.
Of course, I don't mean the
== true
example.To transform this
if (keysDown[i] == true) {
intoif (keysDown[i]) {
is fully ok.Chrisir ;-)