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.
IndexProgramming Questions & HelpSyntax Questions › detecting key combinations
Page Index Toggle Pages: 1
detecting key combinations (Read 1770 times)
detecting key combinations
Oct 1st, 2005, 8:00pm
 
small processing program for detecting key combinations - code is basically copied from the sun keylistener tutorial but thought it might help some processing users to know that this works inside processing :


void setup(){
 size(200,200);
 background(255);
}

void keyPressed(KeyEvent e) {

   String keyString;
   int id = e.getID();
   if (id == KeyEvent.KEY_TYPED) {
     char c = e.getKeyChar();
     keyString = "key character = '" + c + "'";
   }else {
     int keyCode = e.getKeyCode();
     keyString = "key code = " + keyCode
       + " ("
       + KeyEvent.getKeyText(keyCode)
       + ")" + " and keyChar = " + e.getKeyChar();
   }
   println(keyString);

   //

   int modifiers = e.getModifiersEx();
   String modString = "modifiers = " + modifiers;
   String tmpString = KeyEvent.getModifiersExText(modifiers);
   if (tmpString.length() > 0) {
     modString += " (" + tmpString + ")";
   }
   else {
     modString += " (no modifiers)";
   }
   println(modString
   
   //

   String actionString = "action key? ";
   if (e.isActionKey()) {
     actionString += "YES";
   }
   else {
     actionString += "NO";
   }

   println(actionString);

}
Re: detecting key combinations
Reply #1 - Oct 1st, 2005, 11:40pm
 
a couple of corrections/simplifications.. the id will never be KEY_TYPED because you're already inside a KEY_PRESSED event, so that can be removed.

also, with processing, the KeyEvent object is always available as "keyEvent", the getKeyChar() is already in the variable 'key', and getKeyCode() is int the variable 'keyCode'.

generally, it's better to use these variables, and not override keyPressed(KeyEvent e) because p5's version will properly queue calls to keyPressed() until it's safe to draw things to the screen.

so here's a simpler, p5-happy version:

Code:
void keyPressed() {
String keyString = "key code = " + keyCode +
" (" + KeyEvent.getKeyText(keyCode) + ")" +
" and keyChar = " + c;
}
println(keyString);

//

// warning, getModifiersEx() requires java 1.4
// (will break on mac with firefox or ie)
int modifiers = keyEvent.getModifiersEx();
String modString = "modifiers = " + modifiers;
String tmpString = KeyEvent.getModifiersExText(modifiers);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no modifiers)";
}
println(modString);

//

println("action key? " + (e.isActionKey() ? "YES" : "NO"));
}
Re: detecting key combinations
Reply #2 - Oct 3rd, 2005, 3:44pm
 
brilliant! thanks Ben.
Re: detecting key combinations
Reply #3 - Oct 3rd, 2005, 4:00pm
 
a couple of typos in your code ben.
fixed them in version of code below...

btw, what is the correct way to include code in posts, so it has the different colour background?

oh, and how could you test for alt,ctrl or shift key combos on mac in firefox or IE??


void keyPressed() {
 String keyString = "key code = " + keyCode +
" (" + KeyEvent.getKeyText(keyCode) + ")" +
" and keyChar = " + c;
 println(keyString);

 //

 // warning, getModifiersEx() requires java 1.4
 // (will break on mac with firefox or ie)
 int modifiers = keyEvent.getModifiersEx();
 String modString = "modifiers = " + modifiers;
 String tmpString = KeyEvent.getModifiersExText(modifiers);
 if (tmpString.length() > 0) {
   modString += " (" + tmpString + ")";
 } else {
   modString += " (no modifiers)";
 }
 println(modString);

 //

 println("action key? " + (keyEvent.isActionKey() ? "YES" : "NO"));
}
Re: detecting key combinations
Reply #4 - Mar 31st, 2010, 2:21am
 
copy-pasting the above code outputs this error :
Cannot find anything named "c"

just replace c with key at line 3, like this:

Code:

void keyPressed()
{
String keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")" + " and keyChar = " + key ;
println(keyString) ;

// warning, getModifiersEx() requires java 1.4
// (will break on mac with firefox or ie)
int modifiers = keyEvent.getModifiersEx() ;
String modString = "modifiers = " + modifiers ;
String tmpString = KeyEvent.getModifiersExText(modifiers) ;
if (tmpString.length() > 0)
{
modString += " (" + tmpString + ")" ;
}
else
{
modString += " (no modifiers)" ;
}
println(modString) ;

println("action key? " + (keyEvent.isActionKey() ? "YES" : "NO")) ;
}



thanks for the snippet guys !
Page Index Toggle Pages: 1