0

我想为不同的热键按下执行两种方法,检查 if-else 按下了什么键

如何用这个例子做到这一点?

我这样使用:

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");
}

如何使用不同的热键执行两个不同的事件?

4

1 回答 1

4

您需要注册所有热键,然后才能使用Key和的Modifiers属性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.");
}
于 2017-12-15T17:22:59.417 回答