mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
gloablHotkey -> GlobalHotkey
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using Wox.Plugin;
|
|
||||||
|
|
||||||
namespace Wox.Infrastructure
|
namespace Wox.Infrastructure
|
||||||
{
|
{
|
||||||
@@ -29,93 +27,6 @@ namespace Wox.Infrastructure
|
|||||||
WM_SYSKEYDOWN = 260
|
WM_SYSKEYDOWN = 260
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Listens keyboard globally.
|
|
||||||
/// <remarks>Uses WH_KEYBOARD_LL.</remarks>
|
|
||||||
/// </summary>
|
|
||||||
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 static class InterceptKeys
|
||||||
{
|
{
|
||||||
public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam);
|
public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam);
|
||||||
|
|||||||
94
Wox.Infrastructure/GlobalHotkey.cs
Normal file
94
Wox.Infrastructure/GlobalHotkey.cs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using Wox.Plugin;
|
||||||
|
|
||||||
|
namespace Wox.Infrastructure
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Listens keyboard globally.
|
||||||
|
/// <remarks>Uses WH_KEYBOARD_LL.</remarks>
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,7 @@
|
|||||||
<Compile Include="CommonStorage.cs" />
|
<Compile Include="CommonStorage.cs" />
|
||||||
<Compile Include="FuzzyMatcher.cs" />
|
<Compile Include="FuzzyMatcher.cs" />
|
||||||
<Compile Include="GloablHotkey.cs" />
|
<Compile Include="GloablHotkey.cs" />
|
||||||
|
<Compile Include="GlobalHotkey.cs" />
|
||||||
<Compile Include="HotkeyModel.cs" />
|
<Compile Include="HotkeyModel.cs" />
|
||||||
<Compile Include="IniParser.cs" />
|
<Compile Include="IniParser.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace Wox
|
|||||||
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
|
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
|
||||||
|
|
||||||
string text = string.Empty;
|
string text = string.Empty;
|
||||||
SpecialKeyState specialKeyState = new GloablHotkey().CheckModifiers();
|
SpecialKeyState specialKeyState = new GlobalHotkey().CheckModifiers();
|
||||||
if (specialKeyState.AltPressed)
|
if (specialKeyState.AltPressed)
|
||||||
{
|
{
|
||||||
text += "Alt";
|
text += "Alt";
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace Wox
|
|||||||
public static bool initialized = false;
|
public static bool initialized = false;
|
||||||
|
|
||||||
private static readonly List<Result> waitShowResultList = new List<Result>();
|
private static readonly List<Result> waitShowResultList = new List<Result>();
|
||||||
private readonly GloablHotkey globalHotkey = new GloablHotkey();
|
private readonly GlobalHotkey globalHotkey = new GlobalHotkey();
|
||||||
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
|
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
|
||||||
private readonly Storyboard progressBarStoryboard = new Storyboard();
|
private readonly Storyboard progressBarStoryboard = new Storyboard();
|
||||||
private bool WinRStroked;
|
private bool WinRStroked;
|
||||||
@@ -349,7 +349,7 @@ namespace Wox
|
|||||||
{
|
{
|
||||||
bool hideWindow = result.Action(new ActionContext()
|
bool hideWindow = result.Action(new ActionContext()
|
||||||
{
|
{
|
||||||
SpecialKeyState = new GloablHotkey().CheckModifiers()
|
SpecialKeyState = new GlobalHotkey().CheckModifiers()
|
||||||
});
|
});
|
||||||
if (hideWindow)
|
if (hideWindow)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user