LowLevelKeyboardProc - 没有重载macthes委托

时间:2016-03-11 21:21:21

标签: c# keystroke

在我解释我的错误之前知道我自己并不了解它,它主要是来自一个教程,但想到id在这里张贴以防万一有人知道。所以这用于获取每次击键。这些是我的变量等

        #region Key Strokes
    private const int WM_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, UIntPtr lParam);
    private static byte CapsLock = 0;
    private static byte Shift = 0;
    private static IntPtr HookID = IntPtr.Zero;
    private static LowLevelKeyboardProc Proc = GetKeyStrokes;

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lparam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadID);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetModuleHandle(string lpModuleName);
    #endregion

在我的主要功能中,我设置了HookID

   HookID = SetHook(Proc);

然后我有我的SetHook功能

    private static IntPtr SetHook (LowLevelKeyboardProc Proc)
    {
        using (Process p = Process.GetCurrentProcess())
        using (ProcessModule m = p.MainModule)

            return SetWindowsHookEx(WM_KEYBOARD_LL, Proc, GetModuleHandle(m.ModuleName), 0);

    }

最后我有GetKeyStrokes函数

   private static IntPtr GetKeyStrokes(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int Code = Marshal.ReadInt32(lParam);

            if(Keys.Shift == Control.ModifierKeys)
            {
                Program.Shift = 1;
            }

            //Switch statment for special keys(too long)
            }

            Program.Shift = 0;
        }

        return CallNextHookEx(HookID, nCode, wParam, lParam);
    }

唯一的错误是我的变量' Proc' = GetKeyStrokes;其中说"没有过载" GetKeyStrokes"匹配委托" Program.LowLevelKeyboardProc"。我知道这很复杂,我不希望很多人得到它,我甚至没有得到很多,但欢迎任何想法,谢谢

0 个答案:

没有答案