wpf c#处理空格键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13357517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# Handle space key event
提问by TheGeekZn
To begin, this may be repeats of previous questions, but none of those answers work.
For many of the examples, I dont even get the right procedures that are listed.
首先,这可能会重复之前的问题,但这些答案都不起作用。
对于许多示例,我什至没有得到列出的正确程序。
I have a TextBoxand a ListBox. I want to enter an email in the text box, then when I push the SpaceKey, I need to add the email to the list box, then clear the text box.
我有一个TextBox和一个ListBox。我想在文本框中输入一个电子邮件,然后当我按下SpaceKey时,我需要将电子邮件添加到列表框中,然后清除文本框。
As the title suggests, I have had no luck binding the space key.
正如标题所暗示的,我没有绑定空格键。
I have tried:
我试过了:
private void txtEmailAddr_KeyDown(object sender, KeyEventArgs e)
{
KeyEventArgs ke = e;
if (ke != null && ke.Key == Key.Space)
{
ke.Handled = false;
}
if (ke.Key == Key.Space)
{
lstEmails.Items.Add(txtEmail.Text);
}
}
And
和
private void txtEmailAddr_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
lstEmails.Items.Add(txtEmail.Text);
}
}
Please could someone help.
请有人帮忙。
回答by Rohit Vats
Instead of KeyDownevent hook the PreviewKeyDownevent for your textbox -
而不是KeyDown事件挂钩PreviewKeyDown您的文本框的事件 -
<TextBox PreviewKeyDown="txtEmailAddr_KeyDown"/>
From MSDN documentation -
来自 MSDN 文档 -
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.
某些按键(例如 TAB、RETURN、ESC 和箭头键)通常会被某些控件忽略,因为它们不被视为输入按键。例如,默认情况下,Button 控件会忽略箭头键。按箭头键通常会使焦点移动到上一个或下一个控件。箭头键被视为导航键,按下这些键通常不会引发 Button 的 KeyDown 事件。但是,按 Button 的箭头键确实会引发 PreviewKeyDown 事件。通过处理 Button 的 PreviewKeyDown 事件并将 IsInputKey 属性设置为 true,您可以在按下箭头键时引发 KeyDown 事件。但是,如果您处理箭头键,焦点将不再移动到上一个或下一个控件。
回答by TheGeekZn
using
使用
public class SelectableTextBlock : TextBox
{
public SelectableTextBlock()
{
AddHandler(KeyDownEvent, new RoutedEventHandler(HandleHandledKeyDown), true);
}
public void HandleHandledKeyDown(object sender, RoutedEventArgs e)
{
KeyEventArgs ke = e as KeyEventArgs;
if(ke != null && ke.Key == Key.Space)
{
ke.Handled = false;
}
}
}
I am able to add that custom TextBox, which doesnt have the SpaceKey event handled.
我可以添加那个没有处理 SpaceKey 事件的自定义 TextBox。
回答by Marius
It does look like both methods should call the line lstEmails.Items.Add(txtEmail.Text);
看起来这两种方法都应该调用该行 lstEmails.Items.Add(txtEmail.Text);
Have you tried setting a breakpoint at that line to make sure it is getting called?
您是否尝试在该行设置断点以确保它被调用?
Are you sure you have bound the KeyDown event of the textbox to your method?
您确定您已将文本框的 KeyDown 事件绑定到您的方法吗?
Like this:
像这样:
nameOfYourTextBox.KeyDown += new EventHandler(txtEmailAddr_KeyDown);

