Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I block keyboard and mouse input in C#?

I'm looking for some code (preferably C#) that will prevent keyboard and mouse input.

like image 561
Dean Bates Avatar asked Feb 25 '09 15:02

Dean Bates


2 Answers

Expanding on Josh's (correct) answer. Here's the PInvoke signature for that method.

public partial class NativeMethods {

    /// Return Type: BOOL->int
    ///fBlockIt: BOOL->int
    [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 static void BlockInput(TimeSpan span) {
  try { 
    NativeMethods.BlockInput(true);
    Thread.Sleep(span);
  } finally {
    NativeMethods.BlockInput(false);
  }
}

EDIT

Added some code to demonstrate how to block for an interval

like image 179
JaredPar Avatar answered Nov 10 '22 22:11

JaredPar


Look into the BlockInput Win32 function

Sounds like some fun testing :)

like image 24
Josh Stodola Avatar answered Nov 10 '22 22:11

Josh Stodola