Reputation: 319
I am creating a game with Processing and want to input a username. I know it is not perfect (first time processing), I can write every letter but the backspace
does not work.
This is my Code:
void keyTyped() {
if(status("getUsername")) {
if(((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9') || key == ' ') && (key != CODED && keyCode != ENTER)) {
user.addLetter(key);
} else if(key == CODED && keyCode == BACKSPACE) {
user.remLetter();
}
System.out.println(user.getName());
}
}
User
class User {
String name = "";
void addLetter(char letter) {
this.name = this.name + letter;
}
void remLetter() {
if(name.length() != 0) {
System.out.println("rem");
name = name.substring(0, name.length()-2);
}
}
}
Input
Jon[BACKSPACE]
Ouptut
J
Jo
Jon
Does anyone know how I can solve this problem? Or how I could improve my code?
I tried to use the demo code from George Profenza
and it worked perfectly, but when I try to use this in my code, it does not work for an unknown reason...
Full code on: https://github.com/BeeTheKay/GameSnake.git
Upvotes: 1
Views: 846
Reputation: 51837
You might have missed a little gotcha using key
:
For non-ASCII keys, use the keyCode variable. The keys included in the ASCII specification (BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE) do not require checking to see if the key is coded, and you should simply use the
key
variable instead ofkeyCode
If you're making cross-platform projects, note that the ENTER key is commonly used on PCs and Unix and the RETURN key is used instead on Macintosh.
Simply change:
} else if(key == CODED && keyCode == BACKSPACE) {
to
} else if(key == BACKSPACE) {
Additionally, you might want to delete a single character instead two:
name = name.substring(0, name.length()-2);
simply changing it to:
name = name.substring(0, name.length() - 1);
Here's a basic demo sketch mostly based on your code:
User user = new User();
void setup(){}
void draw(){
background(0);
text(user.name,5,15);
}
boolean status(String s){
return true;
}
void keyTyped() {
println(key == CODED, key, BACKSPACE, key == BACKSPACE);
if(status("getUsername")) {
if(((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9') || key == ' ') && (key != CODED && keyCode != ENTER)) {
user.addLetter(key);
} else if(key == BACKSPACE) {
user.remLetter();
}
System.out.println(user.getName());
}
}
class User {
String name = "";
void addLetter(char letter) {
this.name = this.name + letter;
}
void remLetter() {
if(name.length() > 0) {
System.out.println("rem");
name = name.substring(0, name.length() - 1);
}
}
String getName(){
return name;
}
}
(In the future, try to make it easier for other to support you by posting snippets of code that compile)
Update I've tried the github repo posted above and there seems to be a bug with keyTyped() and the OpenGL based renderers (P2D
, P3D
):
P2D
, P3D
coded keys don't seem to trigger keyTyped()
, however keyPressed()
, keyReleased()
doJAVA2D
or FX2D
work as expected.This seems to be a known Processing issue, however it doesn't look like it will be fixed in the near future sadly.
Personally, this is the first time I hear of keyTyped()
after all these years playing with Processing :) I'm used to keyPressed()
/ keyReleased()
.
In the meantime you have at least two options:
keyTyped()
use size(600, 600, JAVA2D);
instead of size(600, 600, P2D);
(and hopefully the performance/rendering will still be good enough)P2D
, but simply move the logic you currently have in keyTyped()
inside keyReleased()
Upvotes: 2