-1

I want to block the user keyboard input for a short time, but the method does not work.

public partial class NativeMethods {

    [DllImport("user32.dll", EntryPoint = "BlockInput")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool BlockInput([MarshalAs(UnmanagedType.Bool)] bool fBlockIt);

}

public class KeyboardBlocker
{

    public static void Block(int span)
    {
        try
        {
            NativeMethods.BlockInput(true);
            Console.WriteLine("should have blocked");
            Thread.Sleep(span);
        }
        finally
        {
            NativeMethods.BlockInput(false);

does someone have an idea ? Thanks already

  • What exactly does not work? What is the return value you get from `BlockInput`? If it's nonzero you can get the last error using [`GetLastError`](https://msdn.microsoft.com/d852e148-985c-416f-a5a7-27b6914b45d4). – Streamline Feb 18 '19 at 14:08
  • I dont really get a return value – Colin1860 Feb 18 '19 at 14:27

1 Answers1

0

Apparently the BlockInput function doesn't work in 64 bit. You can try the answer from here: Using BlockInput to block keyboard and mouse input.

Palle Due
  • 5,929
  • 4
  • 17
  • 32