How to catch the "@" character input from keyboard
I was trying to use the event keyDown
event in a wpf textb开发者_C百科ox control and capture clicked keys with e.Key
, however since the at "@" character doesn't have a key, I can't catch it. How do I detect the "@" key clicked
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.) // nothing corresponding the at key
}
KeyDown
is for actual keys, it does not concern itself with their interpretation. Use PreviewTextInput
instead for example.
private void RichTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == "@")
{
//...
}
}
The Keydown event uses keyboard buttons. It does not really know anything about characters.
Try using the KeyPress event instead. Instead this event returns the ASCII char code of the key just pressed.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '@') MessageBox.Show("The At sign was pressed");
}
Note: Characters that are not ASCII will not trigger this event.
精彩评论