detecting mouse movement and keypresses at the same time

Hello. Total newby here.
Let’s say I want the horizontal movement of the mouse do something if the ‘A’ key is being pressed and do another thing if the ‘B’ key is being pressed? How could I do that?

I know there are the event driven mouseMoved and keyPressed methods, but how can I check for a key inside the mouseMoved?

Thank you.
HEMOglobina

You can record the starting time each time the mouse moves, and on key press check if the elapsed time is within the threshold.

Thanks for the fast reply Arthur. I was wondering if there wasn’t an easier way, since what I want to do is not complex at all. Here is a dummy code:

  
  
void testApp::mouseMoved(int x, int y ){  
	if (theKeyPressedRightNow == 'a') {  
		// do something with the mouseX  
	} else {  
		// do something else with the mouseX  
	}  
}  
  

The keys would act as modifiers to the mouseMoved.
All the best,
HEMOglobina

That makes sense.

the way to do what you want is keep track of what keys are pressed using booleans in keyPressed and keyReleased.

  
  
boolean keyUpIsDown = false;  
boolean keyUpAIsDown = false;  
  
void keyPressed(int key) {  
   switch(key) {  
      case OF_KEY_UP:  
         keyUpIsDown = true;  
         break;  
  
      case 'a':  
         keyAIsDown = true;  
         break;    
   }  
}  
  
void keyReleased(int key) {  
   switch(key) {  
      case OF_KEY_UP:  
         keyUpIsDown = false;  
         break;  
  
      case 'a':  
         keyAIsDown = false;  
         break;    
   }  
}  
  

or a simpler way (which is what I usually prefer) is just keep an array of all keys

  
  
boolean keys[256];  
  
void keyPressed(int key) {  
   keys[key] = true;  
}  
  
void keyReleased(int key) {  
   keys[key] = false;  
}  
  
  

Note that many keyboards can get locks when too many keys are pressed or released at the same time, so you may end up with stray keys which appear to be pressed when they are actually not being held down.

note this wont work with extended / special keys

Thanks a lot Memo. That will surely do it.
All the best,
HEMOglobina