0

I want to perform two methods for different pressing of hot keys, checking in if-else what key was pressed

How to do this with this example?

I use so:

private static void Main()
{
  HotKeyManager.RegisterHotKey(key: Keys.G, modifiers: HotKeyEventArgs.KeyModifiers.Alt);
  HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
  Console.ReadLine();
}
private static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
  Console.WriteLine("Example HotKeys Activate");
}

How do I perform two different events with different HotKeys?

GooliveR
  • 244
  • 4
  • 10

1 Answers1

4

You need to register all your hotkey and then can use the Key and Modifiers properties of the HotKeyEventArgs:

private static void Main()
{
    HotKeyManager.RegisterHotKey(key: Keys.G, modifiers: HotKeyEventArgs.KeyModifiers.Alt);
    HotKeyManager.RegisterHotKey(key: Keys.P, modifiers: HotKeyEventArgs.KeyModifiers.Alt);
    HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
    Console.ReadLine();
}
private static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
    if (e.Key == Keys.G && e.Modifiers == HotKeyEventArgs.KeyModifiers.Alt)
       OnPressedAltG();
    else if (e.Key == Keys.P && e.Modifiers == HotKeyEventArgs.KeyModifiers.Alt)
       OnPressedAltP();
}

private static void OnPressedAltG()
{
    Console.WriteLine("Alt+G was pressed.");
}
private static void OnPressedAltP()
{
    Console.WriteLine("Alt+P was pressed.");
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Does not work, I use together with `HotKeyManager.HotKeyPressed += new EventHandler(HotKeyManager_HotKeyPressed);` – GooliveR Dec 15 '17 at 17:28
  • @GooliveR What does "does not work" mean? Note that "A" and "B" are just examples. And the design of this `HotKeyManager` class does not support multiple events, thatswhy I check the currently pressed hotkey in that single event handler. – René Vogt Dec 15 '17 at 17:31
  • @GooliveR updated the answer to better fit your example. – René Vogt Dec 15 '17 at 17:34