From f9ffa9c387b2f53156a98d48bd011de12dc55080 Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Tue, 18 Mar 2014 17:26:09 +0100 Subject: [PATCH 1/4] gloablHotkey -> GlobalHotkey --- Wox.Infrastructure/GloablHotkey.cs | 89 ------------------ Wox.Infrastructure/GlobalHotkey.cs | 94 ++++++++++++++++++++ Wox.Infrastructure/Wox.Infrastructure.csproj | 1 + Wox/HotkeyControl.xaml.cs | 2 +- Wox/MainWindow.xaml.cs | 4 +- 5 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 Wox.Infrastructure/GlobalHotkey.cs diff --git a/Wox.Infrastructure/GloablHotkey.cs b/Wox.Infrastructure/GloablHotkey.cs index 22602d27d..7fd9f750e 100644 --- a/Wox.Infrastructure/GloablHotkey.cs +++ b/Wox.Infrastructure/GloablHotkey.cs @@ -1,8 +1,6 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using Wox.Plugin; namespace Wox.Infrastructure { @@ -29,93 +27,6 @@ namespace Wox.Infrastructure WM_SYSKEYDOWN = 260 } - /// - /// Listens keyboard globally. - /// Uses WH_KEYBOARD_LL. - /// - public class GloablHotkey : IDisposable - { - private InterceptKeys.LowLevelKeyboardProc hookedLowLevelKeyboardProc; - private IntPtr hookId = IntPtr.Zero; - public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state); - public event KeyboardCallback hookedKeyboardCallback; - - //Modifier key constants - private const int VK_SHIFT = 0x10; - private const int VK_CONTROL = 0x11; - private const int VK_ALT = 0x12; - private const int VK_WIN = 91; - - public GloablHotkey() - { - // We have to store the LowLevelKeyboardProc, so that it is not garbage collected runtime - hookedLowLevelKeyboardProc = LowLevelKeyboardProc; - // Set the hook - hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc); - } - - public SpecialKeyState CheckModifiers() - { - SpecialKeyState state = new SpecialKeyState(); - if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0) - { - //SHIFT is pressed - state.ShiftPressed = true; - } - if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0) - { - //CONTROL is pressed - state.CtrlPressed = true; - } - if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0) - { - //ALT is pressed - state.AltPressed = true; - } - if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0) - { - //ALT is pressed - state.WinPressed = true; - } - - return state; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam) - { - bool continues = true; - - if (nCode >= 0) - { - if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN || - wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP || - wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN || - wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP) - { - if (hookedKeyboardCallback != null) - continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers()); - } - } - - if (continues) - { - return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam); - } - return (IntPtr)1; - } - - ~GloablHotkey() - { - Dispose(); - } - - public void Dispose() - { - InterceptKeys.UnhookWindowsHookEx(hookId); - } - } - public static class InterceptKeys { public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam); diff --git a/Wox.Infrastructure/GlobalHotkey.cs b/Wox.Infrastructure/GlobalHotkey.cs new file mode 100644 index 000000000..4dc2ec259 --- /dev/null +++ b/Wox.Infrastructure/GlobalHotkey.cs @@ -0,0 +1,94 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Wox.Plugin; + +namespace Wox.Infrastructure +{ + /// + /// Listens keyboard globally. + /// Uses WH_KEYBOARD_LL. + /// + public class GlobalHotkey : IDisposable + { + private InterceptKeys.LowLevelKeyboardProc hookedLowLevelKeyboardProc; + private IntPtr hookId = IntPtr.Zero; + public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state); + public event KeyboardCallback hookedKeyboardCallback; + + //Modifier key constants + private const int VK_SHIFT = 0x10; + private const int VK_CONTROL = 0x11; + private const int VK_ALT = 0x12; + private const int VK_WIN = 91; + + public GlobalHotkey() + { + // We have to store the LowLevelKeyboardProc, so that it is not garbage collected runtime + hookedLowLevelKeyboardProc = LowLevelKeyboardProc; + // Set the hook + hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc); + } + + public SpecialKeyState CheckModifiers() + { + SpecialKeyState state = new SpecialKeyState(); + if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0) + { + //SHIFT is pressed + state.ShiftPressed = true; + } + if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0) + { + //CONTROL is pressed + state.CtrlPressed = true; + } + if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0) + { + //ALT is pressed + state.AltPressed = true; + } + if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0) + { + //ALT is pressed + state.WinPressed = true; + } + + return state; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam) + { + bool continues = true; + + if (nCode >= 0) + { + if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN || + wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP || + wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN || + wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP) + { + if (hookedKeyboardCallback != null) + continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers()); + } + } + + if (continues) + { + return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam); + } + return (IntPtr)1; + } + + ~GlobalHotkey() + { + Dispose(); + } + + public void Dispose() + { + InterceptKeys.UnhookWindowsHookEx(hookId); + } + } +} \ No newline at end of file diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 19392cf06..3e813bc9e 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -52,6 +52,7 @@ + diff --git a/Wox/HotkeyControl.xaml.cs b/Wox/HotkeyControl.xaml.cs index 1d64f0fc7..b957f9548 100644 --- a/Wox/HotkeyControl.xaml.cs +++ b/Wox/HotkeyControl.xaml.cs @@ -41,7 +41,7 @@ namespace Wox Key key = (e.Key == Key.System ? e.SystemKey : e.Key); string text = string.Empty; - SpecialKeyState specialKeyState = new GloablHotkey().CheckModifiers(); + SpecialKeyState specialKeyState = new GlobalHotkey().CheckModifiers(); if (specialKeyState.AltPressed) { text += "Alt"; diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index d0dbc9048..d1cb6aa53 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -35,7 +35,7 @@ namespace Wox public static bool initialized = false; private static readonly List waitShowResultList = new List(); - private readonly GloablHotkey globalHotkey = new GloablHotkey(); + private readonly GlobalHotkey globalHotkey = new GlobalHotkey(); private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator()); private readonly Storyboard progressBarStoryboard = new Storyboard(); private bool WinRStroked; @@ -349,7 +349,7 @@ namespace Wox { bool hideWindow = result.Action(new ActionContext() { - SpecialKeyState = new GloablHotkey().CheckModifiers() + SpecialKeyState = new GlobalHotkey().CheckModifiers() }); if (hideWindow) { From 4419c20f33060b96195f50b7ecc4144bf0e18404 Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Tue, 18 Mar 2014 17:33:51 +0100 Subject: [PATCH 2/4] move files to new globalhotkey file --- Wox.Infrastructure/GloablHotkey.cs | 103 ----------------------------- Wox.Infrastructure/GlobalHotkey.cs | 97 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 103 deletions(-) delete mode 100644 Wox.Infrastructure/GloablHotkey.cs 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. From 69204e3998833032277aed7e8a7ca74467257849 Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Tue, 18 Mar 2014 17:34:54 +0100 Subject: [PATCH 3/4] add missing imports + save project changes --- Wox.Infrastructure/GlobalHotkey.cs | 1 + Wox.Infrastructure/Wox.Infrastructure.csproj | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Wox.Infrastructure/GlobalHotkey.cs b/Wox.Infrastructure/GlobalHotkey.cs index 893d7afef..be7cde76a 100644 --- a/Wox.Infrastructure/GlobalHotkey.cs +++ b/Wox.Infrastructure/GlobalHotkey.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Wox.Plugin; diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 3e813bc9e..06326b2f7 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -51,7 +51,6 @@ - From bdc2decd063724ef1ed7dccc7ceadc24391d7e03 Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Tue, 18 Mar 2014 17:36:00 +0100 Subject: [PATCH 4/4] Registe -> Register --- Wox/MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index d1cb6aa53..e42591a8e 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -104,7 +104,7 @@ namespace Wox } catch (Exception) { - MessageBox.Show("Registe hotkey: " + hotkeyStr + " failed."); + MessageBox.Show("Register hotkey: " + hotkeyStr + " failed."); } }