-
-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace DllImport and flags with CSWin32 & Remove useless DllImport and flags & Fix right Win key click issue #3122
Changes from 11 commits
a395936
ca01b25
313f86b
ccfc39e
79f8f05
22170ee
ce8b42b
dbe626d
be72bb7
49fc0b8
8a05c60
a088f91
a899ff8
b96c7b1
532b5dc
3ab1fb1
9fb0233
1073821
6f89909
0153f71
02a4566
9350e82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,11 @@ | ||||||
using System; | ||||||
using System; | ||||||
using System.Diagnostics; | ||||||
using System.Runtime.InteropServices; | ||||||
using Flow.Launcher.Plugin; | ||||||
using Windows.Win32; | ||||||
using Windows.Win32.Foundation; | ||||||
using Windows.Win32.UI.Input.KeyboardAndMouse; | ||||||
using Windows.Win32.UI.WindowsAndMessaging; | ||||||
|
||||||
namespace Flow.Launcher.Infrastructure.Hotkey | ||||||
{ | ||||||
|
@@ -10,44 +15,45 @@ namespace Flow.Launcher.Infrastructure.Hotkey | |||||
/// </summary> | ||||||
public unsafe class GlobalHotkey : IDisposable | ||||||
{ | ||||||
private static readonly IntPtr hookId; | ||||||
|
||||||
|
||||||
|
||||||
private static readonly HOOKPROC _procKeyboard = HookKeyboardCallback; | ||||||
private static readonly UnhookWindowsHookExSafeHandle hookId; | ||||||
|
||||||
public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state); | ||||||
internal static Func<KeyEvent, int, SpecialKeyState, bool> 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; | ||||||
|
||||||
static GlobalHotkey() | ||||||
{ | ||||||
// Set the hook | ||||||
hookId = InterceptKeys.SetHook(& LowLevelKeyboardProc); | ||||||
hookId = SetHook(_procKeyboard, WINDOWS_HOOK_ID.WH_KEYBOARD_LL); | ||||||
} | ||||||
|
||||||
private static UnhookWindowsHookExSafeHandle SetHook(HOOKPROC proc, WINDOWS_HOOK_ID hookId) | ||||||
{ | ||||||
using var curProcess = Process.GetCurrentProcess(); | ||||||
using var curModule = curProcess.MainModule; | ||||||
return PInvoke.SetWindowsHookEx(hookId, proc, PInvoke.GetModuleHandle(curModule.ModuleName), 0); | ||||||
} | ||||||
|
||||||
public static SpecialKeyState CheckModifiers() | ||||||
{ | ||||||
SpecialKeyState state = new SpecialKeyState(); | ||||||
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0) | ||||||
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_SHIFT) & 0x8000) != 0) | ||||||
{ | ||||||
//SHIFT is pressed | ||||||
state.ShiftPressed = true; | ||||||
} | ||||||
if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0) | ||||||
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_CONTROL) & 0x8000) != 0) | ||||||
{ | ||||||
//CONTROL is pressed | ||||||
state.CtrlPressed = true; | ||||||
} | ||||||
if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0) | ||||||
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_MENU) & 0x8000) != 0) | ||||||
{ | ||||||
//ALT is pressed | ||||||
state.AltPressed = true; | ||||||
} | ||||||
if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0) | ||||||
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_LWIN) & 0x8000) != 0 || | ||||||
(PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_RWIN) & 0x8000) != 0) | ||||||
{ | ||||||
//WIN is pressed | ||||||
state.WinPressed = true; | ||||||
|
@@ -56,38 +62,38 @@ public static SpecialKeyState CheckModifiers() | |||||
return state; | ||||||
} | ||||||
|
||||||
[UnmanagedCallersOnly] | ||||||
private static IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam) | ||||||
private static LRESULT HookKeyboardCallback(int nCode, WPARAM wParam, LPARAM 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 (wParam.Value == (int)KeyEvent.WM_KEYDOWN || | ||||||
wParam.Value == (int)KeyEvent.WM_KEYUP || | ||||||
wParam.Value == (int)KeyEvent.WM_SYSKEYDOWN || | ||||||
wParam.Value == (int)KeyEvent.WM_SYSKEYUP) | ||||||
{ | ||||||
if (hookedKeyboardCallback != null) | ||||||
continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers()); | ||||||
continues = hookedKeyboardCallback((KeyEvent)wParam.Value, Marshal.ReadInt32(lParam), CheckModifiers()); | ||||||
} | ||||||
} | ||||||
|
||||||
if (continues) | ||||||
{ | ||||||
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam); | ||||||
return PInvoke.CallNextHookEx(hookId, nCode, wParam, lParam); | ||||||
} | ||||||
return (IntPtr)(-1); | ||||||
|
||||||
return new LRESULT(1); | ||||||
} | ||||||
|
||||||
public void Dispose() | ||||||
{ | ||||||
InterceptKeys.UnhookWindowsHookEx(hookId); | ||||||
PInvoke.UnhookWindowsHookEx(new HHOOK(hookId.DangerousGetHandle())); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Dispose of the hook handle correctly In the Apply this diff: public void Dispose()
{
- PInvoke.UnhookWindowsHookEx(new HHOOK(hookId.DangerousGetHandle()));
+ hookId.Dispose();
} 📝 Committable suggestion
Suggested change
|
||||||
|
||||||
~GlobalHotkey() | ||||||
{ | ||||||
Dispose(); | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
using Windows.Win32; | ||
|
||
namespace Flow.Launcher.Infrastructure.Hotkey | ||
{ | ||
public enum KeyEvent | ||
{ | ||
/// <summary> | ||
/// Key down | ||
/// </summary> | ||
WM_KEYDOWN = 256, | ||
WM_KEYDOWN = (int)PInvoke.WM_KEYDOWN, | ||
|
||
/// <summary> | ||
/// Key up | ||
/// </summary> | ||
WM_KEYUP = 257, | ||
WM_KEYUP = (int)PInvoke.WM_KEYUP, | ||
|
||
/// <summary> | ||
/// System key up | ||
/// </summary> | ||
WM_SYSKEYUP = 261, | ||
WM_SYSKEYUP = (int)PInvoke.WM_SYSKEYUP, | ||
|
||
/// <summary> | ||
/// System key down | ||
/// </summary> | ||
WM_SYSKEYDOWN = 260 | ||
WM_SYSKEYDOWN = (int)PInvoke.WM_SYSKEYDOWN | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jack251970 I am getting an error from using the built-in shortcut active explorer path, could you please check if it's caused by this change.
Repro:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jjw24 Check #3168 please