可以掛鉤的鍵盤所有甚至鼠標事件,以檢測輸入源。換句話說,如果您使用下面鏈接中提到的Global Hook,您可以捕獲系統事件並將其作爲正常事件進行處理或將其壓制。
你應該看看這個CodeProject上文章,Processing Global Mouse and Keyboard Hooks in C#
MSDN Reference:
一個全局鉤子監視在同一桌面與調用線程的所有線程的消息。 A 線程專用掛鉤僅監視單個線程的消息。全局鉤子過程可以在與調用線程相同的桌面中的任何應用程序的上下文中調用,因此過程必須位於單獨的DLL模塊中。線程特定的鉤子過程僅在關聯線程的上下文中調用。
C#代碼:
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public partial class MainWindow : Window
{
// Structure contain information about low-level keyboard input event
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public Keys key;
public int scanCode;
public int flags;
public int time;
public IntPtr extra;
}
//System level functions to be used for hook and unhook keyboard input
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
//Declaring Global objects
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;
private IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys
{
return (IntPtr)1;
}
}
return CallNextHookEx(ptrHook, nCode, wp, lp);
}
public MainWindow()
{
InitializeComponent();
//Get Current Module
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
//Assign callback function each time keyboard process
objKeyboardProcess = new LowLevelKeyboardProc(CaptureKey);
//Setting Hook of Keyboard Process for current module
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
}
}
其他好的參考資料:
- Low-level Windows API hooks from C# to stop unwanted keystrokes
- Disable Special Keys in Win App C#
你提到的第一篇文章,沒有w有一個[CodePlex站點](http://globalmousekeyhook.codeplex.com/),您可以在其中下載該庫。 –
感謝分享這些信息,真的很有用。 –