Detecting the user pressing F10 in WPF

前端 未结 4 1667
迷失自我
迷失自我 2021-01-03 22:40

My WPF application has behaviour triggered by the functions keys (F1-F12).

My code is along these lines:

private void Window_Ke         


        
相关标签:
4条回答
  • 2021-01-03 23:09

    This worked for me, for F1

    Private Sub Window_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If (e.Key = Key.F1) Then
            ShowHelp()
        End If
    End Sub
    
    0 讨论(0)
  • 2021-01-03 23:16

    F10 launches the window menu. It's the same in all Windows apps.

    It seems that Key.System is the expected value for the F10 key.

    0 讨论(0)
  • 2021-01-03 23:19

    Answer with DataContext:

    
    
        public partial class BankView : UserControl
        {
            public BankView()
            {
                InitializeComponent();
    
                this.KeyDown += new KeyEventHandler(BankView_KeyDown);
            } 
    
            private void BankView_KeyDown(object sender, KeyEventArgs e)
            {
                try
                {
                    switch (e.Key)
                    {
                        case Key.F4:
                            ((BankViewModel)DataContext).OpenAccount();
                            break;
                    }
                }
                catch (Exception ex)
                {
                    ...
                }
            }
    

    0 讨论(0)
  • 2021-01-03 23:27

    In addition to Yacoder's response, use the following to check for the F10 key:

    case Key.System:
      if (e.SystemKey == Key.F10)
      {
        // logic...
      }
    

    The SystemKey property will tell you which System key was pressed.

    0 讨论(0)
提交回复
热议问题