简体   繁体   English

按下ALT键时,Keys.Numpad0作为Keys.System进入

[英]Keys.Numpad0 comes in as Keys.System when ALT is pressed

I am trying to detect hotkeys of ALT+1 through ALT+9 but when ALT is pressed the key comes in as Key.System. 我正在尝试检测ALT + 1到ALT + 9的热键,但是当按ALT时,该键作为Key.System出现。 When CTRL+NumPad0 is pressed it key is Key.NumPad0 which is correct. 按下CTRL + NumPad0时,其键为Key.NumPad0,这是正确的。

private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    bool isAlt = Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt);
    bool isCtrl = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);

    string ctrlMod = string.Empty;

    if (isAlt)
    {
        ctrlMod = "alt + " + e.Key.ToString();
    }

    if (isCtrl)
    {
        ctrlMod = "ctrl + " + e.Key.ToString();
    }

    Debug.WriteLine("Key is " + ctrlMod);
}

ALT+NumPad0 through ALT+NumPad9 produces: ALT + NumPad0到ALT + NumPad9产生:

Key is alt + System 关键是alt +系统

Ctrl works properly Ctrl正常工作

Key is ctrl + NumPad1 键是ctrl + NumPad1
Key is ctrl + NumPad2 键是ctrl + NumPad2
Key is ctrl + NumPad3 按键是Ctrl + NumPad3

You could use the Keyboard.Modifiers and KeyEventArgs.SystemKey properties to detect ALT+1 through ALT+9 : 您可以使用Keyboard.ModifiersKeyEventArgs.SystemKey属性来检测ALT+1ALT+9

private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == ModifierKeys.Alt)
    {
        string ctrlMod = "alt + " + e.SystemKey.ToString();
        Debug.WriteLine("Key is " + ctrlMod);
    }
}   

private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    bool isAlt = Keyboard.Modifiers == ModifierKeys.Alt;
    bool isCtrl = Keyboard.Modifiers == ModifierKeys.Control;

    string ctrlMod = string.Empty;

    if (isAlt)
    {
        ctrlMod = "alt + " + e.SystemKey.ToString();
    }

    if (isCtrl)
    {
        ctrlMod = "ctrl + " + e.Key.ToString();
    }

    Debug.WriteLine("Key is " + ctrlMod);
}

I think maccettura is right and its to do with 'Windows Alt Codes'. 我认为maccettura是正确的,并且与“ Windows Alt Codes”有关。 I can get around it using 我可以使用解决它

if (Keyboard.IsKeyDown(Key.NumPad0)) { } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM