Using BlockInput to block keyboard and mouse input

Go To


Here's a touchpad virtual keyboard button. When I press the 'TOUCH' on the screen, 'ab' will be displayed. I'm trying to use BlockInput to prevent any input of mouse and keyboard for 2 seconds just after the TOUCH button is pressed. But it doesn't work, I think need to set a timer for the BlockInput, any1 know how to do that? Here's my code:

public partial class TRY
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "BlockInput")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool BlockInput([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fBlockIt);

public void TOUCH(string key)
        if (key == "Press")

2012-04-04 04:52
by hakunabean
You are not checking for errors, you ignore the BlockInput() return value. So sure, you can't know why it doesn't work. Throw a Win32Exception when it returns false - Hans Passant 2012-04-04 05:09
@HansPassant: Thx for ur info. My code has no error when l try to compile it. But at the period of Thread.Sleep(2000), user's input can still be received. Can u show me the way to fix it - hakunabean 2012-04-04 06:32
Avoid using the sleep in this (UI?)thread and use a background worker - Ralf de Kleine 2012-04-04 11:27


BlockInput() won't work reliably on touch devices as mouse messages are often routed as touch messages and vice versa. There are two ways to block touch input:

1) A HID class filter driver installation that will block all touch messages coming from HID devices. A filter driver would need to be developed and a code signing certificate with kernel code signing capability would be required for the driver to be installable on non-test machines

2) Hook all processes, 32 and 64 bit, and during that time block all messages including touch messages in something like this:

    hkKey  = SetWindowsHookEx(WH_CALLWNDPROC, procTouchMsg, hInstHookDll, 0);
    hkKey2 = SetWindowsHookEx(WH_CALLWNDPROCRET, procTouchMsg, hInstHookDll, 0);
    hkKey3 = SetWindowsHookEx(WH_GETMESSAGE, procTouchMsg, hInstHookDll, 0);

    hkKey4 = SetWindowsHookEx(WH_SYSMSGFILTER, procTouchMsg, hInstHookDll, 0);
    hkKey5 = SetWindowsHookEx(WH_MSGFILTER, procTouchMsg, hInstHookDll, 0);
    hkKey6 = SetWindowsHookEx(WH_MOUSE, procTouchMsg, hInstHookDll, 0);
    hkKey7 = SetWindowsHookEx(WH_MOUSE_LL, procTouchMsg, hInstHookDll, 0);
    hkKey8 = SetWindowsHookEx(WH_KEYBOARD, procTouchMsg, hInstHookDll, 0);
    hkKey9 = SetWindowsHookEx(WH_KEYBOARD_LL, procTouchMsg, hInstHookDll, 0);   

Then just return NULL from the procTouchMsg function. BE CAREFULL - this could disable all input to your device so either stop the hook after some seconds pass or exclude hooking for some vital processes.

2017-10-08 14:58
by Alex Fotios