Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中阻止键盘和鼠标输入?_C#_Windows_Keyboard_Mouse - Fatal编程技术网

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

如何在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"

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

查看Win32函数

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

扩展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);
  }
}
编辑

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

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助手那里拿到信号,我给它打了一针,但它不起作用。它需要特权提升吗?有人能解释一下如何实现这一点吗?它对我来说不阻止任何东西,但显然对其他人有效。我可以只阻止特定类型的输入吗?只喜欢键盘和鼠标。