Global Key/MouseHook with UI

馋奶兔 提交于 2019-12-12 00:19:29

问题


I want to make a C# Application with a Keyboard and Mouse on screen.
Every Key or Button that is clicked should be seen in this Application by for example coloring one of the keys ( i know how to do that ). This should also work if the Application is not focused.
Currently i am using a global Key- and Mousehook which works fine.
The problem is, the Keyhook does only intercept one Key at a time which means i can only show on Key at a time. I want to be able to show multiple keys at a time on screen. KeyListeners are unfortunately no option because they dont work outside the Application. Does anyone has an idea how to make this possible?

Here's the KeyHook i am using:

public class KeyHook
{
    private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

    static int hHook = 0;

    public static List<Keys> KeyCodes = new List<Keys>();

    const int WH_KEYBOARD_LL = 13;

    HookProc KeyboardHookProcedure;

    [StructLayout(LayoutKind.Sequential)]
    private class keyboardHookStruct
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
    IntPtr hInstance, int threadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern bool UnhookWindowsHookEx(int idHook);

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     CallingConvention = CallingConvention.StdCall)]
    private static extern int CallNextHookEx(int idHook, int nCode,
    IntPtr wParam, IntPtr lParam);

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

    public KeyHook()
    {
        Hook();
    }

    ~KeyHook()
    {
        UnHook();
    }

    public int Hook()
    {
        KeyboardHookProcedure = new HookProc(KeyHook.KeyboardHookProc);

        hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, (IntPtr)LoadLibrary("User32"), 0);
        return hHook;
    }

    public bool UnHook()
    {
        bool ret = UnhookWindowsHookEx(hHook);
        if (ret)
            hHook = 0;
        return ret;
    }

    private static int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode < 0)
        {
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        else
        {

            if (((int)wParam == 256) || ((int)wParam == 260))
            {
                keyboardHookStruct MyKeyboardHookStruct = (keyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(keyboardHookStruct));
                //Adding Key to a log i use for other stuff
                KeyCodes.Add((Keys)MyKeyboardHookStruct.vkCode);
                //Code for coloring Key in the UI for pressed Key
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }

}

来源:https://stackoverflow.com/questions/15422424/global-key-mousehook-with-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!