MrVimes
MrVimes

Reputation: 3312

How can I check keyboard key state within a mousemove event

I want to be able to use the space key to modify the behavior of the mouse while it is held down. Without knowing better I am imagining it involves some kind of coordination between two (or three) event handler - mousemove, keydown, and keyup. But I am wondering if there is some way to handle it entirely within one event handler - mousemove.

Example code to give an idea of what I hope to be able to do...

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (Keyboard.KeyDown == Keys.Space)
                {
                    /* Do modified behavour for left mouse being held down while 
                    space is also held down */
                }
                else
                {
                    // Do normal behavour for left mouse being held down
                }

            }
        }

Is something like this possible or will I have to save the state of the space key to a class variable using keydown event handler and check it with the mouse move handler?

Upvotes: 3

Views: 6790

Answers (4)

OldFart
OldFart

Reputation: 2479

You could use PInvoke to call GetKeyState in User32.dll.

Upvotes: 0

ncite
ncite

Reputation: 563

IT could be done using Control.ModifierKeys & Control.MouseButtons. But only works for kays like shift, ctrl and alt.

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {

        if ((Control.ModifierKeys & Keys.Shift) != 0)
        {
            if ((Control.MouseButtons & MouseButtons.Left) != 0)
            { // here you go

            }
        }
    }

Upvotes: 7

Bob.
Bob.

Reputation: 4002

I'd have a variable that tracks when Keys.Space is in the Pressed state, then trigger it with KeyUp and KeyDown events

 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
      // Check if Key.Space pressed
      if(SpacePressed) {
           // Do something
      }
 }

 private void KeyPressed_Event(object sender, KeyEventArgs e) {
      // Check if Key.Space pressed
      if(e.Key == Key.Space) {
           SpacePressed = true;
      }
 }

 private void KeyRelease_Event(object sender, KeyEventArgs e) {
      // Check if Key.Space pressed
      if(e.Key == Key.Space) {
           SpacePressed = false;
      }
 }

Upvotes: 1

Link
Link

Reputation: 1711

You should set a variable in your KeyDown-Event and check it in your MouseEvent:

bool buttonpressed = false;
private void KeyDown_Event(object s, System.Windows.Forms.KeyEventArgs e)
{
   if(e.KeyCode == KeyCode.Space)
      buttonpressed = true;
   else
      buttonpressed = false;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (buttonPressed)
                {
                    /* Do modified behavour for left mouse being held down while 
                    space is also held down */
                }
                else
                {
                    // Do normal behavour for left mouse being held down
                }

            }
        }

Upvotes: 2

Related Questions