Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用if-else定义热键?_C#_Global Hotkey - Fatal编程技术网

C# 如何使用if-else定义热键?

C# 如何使用if-else定义热键?,c#,global-hotkey,C#,Global Hotkey,我想为热键的不同按下执行两种方法,检查是否按了其他键 如何使用示例实现这一点 我这样说: private static void Main() { HotKeyManager.RegisterHotKey(key: Keys.G, modifiers: HotKeyEventArgs.KeyModifiers.Alt); HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager

我想为热键的不同按下执行两种方法,检查是否按了其他键

如何使用示例实现这一点

我这样说:

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");
}
private static void Main()
{
注册表hotKey(key:Keys.G,修饰符:HotKeyEventArgs.KeyModifiers.Alt);
HotKeyManager.HotKeyPressed+=新事件处理程序(HotKeyManager\u HotKeyPressed);
Console.ReadLine();
}
私有静态无效热键管理器\u热键按下(对象发送者,热键事件参数e)
{
控制台.WriteLine(“示例热键激活”);
}

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

您需要注册所有热键,然后可以使用
热键事件参数的
修饰符
属性:

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.");
}
private static void Main()
{
注册表hotKey(key:Keys.G,修饰符:HotKeyEventArgs.KeyModifiers.Alt);
RegisterHotKey(key:Keys.P,修饰符:HotKeyEventArgs.KeyModifiers.Alt);
HotKeyManager.HotKeyPressed+=新事件处理程序(HotKeyManager\u HotKeyPressed);
Console.ReadLine();
}
私有静态无效热键管理器\u热键按下(对象发送者,热键事件参数e)
{
if(e.Key==Keys.G&&e.Modifiers==HotKeyEventArgs.KeyModifiers.Alt)
OnPressedAltG();
else if(e.Key==Keys.P&&e.Modifiers==HotKeyEventArgs.KeyModifiers.Alt)
OnPressedAltP();
}
私有静态void OnPressedAltG()
{
控制台。WriteLine(“按下Alt+G”);
}
私有静态void OnPressedAltP()
{
控制台。WriteLine(“按下Alt+P”);
}

您需要注册所有热键,然后可以使用
热键事件参数的
修饰符
属性

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.");
}
private static void Main()
{
注册表hotKey(key:Keys.G,修饰符:HotKeyEventArgs.KeyModifiers.Alt);
RegisterHotKey(key:Keys.P,修饰符:HotKeyEventArgs.KeyModifiers.Alt);
HotKeyManager.HotKeyPressed+=新事件处理程序(HotKeyManager\u HotKeyPressed);
Console.ReadLine();
}
私有静态无效热键管理器\u热键按下(对象发送者,热键事件参数e)
{
if(e.Key==Keys.G&&e.Modifiers==HotKeyEventArgs.KeyModifiers.Alt)
OnPressedAltG();
else if(e.Key==Keys.P&&e.Modifiers==HotKeyEventArgs.KeyModifiers.Alt)
OnPressedAltP();
}
私有静态void OnPressedAltG()
{
控制台。WriteLine(“按下Alt+G”);
}
私有静态void OnPressedAltP()
{
控制台。WriteLine(“按下Alt+P”);
}

不起作用,我与
HotKeyManager.HotKeyPressed+=neweventhandler(HotKeyManager\u HotKeyPressed)一起使用@GooliveR“不起作用”是什么意思?请注意,“A”和“B”只是示例。这个
HotKeyManager
类的设计不支持多个事件,这就是为什么我在单个事件处理程序中检查当前按下的热键。@GooliveR更新了答案以更好地适应您的示例。不起作用,我与
HotKeyManager.HotKeyPressed+=新事件处理程序(HotKeyManager\u HotKeyPressed)一起使用@GooliveR“不起作用”是什么意思?请注意,“A”和“B”只是示例。这个
HotKeyManager
类的设计不支持多个事件,这就是为什么我在单个事件处理程序中检查当前按下的热键。@GooliveR更新了答案以更好地适应您的示例。您必须多次调用RegisterHotKey()。HotKeyEventArgs对象告诉您命中了哪个特定键。您必须多次调用RegisterHotKey()。HotKeyEventArgs对象告诉您哪个特定的键被击中。