0

我使用以下方法挂钩键盘事件:

HookManager.KeyUp += new KeyEventHandler(HookManager_KeyUp);

然后通过以下方式等待键盘事件:

public void HookManager_KeyUp(object sender, KeyEventArgs e)
{
    StepInfo step = new StepInfo();
    step.keyALT = e.Alt;
    step.keyCTRL = e.Control;
    step.keySHIFT = e.Shift;
    step.KeyHandle = e.Handled;
    step.keySuppress = e.SuppressKeyPress;
    step.keyCode = e.KeyCode;
    step.keyData = e.KeyData;
    step.keyValue = e.KeyValue;
    step.modifiers = e.Modifiers;
}

我想检查组合键shiftctrlalt其他控制键。示例:shift+ a, ctrl+ f.. 等 但我都是 e.KeyData,它只返回 A、B、C 等字母键,但 e.Alt、e.Shift 和 e.Control 始终为“假”值。

我该如何检查,用户是否按下Ashift+ A

4

1 回答 1

0

您可能会发现GetKeyboardState函数在您的情况下很有用:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646299(v=vs.85).aspx

实现可能是这样的:

  [DllImport("User32.dll",
             EntryPoint = "GetKeyboardState",
             CallingConvention = CallingConvention.Winapi)]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern Boolean CoreGetKeyboardState(Byte[] value);

  public static Boolean IsKeyDown(Keys key) {
    Byte[] data = new Byte[256];

    if (!CoreGetKeyboardState(data))
      return false;

    return ((data[(int) key] & 0x80) == 0x80);
  }

  ...

  if (IsKeyDown(Keys.A) && 
      (IsKeyDown(key.Menu) || IsKeyDown(key.ShiftKey) || IsKeyDown(key.ControlKey))) {
    //TODO: You code here  
  }
于 2013-05-02T13:52:30.357 回答