How to intercept all the keyboard events and prevent losing focus in a WinForms application?

后端 未结 2 1500
一生所求
一生所求 2020-12-03 03:50

A friend of mine is blind and I\'ve got an idea of developing a program that would let him use a PC with the help of a blind typing method and audial feedback. The experienc

2条回答
  •  死守一世寂寞
    2020-12-03 04:09

    I know this topic is old, but I found a possible solution. You can use the last answer and edit it a little bit:

    Instead of calling the next hook:

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine((Keys)vkCode);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
    

    just return -1 for stopping preceding any handle to other controls:

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine((Keys)vkCode);
        }
        return -1;
    }
    

    This isn't nice but it's working. Be careful with this!

提交回复
热议问题