I'm looking for some code (preferably C#) that will prevent keyboard and mouse input.
-
Can you give some more context as to why you would want to do this?JaredPar– JaredPar2009-02-25 15:47:01 +00:00Commented Feb 25, 2009 at 15:47
-
Do you mean only in your application or globally across the system?Iain M Norman– Iain M Norman2009-02-25 15:48:27 +00:00Commented Feb 25, 2009 at 15:48
-
And to what; after all, you could be referring to a textbox, which setting the readonly property would do it.Rowland Shaw– Rowland Shaw2009-02-25 15:48:37 +00:00Commented Feb 25, 2009 at 15:48
-
Sorry I wish to block input globallyDean Bates– Dean Bates2009-02-25 16:10:47 +00:00Commented Feb 25, 2009 at 16:10
Add a comment
|
3 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
9 Comments
Dean Bates
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
Matthew Olenik
Take a look at the Managed Windows API, it might have something that wraps this functionality already. mwinapi.sourceforge.net
JaredPar
@Matt, i usually just grab the sig from the PInvoke Interop Assistant codeplex.com/clrinterop
emd
I gave this a shot and it did not work. Does it require privilege elevation?
Zed
Can someone explain how to make this work at all? It doesn't block anything for me but apparently works for others.
|
Look into the BlockInput Win32 function
Sounds like some fun testing :)
1 Comment
Kishori
Can I have only specific type of inputs to be blocked? Like only keyboard and mouse.
using System.Runtime.InteropServices;
public partial class NativeMethods
{
[DllImport("user32.dll", EntryPoint = "BlockInput")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BlockInput([MarshalAs(UnmanagedType.Bool)] bool fBlockIt);
}
internal is not public. So it does not fall into the CA1401
A public or protected method in a public type has the System.Runtime.InteropServices.DllImportAttribute attribute