如何在 C# 中阻止键盘和鼠标输入? [英] How can I block keyboard and mouse input in C#?

查看:74
本文介绍了如何在 C# 中阻止键盘和鼠标输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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);
  }
}

编辑

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

Added some code to demonstrate how to block for an interval

这篇关于如何在 C# 中阻止键盘和鼠标输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆