Skip to content

Commit 22170ee

Browse files
committed
Replace DllImport & flags with CSWin32
1 parent 79f8f05 commit 22170ee

File tree

7 files changed

+114
-182
lines changed

7 files changed

+114
-182
lines changed

Flow.Launcher.Infrastructure/FileExplorerHelper.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Runtime.InteropServices;
5+
using Windows.Win32;
66

77
namespace Flow.Launcher.Infrastructure
88
{
@@ -54,10 +54,6 @@ private static dynamic GetActiveExplorer()
5454
return explorerWindows.Zip(zOrders).MinBy(x => x.Second).First;
5555
}
5656

57-
[DllImport("user32.dll")]
58-
[return: MarshalAs(UnmanagedType.Bool)]
59-
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
60-
6157
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
6258

6359
/// <summary>
@@ -70,9 +66,9 @@ private static IEnumerable<int> GetZOrder(List<dynamic> hWnds)
7066

7167
var index = 0;
7268
var numRemaining = hWnds.Count;
73-
EnumWindows((wnd, _) =>
69+
PInvoke.EnumWindows((wnd, _) =>
7470
{
75-
var searchIndex = hWnds.FindIndex(x => x.HWND == wnd.ToInt32());
71+
var searchIndex = hWnds.FindIndex(x => x.HWND == wnd.Value);
7672
if (searchIndex != -1)
7773
{
7874
z[searchIndex] = index;

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<Prefer32Bit>false</Prefer32Bit>
3636
</PropertyGroup>
3737

38+
<ItemGroup>
39+
<AdditionalFiles Include="NativeMethods.txt" />
40+
</ItemGroup>
41+
3842
<ItemGroup>
3943
<Compile Include="..\SolutionAssemblyInfo.cs" Link="Properties\SolutionAssemblyInfo.cs" />
4044
<None Include="FodyWeavers.xml" />
@@ -56,6 +60,10 @@
5660
</PackageReference>
5761
<PackageReference Include="MemoryPack" Version="1.21.3" />
5862
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
63+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
64+
<PrivateAssets>all</PrivateAssets>
65+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
66+
</PackageReference>
5967
<PackageReference Include="NLog" Version="4.7.10" />
6068
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
6169
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
using System;
1+
using System;
2+
using System.Diagnostics;
23
using System.Runtime.InteropServices;
34
using Flow.Launcher.Plugin;
5+
using Windows.Win32;
6+
using Windows.Win32.Foundation;
7+
using Windows.Win32.UI.Input.KeyboardAndMouse;
8+
using Windows.Win32.UI.WindowsAndMessaging;
49

510
namespace Flow.Launcher.Infrastructure.Hotkey
611
{
@@ -10,44 +15,41 @@ namespace Flow.Launcher.Infrastructure.Hotkey
1015
/// </summary>
1116
public unsafe class GlobalHotkey : IDisposable
1217
{
13-
private static readonly IntPtr hookId;
14-
15-
16-
17-
public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state);
18-
internal static Func<KeyEvent, int, SpecialKeyState, bool> hookedKeyboardCallback;
18+
private static readonly UnhookWindowsHookExSafeHandle hookId;
1919

20-
//Modifier key constants
21-
private const int VK_SHIFT = 0x10;
22-
private const int VK_CONTROL = 0x11;
23-
private const int VK_ALT = 0x12;
24-
private const int VK_WIN = 91;
20+
public delegate bool KeyboardCallback(int keyEvent, int vkCode, SpecialKeyState state);
21+
internal static Func<KeyEvent, int, SpecialKeyState, bool> hookedKeyboardCallback;
2522

2623
static GlobalHotkey()
2724
{
2825
// Set the hook
29-
hookId = InterceptKeys.SetHook(& LowLevelKeyboardProc);
26+
using Process curProcess = Process.GetCurrentProcess();
27+
using ProcessModule curModule = curProcess.MainModule;
28+
hookId = PInvoke.SetWindowsHookEx(
29+
WINDOWS_HOOK_ID.WH_KEYBOARD_LL,
30+
LowLevelKeyboardProc,
31+
PInvoke.GetModuleHandle(curModule.ModuleName), 0);
3032
}
3133

3234
public static SpecialKeyState CheckModifiers()
3335
{
3436
SpecialKeyState state = new SpecialKeyState();
35-
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0)
37+
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_SHIFT) & 0x8000) != 0)
3638
{
3739
//SHIFT is pressed
3840
state.ShiftPressed = true;
3941
}
40-
if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0)
42+
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_CONTROL) & 0x8000) != 0)
4143
{
4244
//CONTROL is pressed
4345
state.CtrlPressed = true;
4446
}
45-
if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0)
47+
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_MENU) & 0x8000) != 0)
4648
{
4749
//ALT is pressed
4850
state.AltPressed = true;
4951
}
50-
if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0)
52+
if ((PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_LWIN) & 0x8000) != 0)
5153
{
5254
//WIN is pressed
5355
state.WinPressed = true;
@@ -56,38 +58,38 @@ public static SpecialKeyState CheckModifiers()
5658
return state;
5759
}
5860

59-
[UnmanagedCallersOnly]
60-
private static IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
61+
private static LRESULT LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
6162
{
6263
bool continues = true;
6364

6465
if (nCode >= 0)
6566
{
66-
if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN ||
67-
wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP ||
68-
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN ||
69-
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP)
67+
var wParamValue = (int)wParam.Value;
68+
if (wParamValue == (int)KeyEvent.WM_KEYDOWN ||
69+
wParamValue == (int)KeyEvent.WM_KEYUP ||
70+
wParamValue == (int)KeyEvent.WM_SYSKEYDOWN ||
71+
wParamValue == (int)KeyEvent.WM_SYSKEYUP)
7072
{
7173
if (hookedKeyboardCallback != null)
72-
continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers());
74+
continues = hookedKeyboardCallback((KeyEvent)wParamValue, Marshal.ReadInt32(lParam), CheckModifiers());
7375
}
7476
}
7577

7678
if (continues)
7779
{
78-
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
80+
return PInvoke.CallNextHookEx(hookId, nCode, wParam, lParam);
7981
}
80-
return (IntPtr)(-1);
82+
return new LRESULT(-1);
8183
}
8284

8385
public void Dispose()
8486
{
85-
InterceptKeys.UnhookWindowsHookEx(hookId);
87+
PInvoke.UnhookWindowsHookEx(new HHOOK(hookId.DangerousGetHandle()));
8688
}
8789

8890
~GlobalHotkey()
8991
{
9092
Dispose();
9193
}
9294
}
93-
}
95+
}

Flow.Launcher.Infrastructure/Hotkey/InterceptKeys.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1+
using Windows.Win32;
2+
13
namespace Flow.Launcher.Infrastructure.Hotkey
24
{
35
public enum KeyEvent
46
{
57
/// <summary>
68
/// Key down
79
/// </summary>
8-
WM_KEYDOWN = 256,
10+
WM_KEYDOWN = (int)PInvoke.WM_KEYDOWN,
911

1012
/// <summary>
1113
/// Key up
1214
/// </summary>
13-
WM_KEYUP = 257,
15+
WM_KEYUP = (int)PInvoke.WM_KEYUP,
1416

1517
/// <summary>
1618
/// System key up
1719
/// </summary>
18-
WM_SYSKEYUP = 261,
20+
WM_SYSKEYUP = (int)PInvoke.WM_SYSKEYUP,
1921

2022
/// <summary>
2123
/// System key down
2224
/// </summary>
23-
WM_SYSKEYDOWN = 260
25+
WM_SYSKEYDOWN = (int)PInvoke.WM_SYSKEYDOWN
2426
}
25-
}
27+
}

0 commit comments

Comments
 (0)