Class GlobalHotkey
Manages a global hotkey registration that allows for handling key events system-wide.
Implements
Inherited Members
Namespace: Coree.NETWindows.Classes
Assembly: Coree.NETWindows.dll
Syntax
public class GlobalHotkey : IDisposable
Constructors
| Edit this page View SourceGlobalHotkey(IntPtr, Keys, KeyModifier, Action?, bool)
Initializes a new instance of the GlobalHotkey class with specified key and modifier, allowing actions to be attached.
private GlobalHotkey? HelloWorldHotKey;
public Form1()
{
InitializeComponent();
this.CreateHandle();
HelloWorldHotKey = new GlobalHotkey(this.Handle, Coree.NETWindows.Keys.H, Coree.NETWindows.KeyModifier.Alt, () =>
{
MessageBox.Show("Hello World !");
});
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
HelloWorldHotKey?.Match(m.Msg, m.WParam);
}
Declaration
public GlobalHotkey(IntPtr handle, Keys key, KeyModifier modifier, Action? action, bool autoRegister = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | handle | The handle to the window that will process the hotkey event. |
Keys | key | The key code. |
KeyModifier | modifier | The modifier. |
Action | action | The action to execute when the hotkey is pressed. |
bool | autoRegister | Whether to automatically register the hotkey after initialization. |
GlobalHotkey(IntPtr, int, int, Action?, bool)
Initializes a new instance of the GlobalHotkey class with specified key and modifier using integer values, allowing actions to be attached.
private GlobalHotkey? HelloWorldHotKey;
public Form1()
{
InitializeComponent();
this.CreateHandle();
HelloWorldHotKey = new GlobalHotkey(this.Handle, Coree.NETWindows.Keys.H, Coree.NETWindows.KeyModifier.Alt, () =>
{
MessageBox.Show("Hello World !");
});
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
HelloWorldHotKey?.Match(m.Msg, m.WParam);
}
Declaration
public GlobalHotkey(IntPtr handle, int key, int modifier, Action? action, bool autoRegister = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | handle | The handle to the window that will process the hotkey event. |
int | key | The key code represented as an integer. |
int | modifier | The modifier code represented as an integer. |
Action | action | The action to execute when the hotkey is pressed. |
bool | autoRegister | Whether to automatically register the hotkey after initialization. |
Properties
| Edit this page View SourceId
Gets the identifier for this hotkey instance.
Declaration
public int Id { get; }
Property Value
Type | Description |
---|---|
int |
Methods
| Edit this page View SourceDispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Dispose(bool)
Disposes of the hotkey by unregistering it and marking the instance as disposed.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Indicates whether the method call comes from a Dispose method (its value is true) or from a finalizer (its value is false). |
~GlobalHotkey()
Finalizer that ensures the hotkey is unregistered when the object is collected by the garbage collector.
Declaration
protected ~GlobalHotkey()
Match(int, IntPtr)
Checks if the incoming system message corresponds to the hotkey event and invokes the action if it does.
Declaration
public void Match(int msg, IntPtr wparam)
Parameters
Type | Name | Description |
---|---|---|
int | msg | The message identifier. |
IntPtr | wparam | The message's WPARAM value, which should match the hotkey ID. |
Register()
Registers the hotkey with the operating system.
Declaration
public bool Register()
Returns
Type | Description |
---|---|
bool | True if registration is successful; otherwise false. |