diff --git a/Wox.Infrastructure/GloablHotkey.cs b/Wox.Infrastructure/GloablHotkey.cs deleted file mode 100644 index 7fd9f750e..000000000 --- a/Wox.Infrastructure/GloablHotkey.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace Wox.Infrastructure -{ - public enum KeyEvent : int - { - /// - /// Key down - /// - WM_KEYDOWN = 256, - - /// - /// Key up - /// - WM_KEYUP = 257, - - /// - /// System key up - /// - WM_SYSKEYUP = 261, - - /// - /// System key down - /// - WM_SYSKEYDOWN = 260 - } - - public static class InterceptKeys - { - public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam); - - private static int WH_KEYBOARD_LL = 13; - - public static IntPtr SetHook(LowLevelKeyboardProc proc) - { - using (Process curProcess = Process.GetCurrentProcess()) - using (ProcessModule curModule = curProcess.MainModule) - { - return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); - } - } - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool UnhookWindowsHookEx(IntPtr hhk); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, UIntPtr wParam, IntPtr lParam); - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - public static extern IntPtr GetModuleHandle(string lpModuleName); - - [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] - public static extern short GetKeyState(int keyCode); - - [DllImport("user32.dll")] - internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); - } - - [StructLayout(LayoutKind.Explicit)] - public struct INPUT - { - [FieldOffset(0)] - public Int32 type;//0-MOUSEINPUT;1-KEYBDINPUT;2-HARDWAREINPUT - [FieldOffset(4)] - public KEYBDINPUT ki; - [FieldOffset(4)] - public MOUSEINPUT mi; - [FieldOffset(4)] - public HARDWAREINPUT hi; - } - [StructLayout(LayoutKind.Sequential)] - public struct MOUSEINPUT - { - public Int32 dx; - public Int32 dy; - public Int32 mouseData; - public Int32 dwFlags; - public Int32 time; - public IntPtr dwExtraInfo; - } - [StructLayout(LayoutKind.Sequential)] - public struct KEYBDINPUT - { - public Int16 wVk; - public Int16 wScan; - public Int32 dwFlags; - public Int32 time; - public IntPtr dwExtraInfo; - } - [StructLayout(LayoutKind.Sequential)] - public struct HARDWAREINPUT - { - public Int32 uMsg; - public Int16 wParamL; - public Int16 wParamH; - } -} \ No newline at end of file diff --git a/Wox.Infrastructure/GlobalHotkey.cs b/Wox.Infrastructure/GlobalHotkey.cs index 4dc2ec259..893d7afef 100644 --- a/Wox.Infrastructure/GlobalHotkey.cs +++ b/Wox.Infrastructure/GlobalHotkey.cs @@ -5,6 +5,103 @@ using Wox.Plugin; namespace Wox.Infrastructure { + public enum KeyEvent : int + { + /// + /// Key down + /// + WM_KEYDOWN = 256, + + /// + /// Key up + /// + WM_KEYUP = 257, + + /// + /// System key up + /// + WM_SYSKEYUP = 261, + + /// + /// System key down + /// + WM_SYSKEYDOWN = 260 + } + + public static class InterceptKeys + { + public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam); + + private static int WH_KEYBOARD_LL = 13; + + public static IntPtr SetHook(LowLevelKeyboardProc proc) + { + using (Process curProcess = Process.GetCurrentProcess()) + using (ProcessModule curModule = curProcess.MainModule) + { + return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); + } + } + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool UnhookWindowsHookEx(IntPtr hhk); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, UIntPtr wParam, IntPtr lParam); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + public static extern short GetKeyState(int keyCode); + + [DllImport("user32.dll")] + internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); + } + + [StructLayout(LayoutKind.Explicit)] + public struct INPUT + { + [FieldOffset(0)] + public Int32 type;//0-MOUSEINPUT;1-KEYBDINPUT;2-HARDWAREINPUT + [FieldOffset(4)] + public KEYBDINPUT ki; + [FieldOffset(4)] + public MOUSEINPUT mi; + [FieldOffset(4)] + public HARDWAREINPUT hi; + } + [StructLayout(LayoutKind.Sequential)] + public struct MOUSEINPUT + { + public Int32 dx; + public Int32 dy; + public Int32 mouseData; + public Int32 dwFlags; + public Int32 time; + public IntPtr dwExtraInfo; + } + [StructLayout(LayoutKind.Sequential)] + public struct KEYBDINPUT + { + public Int16 wVk; + public Int16 wScan; + public Int32 dwFlags; + public Int32 time; + public IntPtr dwExtraInfo; + } + [StructLayout(LayoutKind.Sequential)] + public struct HARDWAREINPUT + { + public Int32 uMsg; + public Int16 wParamL; + public Int16 wParamH; + } + /// /// Listens keyboard globally. /// Uses WH_KEYBOARD_LL.