Skip to content

Commit

Permalink
Update Mouse Hook on First Addin Init to avoid crash during using excel.
Browse files Browse the repository at this point in the history
  • Loading branch information
KitCognac committed Aug 11, 2020
1 parent 00b1ba0 commit adc2dba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions XLIG/Init.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ExcelDna.Integration;
using ExcelDna.IntelliSense;
using Excel = Microsoft.Office.Interop.Excel;
using Gma.System.MouseKeyHook;

namespace XL_IGNITION
{
Expand All @@ -19,6 +20,8 @@ public void AutoOpen()
IntelliSenseServer.Install();
// Ref Current Excel App to Global Var for further usage
AddinContext.XlApp = (Excel.Application)ExcelDnaUtil.Application;
// Hook Mouse on First Load
MouseHook_Main.M_AppHook = Hook.AppEvents();

}
public void AutoClose()
Expand All @@ -27,6 +30,8 @@ public void AutoClose()
IntelliSenseServer.Uninstall();
// Kill Shadow Excel Instance
AddinContext.XlApp.Quit();
// Dispose Mouse Hook
MouseHook_Main.M_AppHook.Dispose();
}
}

Expand Down
6 changes: 3 additions & 3 deletions XLIG/MouseHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace XL_IGNITION
public class MouseHook_Main
{
private static Keys HoldingKey = Keys.None;
static IKeyboardMouseEvents M_AppHook;
public static IKeyboardMouseEvents M_AppHook;
public static void Init_Unload(bool state)
{
if (state)
{
// Note: for the application hook, use the Hook.AppEvents() instead Hook.GlobalEvents();
M_AppHook = Hook.AppEvents();
// M_AppHook = Hook.AppEvents();
M_AppHook.KeyDown += Hook_KeyDown;
M_AppHook.KeyUp += Hook_KeyUp;
M_AppHook.MouseWheelExt += Hook_MWheelExt;
Expand All @@ -23,7 +23,7 @@ public static void Init_Unload(bool state)
M_AppHook.KeyDown -= Hook_KeyDown;
M_AppHook.KeyUp -= Hook_KeyUp;
M_AppHook.MouseWheelExt -= Hook_MWheelExt;
M_AppHook.Dispose();
// M_AppHook.Dispose();
}

}
Expand Down

0 comments on commit adc2dba

Please sign in to comment.