45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
|
|||
|
namespace TibisMathematicsSuite.KeyboardIO
|
|||
|
{
|
|||
|
public static class Hotkey
|
|||
|
{
|
|||
|
private static HotkeyWindow window = new HotkeyWindow();
|
|||
|
private static int currentId = 0;
|
|||
|
|
|||
|
public static event EventHandler<MyKeyEventArgs> HotkeyPressed
|
|||
|
{
|
|||
|
add { window.HotkeyPressed += value; }
|
|||
|
remove { window.HotkeyPressed -= value; }
|
|||
|
}
|
|||
|
|
|||
|
#region Winapi routines
|
|||
|
// Registers a hot key with Windows.
|
|||
|
[DllImport("user32.dll")]
|
|||
|
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
|
|||
|
|
|||
|
// Unregisters the hot key with Windows.
|
|||
|
[DllImport("user32.dll")]
|
|||
|
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
|||
|
#endregion
|
|||
|
|
|||
|
public static void Register(MyKey key)
|
|||
|
{
|
|||
|
currentId++;
|
|||
|
|
|||
|
if (!RegisterHotKey(window.Handle, currentId, (uint)key.Mods, (uint)key.Key))
|
|||
|
throw new InvalidOperationException("Could not register the hotkey!");
|
|||
|
}
|
|||
|
|
|||
|
public static void UnregisterAll()
|
|||
|
{
|
|||
|
for (; currentId > 0; currentId--)
|
|||
|
UnregisterHotKey(window.Handle, currentId);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|