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

问题描述:

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

+0

你可以给出更多的背景知道为什么你想这样做吗? – JaredPar 2009-02-25 15:47:01

+0

您的意思是只在您的应用程序中或整个系统中的全局? – 2009-02-25 15:48:27

+0

什么;毕竟,你可能指的是一个文本框,它设置只读属性会做到这一点。 – 2009-02-25 15:48:37

查找到BlockInput Win32函数

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

扩大对约什的(正确的)答案。这是该方法的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); 
    } 
} 

编辑

添加了一些代码来说明如何阻止一个时间间隔

看一看这篇文章Processing Global Mouse and Keyboard Hooks in C#。写这篇文章的人对这个问题做了大量的研究。

也许有一些东西可以使用。