2009-02-25 86 views
10

我正在尋找一些代碼(最好是C#)來防止鍵盤和鼠標輸入。如何阻止C#中的鍵盤和鼠標輸入?

+0

你可以給出更多的背景知道爲什麼你想這樣做嗎? – JaredPar 2009-02-25 15:47:01

+0

您的意思是隻在您的應用程序中或整個系統中的全局? – 2009-02-25 15:48:27

+0

什麼;畢竟,你可能指的是一個文本框,它設置只讀屬性會做到這一點。 – 2009-02-25 15:48:37

回答

7

查找到BlockInput Win32函數

聽起來像一些有趣的測試:)

19

擴大對約什的(正確的)答案。這是該方法的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); 
    } 
} 

編輯

添加了一些代碼來說明如何阻止一個時間間隔