c# 捕获系统快捷键

private void GlobalKeyCapture_KeyDown(object sender, KeyEventArgs e)
{
    //判断当前进程是否是活动进程,以决定是否响应
    var currentProcess = getActiveProcess();
    if (currentProcess.Id != Process.GetCurrentProcess().Id)
        return;

    if (e.KeyCode == Keys.F)
    {
        var ctrl = Control.ModifierKeys.HasFlag(Keys.Control);
        if (ctrl)
            menuItemFind_Click(menuItemFind, new EventArgs());
    }
}

private void GlobalKeyCapture_KeyUp(object sender, KeyEventArgs e)
{
    //判断当前进程是否是活动进程,以决定是否响应
    var currentProcess = getActiveProcess();
    if (currentProcess.Id != Process.GetCurrentProcess().Id)
        return;

    if (e.KeyCode == Keys.F12)
    {
        
    }
    else if (e.KeyCode == Keys.F5)
    {
        
    }
    else if (e.KeyCode == Keys.Escape)
    {
        
    }
}

核心类:

public class GlobalKeyCapture : NativeWindow
{
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_KEYUP = 0x0101;

    public event EventHandler<KeyEventArgs> KeyUp;
    public event EventHandler<KeyEventArgs> KeyDown;

    public GlobalKeyCapture()
    {
        CreateHandle(new CreateParams());
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_KEYUP)
        {
            Keys key = (Keys)m.WParam;
            OnKeyUp(new KeyEventArgs(key));
        }
        else if (m.Msg == WM_KEYDOWN)
        {
            Keys key = (Keys)m.WParam;
            OnKeyDown(new KeyEventArgs(key));
        }
        base.WndProc(ref m);
    }

    protected virtual void OnKeyUp(KeyEventArgs e)
    {
        KeyUp?.Invoke(this, e);
    }

    protected virtual void OnKeyDown(KeyEventArgs e)
    {
        KeyDown?.Invoke(this, e);
    }

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);


    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

    [DllImport("user32.dll")]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll")]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("kernel32.dll")]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    private const int WH_KEYBOARD_LL = 13;
    private const int WM_SYSKEYDOWN = 0x0104;
    private const int WM_SYSKEYUP = 0x0105;

    private IntPtr hookId = IntPtr.Zero;
    private LowLevelKeyboardProc proc;

    public void StartCapturing()
    {
        proc = HookCallback;
        hookId = SetHook(proc);
    }

    public void StopCapturing()
    {
        UnhookWindowsHookEx(hookId);
    }

    private IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            Keys key = (Keys)vkCode;

            if (wParam.ToInt32() == WM_KEYUP || wParam.ToInt32() == WM_SYSKEYUP)
            {
                OnKeyUp(new KeyEventArgs(key));
            }
            else if (wParam.ToInt32() == WM_KEYDOWN || wParam.ToInt32() == WM_SYSKEYDOWN)
            {
                OnKeyDown(new KeyEventArgs(key));
            }
        }
        return CallNextHookEx(hookId, nCode, wParam, lParam);
    }
}

 

posted on 2024-10-11 11:57  空明流光  阅读(13)  评论(0编辑  收藏  举报

导航