如何在C#中阻止键盘和鼠标输入?

Dea*_*tes 12 c# windows keyboard mouse

我正在寻找一些阻止键盘和鼠标输入的代码(最好是C#).

Jar*_*Par 21

扩展Josh的(正确的)答案.这是该方法的PInvoke签名.

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑

添加了一些代码来演示如何阻止间隔

  • 我试了一下,它没有用。是否需要权限提升? (2认同)
  • 有人可以解释一下如何使这项工作吗?它对我没有任何阻碍,但显然对其他人有用。 (2认同)

Jos*_*ola 7

查看BlockInput Win32函数

听起来像一些有趣的测试:)