如何在c#中禁用Windows键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class KeyboardHook : IDisposable { private const int WM_KEYDOWN = 0x0100; private const int WM_KEYUP = 0x0101; private const int WM_SYSKEYDOWN = 0x0104; private const int WM_SYSKEYUP = 0x0105; private IntPtr _hookId = IntPtr.Zero; public event KeyEventHandler KeyDown; public event KeyEventHandler KeyUp; public KeyboardHook() { _hookId = SetHook(HookCallback); } private IntPtr SetHook(LowLevelKeyboardProc proc) { using ( var curProcess = Process.GetCurrentProcess()) using ( var curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelKeyboardProc( int nCode, IntPtr wParam, IntPtr lParam); private IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { var key = (Keys)Marshal.ReadInt32(lParam); if (key == Keys.LWin || key == Keys.RWin) { return (IntPtr)1; } var eventArgs = new KeyEventArgs(key); if ((wParam.ToInt32() == WM_KEYDOWN || wParam.ToInt32() == WM_SYSKEYDOWN) && KeyDown != null ) { KeyDown( this , eventArgs); } else if ((wParam.ToInt32() == WM_KEYUP || wParam.ToInt32() == WM_SYSKEYUP) && KeyUp != null ) { KeyUp( this , eventArgs); } if (eventArgs.Handled) { return (IntPtr)1; } } return CallNextHookEx(_hookId, nCode, wParam, lParam); } public void Dispose() { if (_hookId != IntPtr.Zero) { UnhookWindowsHookEx(_hookId); _hookId = IntPtr.Zero; } } [DllImport( "user32.dll" , CharSet = CharSet.Auto, SetLastError = true )] private static extern IntPtr SetWindowsHookEx( int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport( "user32.dll" , CharSet = CharSet.Auto, SetLastError = true )] [ return : MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport( "user32.dll" , CharSet = CharSet.Auto, SetLastError = true )] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport( "kernel32.dll" , CharSet = CharSet.Auto, SetLastError = true )] private static extern IntPtr GetModuleHandle( string lpModuleName); private const int WH_KEYBOARD_LL = 13; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static void Main() { using ( var keyboardHook = new KeyboardHook()) { keyboardHook.KeyDown += (sender, args) => { if (args.KeyCode == Keys.LWin || args.KeyCode == Keys.RWin) { args.Handled = true ; } }; Application.Run(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异