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
2009-02-25 15:49:16
+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
2009-02-25 15:55:13
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
2009-02-25 16:12:24
Take a look at the Managed Windows API, it might have something that wraps this functionality already.http://mwinapi.sourceforge.net/
Matt Olenik
2009-02-25 17:16:19
@Matt, i usually just grab the sig from the PInvoke Interop Assistant http://www.codeplex.com/clrinterop
JaredPar
2009-02-25 17:39:08
A:
Allan Simonsen
2009-02-25 16:26:14
If you don't like the question, vote it down. I think it's a good question.
Snake
2010-05-20 08:12:51
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
2010-05-20 08:09:06
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
2010-05-20 08:17:40