C#检查是否按下了多个键(全局键盘挂钩)

问题描述 投票:2回答:1

我正在制作一个在后台运行的C#表单应用程序,并检查是否按了CTRL + A +S。所以我正在检查互联网上的论坛,我已经设置为该应用程序在后台运行,现在我是试图设置键盘挂钩。我在互联网上找到了一个全局键盘挂钩代码。

这里是代码:

    // GLOBAL HOOK
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);

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

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

    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string lpFileName);

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

    const int WH_KEYBOARD_LL = 13; // Number of global LowLevel- hook on the keyboard
    const int WM_KEYDOWN = 0x100; // Messages pressing

    private LowLevelKeyboardProc _proc = hookProc;

    private static IntPtr hhook = IntPtr.Zero;

    public void SetHook()
    {
        IntPtr hInstance = LoadLibrary("User32");
        hhook = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hInstance, 0);
    }

    public static void UnHook()
    {
        UnhookWindowsHookEx(hhook);
    }

    public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
    {
        if (code >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            if (vkCode.ToString() == "162") //162 is ASCI CTRL
            {
                MessageBox.Show("You pressed a CTRL");                    
            }
            return (IntPtr)1;
        }
        else
            return CallNextHookEx(hhook, code, (int)wParam, lParam);
    }

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Remove the hook
        UnHook();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Set the hook
        SetHook();
    }

}

我的问题是,此挂钩设置了1个键,我不知道如何检查是否按了3个键(CTRL + A + S)。

我已经尝试过了,但是没有用。

if (vkCode.ToString() == "162" && vkCode.ToString() == "65" && vkCode.ToString() == "83") //162 is CTRL, 65 is A, 83 is S, ASCII CODE
            {
                MessageBox.Show("You pressed a CTRL + A + S");                    
            }

所以我的问题是,该程序或该挂钩将使我能够检查3个按下的键(CTRL + A + S)。

c# winforms winapi keyboard-hook
1个回答
3
投票

我相信您必须分别检测CTRL,A和S,并使用标志来跟踪CTRL和A是否是最后的按键,例如:

static bool ctrlPressed = false;
static bool ctrlAPressed = false;

public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
{
    if (code >= 0 && wParam == (IntPtr)WM_KEYDOWN)
    {
        int vkCode = Marshal.ReadInt32(lParam);

        if (vkCode == 162 || vkCode == 163) //162 is Left Ctrl, 163 is Right Ctrl
        {
            ctrlPressed = true;
        }
        else if (vkCode == 65 && ctrlPressed == true) // "A"
        {
            ctrlPressed = false;
            ctrlAPressed = true;
        }
        else if (vkCode == 83 && ctrlAPressed == true) // "S"
        {
            ctrlPressed = false;
            ctrlAPressed = false;
            MessageBox.Show("Bingo!");
        }
        else
        {
            ctrlPressed = false;
            ctrlAPressed = false;
        }

        // return (IntPtr)1; // note: this will interfere with keyboard processing for other apps
    }
//  else // don't interfere , always return callnexthookex
        return CallNextHookEx(hhook, code, (int)wParam, lParam);
}

如果要确保同时按下所有按钮,逻辑相似,则只需添加检查按键是否按下或按下。您也可以使用某种列表或队列或一些按键来提出更聪明的方法。

© www.soinside.com 2019 - 2024. All rights reserved.