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 & HelpPrograms › toggle normal buttontype on/off switcher
Page Index Toggle Pages: 1
toggle? normal buttontype? on/off switcher (Read 2253 times)
toggle? normal buttontype? on/off switcher
Jun 2nd, 2005, 2:55am
 
How can I code something like a normal on/off switcher?
I know now with the help of the kind people in this forum how to use the keys of my powerbook-keyboard, but I want to be able to use them like regular buttons. So when I push once the key 'a' then processing should do: blabla (A) and when I push it again it should do blabla (B) and then when I push it should do again the same like (A). I need this for communicating with SC3 via OSC.

--

void aaOscMessage() {
       OscMessage oscMsg = oscP5.newMsg("/windstop");
       oscP5.sendMsg(oscMsg);
}

void abOscMessage() {
       OscMessage oscMsg = oscP5.newMsg("/windstop");
       oscP5.sendMsg(oscMsg);
}

void keyPressed()
{
 switch(key){
 case 's':
// here I need something .... Smiley if u push it the first time do
aaOscMessage();
// if u push it the second time do
abOscMessage();
// set counter to Zero? So next time is again first time??
   break;
}
}

Re: toggle? normal buttontype? on/off switcher
Reply #1 - Jun 2nd, 2005, 3:44am
 
If I'm reading you right, you need a boolean switch? Something like this?

Code:

boolean button;

if(button)
button=false;
else
button=true;



If that's the case, you can write a toggle function for a boolean like so:

Code:


boolean toggle(boolean b)
{
if(b) return false; else return true;
}
//using it would look like

button=toggle(button);


Re: toggle? normal buttontype? on/off switcher
Reply #2 - Jun 2nd, 2005, 9:05am
 
Here's a way of programming a key toggle function using keyPressed() and released().

Code:

void draw() {

if(pause) {return;}

else {
do stuff....
}

}

boolean pause = false;
boolean ptoggle = false;

void keyPressed() {

if(key == 'p' && !ptoggle) {pause=!pause; ptoggle=true;}

}

void keyReleased() {

if(key == 'p') {ptoggle=false;}
}


Re: toggle? normal buttontype? on/off switcher
Reply #3 - Jun 2nd, 2005, 2:10pm
 
thank u for the answers!
But sorry mflux I dont understand this!
Where is the key Part and where is the "do blabla when A and do bla bla when B"-part? Or could u explain me how to use it?

also thanks to Mark Hill
But in this way it sends continiously OSC Messages to SC3 (because it is in the draw void, I suppose?)...

So the action has to be done just once.
I am looking for a way to programm this kind of thing:

push and release a key of ur pc/mac keyboard once (and/or for the first time)
then do this=A:  // just once
OscMessage oscMsg = oscP5.newMsg("/wind");
oscP5.sendMsg(oscMsg);

if u push and release the same key again
then do this=B: (not the same message) // just once
OscMessage oscMsg = oscP5.newMsg("/windstop");
oscP5.sendMsg(oscMsg);

if u push and release again then do A.






Re: toggle? normal buttontype? on/off switcher
Reply #4 - Jun 2nd, 2005, 4:17pm
 
Switching background on and off, with keyreleased:
Code:

boolean showbg = false;

void draw()
{
if(showbg) background(0);
else background(255);
}

void keyReleased()
{
switch(key){
case ' ':
if(showbg) showbg = false;
else if(!showbg) showbg = true;
break;
}
}


off the top of my head, so might be some typos..
-seltar
Re: toggle? normal buttontype? on/off switcher
Reply #5 - Jun 6th, 2005, 12:09pm
 
thank u all.
is it not possible to do it without the draw void?
I will try to code this in SC3 (=Supercollider) maybe.
...
thank u anyway.
Page Index Toggle Pages: 1