开发者

Determining which key is pressed

Thi开发者_如何学Gos function has many applications. I have a TextBox control. How to display keys that the user pressed. For example pressed CTRL and z in the TextBox control should appear "Ctrl + Z"? It's WPF application. Thanks.

Now i'm trying like this:

  private void txtHotKey_PreviewKeyUp(object sender, KeyEventArgs e)
    {            
        txtHotKey.Text += e.Key.ToString();
        txtHotKey.Text += "+";
        e.Handled = true;
    }

Now if I pressed Ctrl and Z in the textbox appear "Ctrl+Z+". Then press Ctrl and A. Will be "Ctrl+Z+Ctrl+A+". It's wrong.


Have you looked at the KeyEventArgs you get passed to the KeyDown event handler of the TextBox? It's got plenty of properties that identify which key has been pressed.


Take a look at the following post with example code: Capture key press events with WPF

Does it help you? You can either attach yourself to the grid (or your textbox) with the KeyDown event handler or you could use a Window_KeyDown method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜