Coree.NETWindows
  • API
Show / Hide Table of Contents
  • Coree.NETWindows
    • KeyModifier
    • Keys
  • Coree.NETWindows.Classes
    • GlobalHotkey
  • Coree.NETWindows.NativeMethods
    • ConsoleManagement
    • ConsoleManagement.ConsoleColors
  • Coree.NETWindows.Services.PInvoke
    • IPInvokeService
    • PInvokeService
  • Coree.NETWindows.Utilities
    • MainContext

Class GlobalHotkey

Manages a global hotkey registration that allows for handling key events system-wide.

Inheritance
object
GlobalHotkey
Implements
IDisposable
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: Coree.NETWindows.Classes
Assembly: Coree.NETWindows.dll
Syntax
public class GlobalHotkey : IDisposable

Constructors

| Edit this page View Source

GlobalHotkey(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.

| Edit this page View Source

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 Source

Id

Gets the identifier for this hotkey instance.

Declaration
public int Id { get; }
Property Value
Type Description
int

Methods

| Edit this page View Source

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Declaration
public void Dispose()
| Edit this page View Source

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).

| Edit this page View Source

~GlobalHotkey()

Finalizer that ensures the hotkey is unregistered when the object is collected by the garbage collector.

Declaration
protected ~GlobalHotkey()
| Edit this page View Source

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.

| Edit this page View Source

Register()

Registers the hotkey with the operating system.

Declaration
public bool Register()
Returns
Type Description
bool

True if registration is successful; otherwise false.

Implements

IDisposable
  • Edit this page
  • View Source
In this article
Back to top