简体   繁体   English

检查鼠标事件期间非修饰键是否处于按下状态

[英]Check if non-modifier keys are in pressed state during mouse events

According msdn documentations, in WinForms I can check if modifier keys ( SHIFT , CTRL , and ALT ) is in a pressed state, during mouse events. 根据msdn文档,在WinForms中,可以在鼠标事件期间检查修饰键( SHIFTCTRLALT )是否处于按下状态。 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? 例如,如何在鼠标移动事件期间检查非修饰键(例如空格键)是否处于按下状态?

You have several options: 您有几种选择:

Option 1 : Add custom message filter to your application: 选项1 :向您的应用程序添加自定义消息过滤器:

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: 选项2 :将GetKeyState API函数用作之前链接的对象:

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. 选项3:自己处理KeyDown和KeyUp事件。 It's not best approach but for small application would be nice. 这不是最佳方法,但是对于小型应用程序将是不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM