GetAsyncKeyState is a bad idea as it may ignore two combined keys, RegisterHotkey too as you don't need to hook system keys.
I'd prefer this method, maybe a bit risky, by overriding the ProcessCmdKey method. It should works just with forms.
Code:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.A)
{
myButton.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData); // return the base method to let process shortcuts or hotkeys you are not handling yourself
}
if you need to handle more hotkeys, I suggest to do a switch case to make it more readable. You could make a wrapper for this if you are too lazy to handle the hotkeys yourself. Something like this should give you an idea:
Code:
using System.Windows.Forms;
class hotkeyButton : Button
{
private Keys _hotkey;
public Keys Hotkey
{
get { return _hotkey; }
set
{
if(_hotkey != value)
{
_hotkey = value;
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == _hotkey)
{
this.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
build, go to the toolbox, put an hotkeyButton in your form, explore its properties, find the Hotkey property and select your favourite hotkey.