1

According msdn documentations, in WinForms I can check if modifier keys (SHIFT, CTRL, and ALT) is in a pressed state, during mouse events. for example:

private void button1_Click(object sender, System.EventArgs e)
{
   /* If the CTRL key is pressed when the 
    * control is clicked, hide the control. */ 
   if(Control.ModifierKeys == Keys.Control)
   {
     ((Control)sender).Hide();
   }
}

How can I check if non modifier keys, such as Space are in pressed state, during mouse movement event for example?

Masoud
  • 8,020
  • 12
  • 62
  • 123
  • possible duplicate of [How to get IsKeyDown method to work in C#](http://stackoverflow.com/questions/12984522/how-to-get-iskeydown-method-to-work-in-c-sharp) – Dennis Nov 24 '14 at 06:26
  • also: http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key – Dennis Nov 24 '14 at 06:29

1 Answers1

-1

You have several options:

Option 1: Add custom message filter to your application:

private KeyMessageFilter _filter = new KeyMessageFilter();

private void Form1_Load(object sender, EventArgs e)
{
    Application.AddMessageFilter(_filter);    
}    

public class KeyMessageFilter : IMessageFilter
{
    private Dictionary<Keys, bool> _keyTable = new Dictionary<Keys, bool>();

    public Dictionary<Keys, bool> KeyTable
    {
        get { return _keyTable; }
        private set { _keyTable = value; }
    }

    public bool IsKeyPressed()
    {
        return _keyPressed; 
    }

    public bool IsKeyPressed(Keys k)
    {
        bool pressed = false;

        if (KeyTable.TryGetValue(k, out pressed))
        {
            return pressed;                  
        }

        return false; 
    }

    private const int WM_KEYDOWN = 0x0100;

    private const int WM_KEYUP = 0x0101;

    private bool _keyPressed = false;


    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_KEYDOWN)
        {
            KeyTable[(Keys)m.WParam] = true;

            _keyPressed = true;
        }

        if (m.Msg == WM_KEYUP)
        {                
            KeyTable[(Keys)m.WParam] = false;

            _keyPressed = false;
        }

        return false;        
}

Option 2: Use GetKeyState API function as linked before:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MouseKeyboardStateTest
{
  public abstract class Keyboard
  {
    [Flags]
    private enum KeyStates
    {
      None = 0,
      Down = 1,
      Toggled = 2
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern short GetKeyState(int keyCode);

    private static KeyStates GetKeyState(Keys key)
    {
      KeyStates state = KeyStates.None;

      short retVal = GetKeyState((int)key);

      //If the high-order bit is 1, the key is down
      //otherwise, it is up.
      if ((retVal & 0x8000) == 0x8000)
        state |= KeyStates.Down;

      //If the low-order bit is 1, the key is toggled.
      if ((retVal & 1) == 1)
        state |= KeyStates.Toggled;

      return state;
    }

    public static bool IsKeyDown(Keys key)
    { 
      return KeyStates.Down == (GetKeyState(key) & KeyStates.Down);
    }

    public static bool IsKeyToggled(Keys key)
    { 
      return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled);
    }
  }
}

Option 3: Handle KeyDown And KeyUp events by yourself. It's not best approach but for small application would be nice.

al_amanat
  • 615
  • 3
  • 10
  • It is better to mark a question as duplicate, providing a link to already answered question, instead of copy/paste of answer from that question, and pass it as your own answer: http://stackoverflow.com/a/1927644/580053 – Dennis Nov 24 '14 at 06:25