1

We having some issue with keyboard KeyPress event. It is stoped when key press event took longer time to complete the request. We have support for KeyBoardHook which will use to capture the global keyboard events. It is same class as mention in this SO answer.

Microsoft recommnds that the callback for a keyboard hook should be as short as possible since it it actually part of an interrupt handler. If the interrupt takes too long, the offending part of the daisy chain of handlers is simply removed. Since we are not told when this happens, there is no way of detecting it. The keyboard handler simply stops working.

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

        EventHandlerKey( keyAction, vkCode );
    }

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

private void EventHandlerKey(int keyAction, int keyCode)
{
    // Some process which taking long time..!
}

Any suggested solution to resolve the issue?

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • 2
    There are only two ways, don't take more the 5 seconds or change the registry key that sets the timeout. ~15 billion processor cycles is rather a lot, ought to be enough for everybody. The *usual* problem is the debugger, you can't set a breakpoint in the callback without tripping that timeout. Do beware ShowDialog(). – Hans Passant May 28 '18 at 12:51

0 Answers0