Skip to content

Commit

Permalink
Use GCHandle to hold callback
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmcohn committed Apr 7, 2020
1 parent ff10354 commit 34d5cb1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion OneMore/AddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void StopTimer_Elapsed(object sender, ElapsedEventArgs e)
private void RegisterHotkeys()
{
HotkeyManager.RegisterHotKey(Forms.Keys.F, Hotmods.ControlAlt);
HotkeyManager.RegisterHotKey(Forms.Keys.F, Hotmods.ControlAltShift);
HotkeyManager.RegisterHotKey(Forms.Keys.F, Hotmods.ControlShift);
HotkeyManager.RegisterHotKey(Forms.Keys.OemMinus, Hotmods.AltShift);
HotkeyManager.RegisterHotKey(Forms.Keys.Oemplus, Hotmods.AltShift);
HotkeyManager.RegisterHotKey(Forms.Keys.F4);
Expand Down
27 changes: 22 additions & 5 deletions OneMore/Helpers/Hotkeys/HotkeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace River.OneMoreAddIn
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

Expand Down Expand Up @@ -41,6 +42,7 @@ internal static class HotkeyManager
private static readonly uint threadId;
private static bool registered = false;
private static int counter = 0xE000;
private static GCHandle gch;


static HotkeyManager()
Expand All @@ -50,12 +52,13 @@ static HotkeyManager()
threadId = Native.GetWindowThreadProcessId(mgr.WindowHandle, out _);
}

new Thread(delegate () { Application.Run(new MessageWindow()); })
var mthread = new Thread(delegate () { Application.Run(new MessageWindow()); })
{
Name = $"{nameof(HotkeyManager)}Thread",
IsBackground = true
}
.Start();
};

mthread.Start();
}


Expand Down Expand Up @@ -106,6 +109,11 @@ public static void Unregister()
{
registeredKeys.ForEach(k =>
window.Invoke(new UnRegisterHotkeyDelegate(Unregister), handle, k.Id));

if (gch != null)
{
gch.Free();
}
}


Expand All @@ -128,6 +136,7 @@ private static void OnHotKeyPressed(HotkeyEventArgs e)

private class MessageWindow : Form
{
private readonly Native.WinEventDelegate evDelegate;
private readonly uint msgThreadId;

public MessageWindow()
Expand All @@ -139,21 +148,28 @@ public MessageWindow()
// process started by the OneNote process (with SysWOW64 in its command line)
msgThreadId = Native.GetWindowThreadProcessId(handle, out _);

// maintain a ref so GC doesn't remove it and cause exceptions
evDelegate = new Native.WinEventDelegate(WinEventProc);
gch = GCHandle.Alloc(evDelegate);

// set up event hook to monitor switching application
Native.SetWinEventHook(
Native.EVENT_SYSTEM_FOREGROUND,
Native.EVENT_SYSTEM_MINIMIZEEND,
IntPtr.Zero,
new Native.WinEventDelegate(WinEventProc),
0, 0, Native.WINEVENT_OUTOFCONTEXT);
evDelegate,
0, 0, Native.WINEVENT_OUTOFCONTEXT | Native.WINEVENT_SKIPOWNTHREAD);

resetEvent.Set();
}


private void WinEventProc(
IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
//Logger.Current.WriteLine($"hotkey event:{eventType} thread:{dwEventThread}");

if (eventType == Native.EVENT_SYSTEM_FOREGROUND ||
eventType == Native.EVENT_SYSTEM_MINIMIZESTART ||
eventType == Native.EVENT_SYSTEM_MINIMIZEEND)
Expand Down Expand Up @@ -189,6 +205,7 @@ private void WinEventProc(
}
}


protected override void WndProc(ref Message m)
{
if (m.Msg == Native.WM_HOTKEY)
Expand Down
7 changes: 6 additions & 1 deletion OneMore/Helpers/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ internal static class Native
// Constants...

public const int WM_HOTKEY = 0x312;
public const uint WINEVENT_OUTOFCONTEXT = 0;

public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
public const uint WINEVENT_SKIPOWNTHREAD = 0x0001;
public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
public const uint WINEVENT_INCONTEXT = 0x0004;

public const uint EVENT_SYSTEM_FOREGROUND = 3;
public const uint EVENT_SYSTEM_MINIMIZESTART = 22;
public const uint EVENT_SYSTEM_MINIMIZEEND = 23;
Expand Down
2 changes: 1 addition & 1 deletion OneMore/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion OneMore/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
<value>Add footnote (Ctrl + Alt + F)</value>
</data>
<data name="footnoteRemoveButton_Screentip" xml:space="preserve">
<value>Remove footnote (Shift + Alt + F)</value>
<value>Remove footnote (Ctrl + Shift + F)</value>
</data>
<data name="increaseFontSizeButton_Screentip" xml:space="preserve">
<value>Increase page font size (Ctrl + Alt + Plus)</value>
Expand Down

0 comments on commit 34d5cb1

Please sign in to comment.