Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions MouseTrap/src/Forms/ConfigFrom.Designer.cs

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

29 changes: 28 additions & 1 deletion MouseTrap/src/MouseTrapTrayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MouseTrap.Forms;
using MouseTrap.Forms;
using MouseTrap.Models;
using MouseTrap.Service;

Expand All @@ -10,6 +10,8 @@ public class MouseTrapTrayIcon : TrayIcon {
private WeakReference<ConfigFrom>? _configFromRef;


private GlobalHotkeyManager? _hotkeys;
private Form? _hotkeyForm;
public MouseTrapTrayIcon(ServiceThread service)
{
_service = service;
Expand All @@ -33,6 +35,20 @@ public MouseTrapTrayIcon(ServiceThread service)
ContextMenu.Items[0].Font = WithFontStyle(ContextMenu.Items[0].Font, FontStyle.Bold);

Visible = true;
// initialize hotkeys using a minimal form
_hotkeyForm = new Form { ShowInTaskbar = false, WindowState = FormWindowState.Minimized, Opacity = 0 };
_hotkeyForm.Load += (s, e) => _hotkeyForm.Hide();
_hotkeyForm.Show();
_hotkeys = new GlobalHotkeyManager(_hotkeyForm.Handle);
_hotkeys.EnableRequested += () => {
_service.StartService();
((ToolStripMenuItem)ContextMenu.Items[1]).Checked = true;
};
_hotkeys.DisableRequested += () => {
_service.StopService();
((ToolStripMenuItem)ContextMenu.Items[1]).Checked = false;
};
_hotkeys.Register();

// show config form on first startup
var settings = Settings.Load();
Expand Down Expand Up @@ -77,6 +93,7 @@ protected override void WndProc(ref Message m)
Close();
}

_hotkeys?.HandleWndProc(ref m);
_service.WndProc(ref m);

base.WndProc(ref m);
Expand All @@ -90,4 +107,14 @@ private static Font WithFontStyle(Font font, FontStyle style)
{
return new Font(font.Name, font.Size, style, font.Unit);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_hotkeys?.Unregister();
_hotkeys?.Dispose();
_hotkeyForm?.Dispose();
}
base.Dispose(disposing);
}
}
91 changes: 91 additions & 0 deletions MouseTrap/src/Service/GlobalHotkeyManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MouseTrap.Service;

public sealed class GlobalHotkeyManager : IDisposable
{
private const int WM_HOTKEY = 0x0312;

private const int HOTKEY_ENABLE = 1;
private const int HOTKEY_DISABLE = 2;

private const uint MOD_WIN = 0x0008;

private readonly IntPtr _windowHandle;
private bool _registered;

public event Action? EnableRequested;
public event Action? DisableRequested;

public GlobalHotkeyManager(IntPtr windowHandle)
{
_windowHandle = windowHandle;
}

public void Register()
{
if (_registered)
return;

// Win + PageUp
RegisterHotKey(_windowHandle, HOTKEY_ENABLE, MOD_WIN, (uint)Keys.PageUp);

// Win + PageDown
RegisterHotKey(_windowHandle, HOTKEY_DISABLE, MOD_WIN, (uint)Keys.PageDown);

_registered = true;
}

public void Unregister()
{
if (!_registered)
return;

UnregisterHotKey(_windowHandle, HOTKEY_ENABLE);
UnregisterHotKey(_windowHandle, HOTKEY_DISABLE);

_registered = false;
}

public void HandleWndProc(ref Message m)
{
if (m.Msg != WM_HOTKEY)
return;

switch ((int)m.WParam)
{
case HOTKEY_ENABLE:
EnableRequested?.Invoke();
break;

case HOTKEY_DISABLE:
DisableRequested?.Invoke();
break;
}
}

public void Dispose()
{
Unregister();
}

#region Win32

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
uint fsModifiers,
uint vk
);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);

#endregion
}
Loading