views:

2439

answers:

5

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

+2  A: 

Look into the BlockInput Win32 function

Sounds like some fun testing :)

Josh Stodola
+3  A: 

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

JaredPar
Many thanks for this. Do you have any code which uses this signature? For example block input, wait for 30 seconds, re-enable input.Thanks
Take a look at the Managed Windows API, it might have something that wraps this functionality already.http://mwinapi.sourceforge.net/
Matt Olenik
@Matt, i usually just grab the sig from the PInvoke Interop Assistant http://www.codeplex.com/clrinterop
JaredPar
A: 
Allan Simonsen
A: 

Can't you search on Google and MSDN ?!!!

If you don't like the question, vote it down. I think it's a good question.
Snake
A: 

It does block the inputs some time and some time it does not. If you know how to enforce consistency in blocking the inputs.

Imran Haider Ashraf
This isn't an answer to the question. Instead it's a new question, that has a connection to this one. So please ask a new question, where you say that you tried this function, but it sometimes doesn't block. Maybe you can also write a little more under which circumstances it won't work (in your new question).
Oliver