how do you tell if a key is pressed in mouse event?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Say I want to do a specific action if the user holds down the CTRL key while
clicking something in my UI- how can I determine if this key is currently
pressed within the mouse event?
 
Say I want to do a specific action if the user holds down the
CTRL key while clicking something in my UI- how can I determine
if this key is currently pressed within the mouse event?

One way would be to use three events (OnKeyDown, OnKeyUp, and
OnMouse*), and a boolean class-wide variable.

Here's some pseudocode:


// Class-wide variable
private bool isControlKeyPressed = false;

OnKeyDown event:
if control key is pressed:
this.isControlKeyPressed = true;

OnKeyUp event:
if control key is being released:
this.isControlKeyPressed = false;

OnMouse* event (where * = enter, leave, move, etc...):
if this.isControlKeyPressed:
// do control key-related processing here.
 
MrNobody,

I am guessing that you want a way for draging controls (or whatever) over
your screen and therefore simple told.

Normally you set therefore a switch as the mousedown event occurs which you
set to off when the mouse up event occurs.

Than you can see in the mousemove event that it is down, and you can move an
object over your screen.

Just guessing,

Cor
 
Hi MrNobody,

A Control has a handy static property called ModifierKeys that you can tap
into at any time.
So, in your mouseevent check

if((Control.Modifierkeys & Keys.Control) > 0)
//Ctrl is in a pressed state

Or if you only want to allow ctrl without any alt or shift use ==
 
Back
Top