Listening To Hotkeys
Including hotkey-listening logic at runtime
To listen to key presses, use the HotkeyPressed
event:
// Add a HotkeyPressed event.
hkl.HotkeyPressed += Hkl_HotkeyPressed;
private void Hkl_HotkeyPressed(object sender, HotkeyEventArgs e)
{
if (e.Hotkey == hotkey1)
MessageBox.Show($"First hotkey '{hotkey1}' was pressed.");
if (e.Hotkey == hotkey2)
MessageBox.Show($"Second hotkey '{hotkey2}'was pressed.");
}
Unlike with the standard KeyDown
and KeyUp
events, here only the registered hotkeys will be detected.
If you'd like to get the details of the active application where a hotkey was pressed, use the SourceApplication
argument property:
private void Hkl_HotkeyPressed(object sender, HotkeyEventArgs e)
{
if (e.Hotkey == hotkey1)
{
MessageBox.Show(
"Application:" + e.SourceApplication.Name + "\n" +
"Title: " + e.SourceApplication.Title + "\n" +
"ID: " + e.SourceApplication.ID + "\n" +
"Handle: " + e.SourceApplication.Handle + "\n" +
"Path: " + e.SourceApplication.Path + "\n"
);
}
}
As a special feature (still in Beta
though), if you'd like to get any text that may have been selected when a hotkey added was pressed, use Hotkey Listener's GetSelection()
method:
private void Hkl_HotkeyPressed(object sender, HotkeyEventArgs e)
{
if (e.Hotkey == hotkey2)
{
// Get the selected text, if any.
string selection = hkl.GetSelection();
// If some text was selected, display a MessageBox.
if (selection != string.Empty)
MessageBox.Show(selection);
}
}
Last updated
Was this helpful?