13

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

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Dean Bates
  • 1,927
  • 5
  • 25
  • 24

3 Answers3

23

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
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    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 – Dean Bates Feb 25 '09 at 16:12
  • Take a look at the Managed Windows API, it might have something that wraps this functionality already. http://mwinapi.sourceforge.net/ – Matthew Olenik Feb 25 '09 at 17:16
  • @Matt, i usually just grab the sig from the PInvoke Interop Assistant http://www.codeplex.com/clrinterop – JaredPar Feb 25 '09 at 17:39
  • @JaredPar I didn't work in my application! does it work on win 7? – Murhaf Sousli Apr 12 '12 at 01:01
  • 2
    I gave this a shot and it did not work. Does it require privilege elevation? – emd Dec 03 '12 at 15:37
  • It does require privileges for obvious reasons (you're blocking most input from the user - control-alt-del and maybe a few others will still work) – Charles P. Jan 28 '15 at 00:26
  • This worked for me but I wanted to disable only Keyboard and mouse and support touch. BlockInput blocks everything. Does this accepts any parameter for type of input? – Kishori Jul 28 '15 at 05:20
  • 4
    Can someone explain how to make this work at all? It doesn't block anything for me but apparently works for others. – Zed Jun 19 '17 at 15:18
  • @Zed it also doesn't work for me, but if I run the app with Administrator rights - it works fine. It can be disabled by pressing Ctrl+Alt+Del. https://www.pinvoke.net/default.aspx/user32.blockinput – Artiom Jan 13 '20 at 06:55
7

Look into the BlockInput Win32 function

Sounds like some fun testing :)

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
0
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

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33