开发者

How can i know if the user press on the buttons Ctrl+K?

开发者 https://www.devze.com 2023-03-16 23:05 出处:网络
I define this in the page1 in the \'OnNavigatedTo\' method page1.keyEventHandler += (s, k) => { if( k.Key == Key.P )

I define this in the page1 in the 'OnNavigatedTo' method

page1.keyEventHandler += (s, k) => 
{
     if( k.Key == Key.P )
     {
            // Do Something ... 
     }
}

But i need to know if the user pressed on two keys together ... i need to know if the user pressed on the Key.Ctrl+Key.P

How can i do it ?

Thanks 开发者_如何学Python


Here you go:

page1.keyEventHandler += (s, k) => {

  if (Keyboard.Modifiers == ModifierKeys.Control && k.Key == Key.P) {
    ...
  }
}


Unfortunately there isn't nice way of doing this. You'll need to fire 2 events. One for each key, like so:

private bool KeyADown = false;
private bool KeyBDown = false;

private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A)
    {
        KeyADown = true;
    }
    else if (e.Key == Key.B)
    {
        KeyBDown = true;
    }

    // check for Ctrl+A+B
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        if (KeyADown && KeyBDown)
        {
            MessageBox.Show("Ctrl+A+B detected");
        }
    }
}

private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A)
    {
        KeyADown = false;
    }
    else if (e.Key == Key.B)
    {
        KeyBDown = false;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消