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 › CONTROL + char commands.
Page Index Toggle Pages: 1
CONTROL + char commands. (Read 733 times)
CONTROL + char commands.
Aug 8th, 2009, 11:48am
 
Hello everybody!

I'm developing this live visuals program in Processing, and I was curious about giving it some keyboard-stroke comands. Until now I have reserved lowercase letters for typing and uppercase letters for commands, so if I want to save a screenshot of the running program I have to hit SHIFT + S; on the other hand, a lowercase 's' would do nothing at all, so I can still use the keyboard to type some text to be displayed, but only in lowercase, which is annoing.

My question is:
Is there a way to do the same in Processing but with the CONTROL key?
I think it's better (and more intuitive) to save comands for the CONTROL key. I tried different (newbie) ways to do this, but none of them worked.

Any advice? Smiley

Thank you! Smiley
Re: CONTROL + char commands.
Reply #1 - Aug 9th, 2009, 6:46am
 
Did you check the reference and the keyCode variable?
http://processing.org/reference/keyCode.html

Looks like it can help you detect whether the user pressed the control key or not. You can store the state of the control key in a boolean :

Code:
boolean ctrlPressed;

void keyPressed() {
 // special key
 if (key == CODED) {
   if (keyCode == CONTROL) {
     ctrlPressed = true;
   }
 }
 // regular key
 else {
   if (ctrlPressed) {
     // CTRL + KEY
   }
   else {
     // OTHER
   }
 }
}

void keyReleased() {
 if (key == CODED) {
   if (keyCode == CONTROL) {
     ctrlPressed = false;
   }
 }
}
Re: CONTROL + char commands.
Reply #2 - Aug 20th, 2009, 2:00am
 
Nice Smiley

I think it should also work for

Code:
boolean ctrlPressed;

void keyPressed() {
 // special key
 if (key == CODED) {
   if (keyCode == CONTROL) {
     ctrlPressed = true;
   }
 }
 // regular key
 else {
   if (ctrlPressed && key == 'a') {
     //do something
   }
   else {
     // do another thing
   }... ... ...


does it?

I'll try your approach! thank you veru much! Cheesy
Re: CONTROL + char commands.
Reply #3 - Aug 20th, 2009, 3:08am
 
Looks like it won't work until you use keyCode instead of key :

Code:
if (ctrlPressed && keyCode == 65) {
 // do something
}
Re: CONTROL + char commands.
Reply #4 - Aug 22nd, 2009, 1:21pm
 
thank you very much!
It's working beautifuly Cheesy
Page Index Toggle Pages: 1