如何在C#中阻止键盘和鼠标输入?
我正在寻找一些防止键盘和鼠标输入的代码(最好是C#) 查看Win32函数 听起来像是一些有趣的测试:)扩展Josh(正确)的答案。这是该方法的PInvoke签名如何在C#中阻止键盘和鼠标输入?,c#,windows,keyboard,mouse,C#,Windows,Keyboard,Mouse,我正在寻找一些防止键盘和鼠标输入的代码(最好是C#) 查看Win32函数 听起来像是一些有趣的测试:)扩展Josh(正确)的答案。这是该方法的PInvoke签名 public partial class NativeMethods { /// Return Type: BOOL->int ///fBlockIt: BOOL->int [System.Runtime.InteropServices.DllImportAttribute("user32.dll"
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);
}
}
编辑
添加了一些代码来演示如何在一个时间间隔内阻塞
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);
}
内部
不是公共
。所以它不属于CA1401
public类型中的public或protected方法具有属性您能否提供更多的上下文来说明为什么要这样做?您的意思是仅在应用程序中还是在整个系统中全局执行?以及执行什么操作;毕竟,你可能指的是一个文本框,设置readonly属性就可以了。你有使用这个签名的代码吗?例如,阻止输入,等待30秒,重新启用输入。感谢您查看托管Windows API,它可能已经包含了一些包装此功能的内容@马特,我通常只是从PInvoke Interop助手那里拿到信号,我给它打了一针,但它不起作用。它需要特权提升吗?有人能解释一下如何实现这一点吗?它对我来说不阻止任何东西,但显然对其他人有效。我可以只阻止特定类型的输入吗?只喜欢键盘和鼠标。