Use Safe Delegate Instead

This commit is contained in:
Kevin Zhang 2021-12-05 21:04:41 -06:00
parent 84528ae8a7
commit 5f9036568d
2 changed files with 6 additions and 5 deletions

View file

@ -12,8 +12,8 @@ namespace Flow.Launcher.Infrastructure.Hotkey
public unsafe class GlobalHotkey : IDisposable
{
private static readonly IntPtr hookId;
private static InterceptKeys.LowLevelKeyboardProc _hookedKeyboardProc;
public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state);
internal static Func<KeyEvent, int, SpecialKeyState, bool> hookedKeyboardCallback;
@ -26,8 +26,9 @@ namespace Flow.Launcher.Infrastructure.Hotkey
static GlobalHotkey()
{
_hookedKeyboardProc = LowLevelKeyboardProc;
// Set the hook
hookId = InterceptKeys.SetHook(& LowLevelKeyboardProc);
hookId = InterceptKeys.SetHook(_hookedKeyboardProc);
}
public static SpecialKeyState CheckModifiers()

View file

@ -10,7 +10,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey
private const int WH_KEYBOARD_LL = 13;
public static IntPtr SetHook(delegate*<int, UIntPtr, IntPtr, IntPtr> proc)
public static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
@ -20,7 +20,7 @@ namespace Flow.Launcher.Infrastructure.Hotkey
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, delegate*<int, UIntPtr, IntPtr, IntPtr> lpfn, IntPtr hMod, uint dwThreadId);
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)]