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

标签 c# windows keyboard mouse

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

最佳答案

扩展乔希的(正确)答案。这是该方法的 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);
  }
}

编辑

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

关于c# - 如何在 C# 中阻止键盘和鼠标输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/586547/

相关文章:

c# - EF Core、Sqlite、InMemory、Code First - 如何?

c++ - fscanf 双重读取文件中间的变量 - Windows NT 4.0 dll

windows - 如何在 Visual Studio 中为 xamarin.forms 应用程序生成 .ipa 文件

android - 提交时将 Android Numpad 的 "Next"更改为 "Go"

javascript - JQuery 键盘 css 将颜色分配给具有特定类的按钮(键)失败

.net - Keys.Control/Shift 和 Keys.ControlKey/ShiftKey 之间的确切区别是什么?

c# - 在 asp.net web api 中上传文件之前检查文件扩展名

c# - LINQ 的延迟执行,但是如何呢?

c# - 直接向U盘写入字节

c# - IdentityServer4:如何添加持久 key 并部署到生产环境中?