We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Keys (Read 939 times)
Keys
Oct 5th, 2009, 9:10pm
 
i'm having troubles with my program here.

instead of having if(key=='q') etc. how would i do it so i would have to type Quit or quit for it to exit instead of "q"?


Thanks!

Re: Keys
Reply #1 - Oct 5th, 2009, 11:43pm
 
you must be a linux user ;)

I would do it like this: keep a buffer of the last four keypresses.

array[] keyBuffer = new char[4];

every time a key is pressed, shift all the characters in the keyBuffer and add the last keyPress to the end.

void keyPressed(){
 for (int i=0;i<3;i++){
   keyBuffer[i] = keyBuffer[i+1];
 }
 keyBuffer[3] = key; // add the last keypress to the buffer
 checkBuffer();
}

Now you just a checkBuffer() function to see if the keyBuffer spells "quit."  Helpful commands here will be String.toLowerCase(someString), string += char, and/or Character.toLowerCase(someChar)...

--Ben
Re: Keys
Reply #2 - Oct 6th, 2009, 2:22am
 
You might also want to filter out special keys like Shift since they are registered as a key press and could obscure the correct string in the buffer.  Also, there were a couple of relevant threads posted a few months ago: Might be worth doing a search as there was some good advice posted IIRC.

Actually here they are:
http://processing.org/discourse/yabb2/num_1244664218.html

http://processing.org/discourse/yabb2/num_1223627482.html

I'm feeling nice today Smiley
Re: Keys
Reply #3 - Oct 6th, 2009, 10:19am
 
good point, hadn't thought of that -- the shift key will be a ? mark in the buffer...rather than try to catch all coded keys, I would do this:
Quote:
String alphabet = "abcdefghijklmnopqrstuvwxyz";

boolean validKey(char key){
 for (int i=0;i<alphabet.length();i++){
   if (Character.toLowerCase(key) == alphabet.charAt(i)) return true;
 }
 return false;
}

just because then you know exactly what can go into your buffer -- no nasty surprises.       --Ben
Re: Keys
Reply #4 - Oct 6th, 2009, 8:00pm
 
alright i found out that i have to use "String" Syntax. but i have no idea how to do it =\ any ideas?!?!
Re: Keys
Reply #5 - Oct 6th, 2009, 8:12pm
 
Never mind thanks guy! i found what i had to use!

if(sBuffer.contains("quit")&&key==ENTER)
{
 exit();
}
Page Index Toggle Pages: 1