不传播到窗口的背景键盘快捷键

时间:2012-02-28 19:51:33

标签: c# keyboard-shortcuts

我制作了一个应用程序,即使窗口不活动也需要处理特定的键盘按下,现在我有了这个并且它工作得很好;但是处理过的密钥仍然传播到窗口(不确定我的应用程序是否先处理它们,所以我可能会对此做出反击)。

有没有办法让我的应用程序可以处理我想要的按键,但没有将密钥发送到当前的应用程序/窗口。

EX)我的应用程序在后台打开监控数字键盘,每次按数字键盘上的数字键我想将该数字添加到文本框中以供显示。现在我打开了chrome并将光标放在地址栏中,我希望能够在我的应用程序处理它们的同时按数字键,但不要将它们显示在chromes地址栏中。

感谢。

这基本上是一个非常简单的键盘记录器。

EDIT) 键盘钩

#endregion
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;   

public class GlobalKeyboardHook
{
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam);
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);

public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam);

public struct keyBoardHookStruct
{
    public int vkCode;
    public int scanCode;
    public int flags;
    public int time;
    public int dwExtraInfo;
}

const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int WM_SYSKEYDOWN = 0x0104;
const int WM_SYSKEYUP = 0x0105;

LLKeyboardHook llkh;
public List<Keys> HookedKeys = new List<Keys>();

IntPtr Hook = IntPtr.Zero;

public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;

public GlobalKeyboardHook()
{
    llkh = new LLKeyboardHook(HookProc);
    hook();
}
~GlobalKeyboardHook()
{ unhook(); }

public void hook()
{
    IntPtr hInstance = LoadLibrary("User32");
    Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0);
}

public void unhook()
{
    UnhookWindowsHookEx(Hook);
}

public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam)
{
    if (Code >= 0)
    {
        Keys key = (Keys)lParam.vkCode;
        if (HookedKeys.Contains(key))
        {
            KeyEventArgs kArg = new KeyEventArgs(key);
            if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                KeyDown(this, kArg);
            else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                KeyUp(this, kArg);
            if (kArg.Handled)
                return 1;
        }
    }
    return CallNextHookEx(Hook, Code, wParam, ref lParam);
}

}

使用GlobalKeyboardHook

GlobalKeyboardHook gHook;
private void Form1_Load(object sender, EventArgs e)
{
   gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook
   // Declare a KeyDown Event
   gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
   // Add the keys you want to hook to the HookedKeys list
   foreach (Keys key in Enum.GetValues(typeof(Keys)))
       gHook.HookedKeys.Add(key);
}

// Handle the KeyDown Event
public void gHook_KeyDown(object sender, KeyEventArgs e)
{
   textBox1.Text += ((char)e.KeyValue).ToString();
}

private void button1_Click(object sender, EventArgs e)
{
   gHook.hook();
}

private void button2_Click(object sender, EventArgs e)
{
   gHook.unhook();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   gHook.unhook();
}

1 个答案:

答案 0 :(得分:1)

在你的hookProc中,如果你不想传播WM_KEYS,你不能调用return CallNextHookEx(Hook, Code, wParam, ref lParam);

如果您调用CallNextHookEx,则会传播消息。

顺便说一句,您知道您使用的是Global钩子,而不是特定于线程的钩子?因此,您将捕获所有按键,而不仅仅是与您的应用相关的按键。