I tried using this post here: Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C# And i have this sucessfully working.
But there is something i cant get my finger be
To ensure that the key combination is actually pressed by the user, you have to check the state of both key and thus you have to keep track of their state.
An approach can be :
List<Key> keys = new List<Key>();
void KListener_KeyDown(object sender, RawKeyEventArgs args)
{
SetKeyDown(args.key);
if(IsKeyDown(Key.LeftCtrl) && IsKeyDown(Key.C))
MessageBox.Show("Woot!");
}
void KListener_KeyUp(object sender, RawKeyEventArgs args)
{
SetKeyUp(args.key);
}
private bool IsKeyDown(Key key)
{
return keys.Contains(key);
}
private void SetKeyDown(Key key)
{
if(!keys.Contains(key))
keys.Add(key);
}
private void SetKeyUp(Key key)
{
if(keys.Contains(key))
keys.Remove(key);
}
Store the value of the key that is pressed and the next time your method is called check if this stored value and the actual value are your key combination.
var lastKey;
void KListener_KeyDown(object sender, RawKeyEventArgs args)
{
Console.WriteLine(args.Key.ToString());
if (lastKey == Key.LeftCtrl && args.Key == Key.C)
{
MessageBox.Show(args.Key.ToString());
}
lastKey = args.Key;
}
Some time ago I relied on the RegisterHotKey function and the result was pretty good. I used this in VB.NET and just for combinations of CTRL/SHIFT/ALT + letter or number, but here you have a pretty detailed C# code allowing even more combinations.