Global Hooks(非活动程序)

MLM*_*MLM 1 c# hook global hotkeys

我正在创建一个程序,该程序允许您粘贴文本,但是看起来像您在粘贴时键入文本,Paste Typer。

我想用来Ctrl + b取消粘贴,但是我在使用热键时遇到问题。

我认为我需要使用一个WH_KEYBOARD_LL钩子并包含该user32文件,但是添加它和命名空间时通常会出错。

我最接近的是:http : //thedarkjoker94.cer33.com/?p=111-但即使使用Ctrl,它也似乎无法与alt和其他修饰符一起使用KeyData

我需要一种即使在程序不是活动窗口时也可以使用的热键。这是Microsoft Visual C#2010中的Windows窗体应用程序。

有很多StackOverlow主题,但是它们过时,而且不够完整,无法运行。

Chu*_*age 5

您在谈论HookManager吗?

我这样成功地使用它:

HookManager.KeyDown += new KeyEventHandler(HookManager_KeyDown);
Run Code Online (Sandbox Code Playgroud)

void HookManager_KeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.IsKeyDown(Keys.LWin)) // Is the Left-Windows Key down and ...
        switch (e.KeyCode)
        {
            case Keys.O: 
                // ...
                e.Handled = true;
                break;
            case Keys.H: 
                // ...
                e.Handled = true;
                break;
        }
}
Run Code Online (Sandbox Code Playgroud)

我创建了Keyboard类,这里是代码:

// Used: http://www.pinvoke.net/default.aspx/user32.getasynckeystate
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Win32.Devices
{
    public class Keyboard
    {
        [DllImport("user32.dll")]
        static extern ushort GetAsyncKeyState(Keys vKey);

        public static bool IsKeyDown(Keys key)
        {
            return 0 != (GetAsyncKeyState(key) & 0x8000);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)