I am putting together a Processing sketch where users input text via the keyboard, text is saved to a text file and printed, and later it's manipulated and saved as PNG to a Tumblr account. I had a keyPressed event initialising saving as text file etc. When users hit the CONTROL key it would do its thing and cycle through to the next screens.
I wanted to change this to using the RETURN or ENTER key, as I thought it would be far more intuitive. Since RETURN is not a coded key, I took out the part to check is key pressed is coded, and since then every key I type is displayed twice, but RETURN works to save text file and advance etc.
If I leave in checking key is coded. Text displays normally, but hitting the RETURN key doesn't initialise saving as text file etc. This is probably something really simple, and I've been looking at it for too long - but I really can't see where the problem is. Any help would be gratefully appreciated. Thanks.
Relevant bit of code below:
void keyPressed() {
if (keyCode == BACKSPACE) {
if (yourText.length() > 0) {
yourText = yourText.substring(0, yourText.length()-1);
}
} else if (keyCode == DELETE) {
yourText = "";
} else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != RETURN && keyCode != ENTER && keyCode != ALT) {
myText = "";
yourText = yourText + key;
}
// If the Return key is pressed save the String and write it to text file
//if (key == CODED)
//{
if (key == RETURN || key == ENTER) {
savedText = yourText;
textFile = createWriter("stories/"+timestamp()+".txt");
textFile.println(savedText);
textFile.flush();
textFile.close();
rect (0,0,width, height); //PROBLEM sometimes visible when screen is switched.
noStroke ();
currentScreen++;
if (currentScreen > 2) { currentScreen = 0; } //switches to next screen
}
else {
// Otherwise, concatenate the String
// Each character typed by the user is added to the end of the String variable.
yourText = yourText + key;
}
//}
}
The short answer to your problem is that you should use the key
variable to check for BACKSPACE
, TAB
, ENTER
, RETURN
, ESC
, and DELETE
, not the keyCode
variable like you're doing.
For a longer explanation, here's a simplified MCVE of your program:
void draw() {
background(0);
}
void keyPressed() {
if (keyCode == BACKSPACE) {
println("1: backspace");
} else if (keyCode == DELETE) {
println("2: delete");
} else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != RETURN && keyCode != ENTER && keyCode != ALT) {
println("3: " + key);
}
if (key == RETURN || key == ENTER) {
println("4: return");
} else {
println("5: " + key);
}
}
Run that and you'll see a similar problem, and you'll see why it's happening: You're entering two separate conditions for most of the keys, because your logic for determining different keys is wrong.
The first half of your logic goes something like this:
Is the keyCode BACKSPACE? If so do something.
Otherwise, is the keyCode DELETE? If so do something.
Otherwise, is the keyCode ANYTHING ELSE other than SHIFT, CTRL, RETURN, ENTER, or ALT? If so do something.
The problem is, any uncoded key is going to enter that 3rd else/if statement.
The second half of your logic then prints out the value of the key
variable, even though you already handled it in the above if statement.
The solution is to correctly differentiate between coded and uncoded keys, then use the key
variable to check for values such as ENTER
, RETURN
, and BACKSPACE
:
void draw() {
background(0);
}
void keyPressed() {
if (key==CODED) {
if (keyCode == SHIFT){println("shift");}
else if(keyCode == CONTROL){println("ctrl");}
else if(keyCode == ALT) {println("alt");}
} else {
if (key == BACKSPACE) {
println("1: backspace");
} else if (key == DELETE) {
println("2: delete");
} else if (key == RETURN || key == ENTER) {
println("4: return");
} else {
println("5: " + key);
}
}
}
For more info, always check the reference.