Skip to content

Commit ff1c894

Browse files
committed
Issue #42
1 parent 44b9846 commit ff1c894

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

SmartSystemMenu/Forms/MainForm.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,28 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
735735
private void HotKeyHooked(object sender, HotKeyEventArgs e)
736736
{
737737
var handle = NativeMethods.GetForegroundWindow();
738-
NativeMethods.PostMessage(handle, NativeConstants.WM_SYSCOMMAND, (uint)e.MenuItemId, 0);
738+
var systemMenuHandle = NativeMethods.GetSystemMenu(handle, false);
739+
740+
if (handle != null && handle != IntPtr.Zero && systemMenuHandle != null && systemMenuHandle != IntPtr.Zero)
741+
{
742+
var processName = "";
743+
744+
try
745+
{
746+
NativeMethods.GetWindowThreadProcessId(handle, out var processId);
747+
var process = SystemUtils.GetProcessByIdSafely(processId);
748+
processName = Path.GetFileName(process.GetMainModuleFileName());
749+
}
750+
catch
751+
{
752+
}
753+
754+
if (!_settings.ProcessExclusions.Contains(processName.ToLower()))
755+
{
756+
NativeMethods.PostMessage(handle, NativeConstants.WM_SYSCOMMAND, (uint)e.MenuItemId, 0);
757+
e.Succeeded = true;
758+
}
759+
}
739760
}
740761

741762
private void SetPriorityMenuItem(Window window, int itemId, Priority priority)

SmartSystemMenu/HotKeys/HotKeyEventArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class HotKeyEventArgs : EventArgs
66
{
77
public int MenuItemId { get; private set; }
88

9+
public bool Succeeded { get; set; }
10+
911
public HotKeyEventArgs(int menuItemId)
1012
{
1113
MenuItemId = menuItemId;

SmartSystemMenu/HotKeys/HotKeyHook.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ private int HookProc(int code, IntPtr wParam, ref KeyboardLLHookStruct lParam)
8787
{
8888
var menuItemId = MenuItemId.GetId(item.Name);
8989
var eventArgs = new HotKeyEventArgs(menuItemId);
90-
handler.BeginInvoke(this, eventArgs, null, null);
90+
handler.Invoke(this, eventArgs);
91+
if (eventArgs.Succeeded)
92+
{
93+
return 1;
94+
}
9195
}
9296
}
9397
}
@@ -115,7 +119,11 @@ private int HookProc(int code, IntPtr wParam, ref KeyboardLLHookStruct lParam)
115119
if (handler != null)
116120
{
117121
var eventArgs = new HotKeyEventArgs(item.Id);
118-
handler.BeginInvoke(this, eventArgs, null, null);
122+
handler.Invoke(this, eventArgs);
123+
if (eventArgs.Succeeded)
124+
{
125+
return 1;
126+
}
119127
}
120128
}
121129
}

0 commit comments

Comments
 (0)