We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've setup a processing sketch that runs on a web server communicating with a php script.
Part of it involves typing input directly into the browser window - no forms.
It works, but my problem is - I cannot find a way to stop the typed input accepting spaces (which I do not want).
Here's the relevant bit of code for what I am trying to do:
void keyReleased() {
if (key == CODED) {
}
if (key != CODED) {
switch(key) {
default:
if (key== " "){
return;
}
if (key!=" ") {
typedEntry += str(key);
//up=false;
//down=false;
}
}
}
}
Answers
You're getting your control structures all mixed up. Do you want an if statement or a switch? I suggest you use an if statement:
void keyReleased() { if (key != CODED && key!=" ") { typedEntry += key; } }
Thanks!
Just tried that. (Also used str(key) so I don't get the ascii)
Maybe the problem is javascript, because I can still type a space with that....
Did it!
Here's the code that worked:
You made me realize the space was seen as ascii by the javascript :)
Oh man, my bad. I've been working in python a lot lately, and company policy is that all quotes are double quotes.
The trouble you were having is because " " is a string and ' ' is a space character.
void keyReleased() { if (key != CODED && key!=' ') { typedEntry += key; } }
There's no need to test for CODED! Just a redundant and visual pollution! :P
Also, w/ keyCode, we don't need to test for lowercase letters! o->
Thanks Guys - great input
The final code that works is this:
I have to use the str cast as otherwise the type comes up as ascii in the web browser (javascript).
I've not seen this in many references, and it had me stumped for a bit, so I include it here for people....
Cheers!
Actually, i found I need to keep the CODED, otherwise it messes up the deleting of characters (a sort of double pipe appears instead). Here's relevant bits of code....
//blinking cursor, typedEntry display
//code to delete typed input
//code to block space input
Indeed, since it's a text input program, testing for CODED is just an extra way to know whether we can actually use key variable.
Most of the time, I just use keyCode exclusively rather than key, b/c I'm usually not interested in lowercase letters!
I've got an online example which uses both keyTyped() & keyPressed().
Of course, I coulda used keyReleased() instead of keyPressed() too.
Inside keyTyped(), I'm not interested to continue if key = CODED. So I check for it to issue an early quit if it's so:
While within keyPressed(), it's the opposite:
In short, in some situations, testing for CODED is more like a shortcut; but not exactly essential: :-B
http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
Wow, that's really helpful.
I was just staggering around and testing what worked, that will help me immensely when I refine this - thank you sir.