C# 按退出键调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1798383/
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
Press Escape key to call method
提问by raimis
Is there a way to start a method in C# if a key is pressed? For example, Esc?
如果按下一个键,有没有办法在 C# 中启动一个方法?例如,Esc?
采纳答案by ChrisW
I am writing WinForms application. User fills the textbox and if he wants to delete everything, he just clicks esckey on keyboard
我正在编写 WinForms 应用程序。用户填充文本框,如果他想删除所有内容,他只需点击esc键盘上的键
I think you need to handle the KeyDown event.
我认为您需要处理KeyDown 事件。
回答by Martin Peck
Are you writing a Console application, a WinForms application or something else? Are you trying to capture the ESCkey at all times (regardless of the focused window/application) or something else?
您是在编写控制台应用程序、WinForms 应用程序还是其他应用程序?您是否一直试图捕获ESC密钥(无论聚焦的窗口/应用程序如何)或其他什么?
More context required.
需要更多上下文。
If you're writing a Console app, then you should start looking at things like Console.ReadKey...
如果您正在编写控制台应用程序,那么您应该开始查看诸如 Console.ReadKey...
http://msdn.microsoft.com/en-us/library/system.console.readkey.aspx
http://msdn.microsoft.com/en-us/library/system.console.readkey.aspx
回答by Gregtheitroade
use the OnKeyPress Event of your textbox and in the event
使用文本框和事件中的 OnKeyPress 事件
if(e.KeyCode==Keys.Escape)
{
yourTextBox.Text = string.Empty;
}
回答by Dr. Wily's Apprentice
As others have mentioned, handle the KeyDown
or KeyUp
event of the appropriate control. The KeyPress
event would work for the Escapekey as well, though it will not trigger for some keys, such as Shift, Ctrlor ALt.
正如其他人所提到的,处理适当控件的KeyDown
或KeyUp
事件。该KeyPress
事件也适用于该Escape键,但它不会触发某些键,例如Shift、Ctrl或ALt。
If you want to execute this function anytime the user presses the Escapekey, then you probably want to handle the event on the Form. If you do this, you will probably also want to set the Form's KeyPreview
property to true. This will allow the Form control to receive the event even if the focus is currently inside of one of the child controls.
如果您想在用户按下Escape键的任何时候执行此功能,那么您可能需要处理 Form 上的事件。如果这样做,您可能还想将 Form 的KeyPreview
属性设置为 true。这将允许 Form 控件接收事件,即使焦点当前位于其中一个子控件内。
If you want the behavior to be specific to a control, such as clearing the text within a textbox that currently has focus, then you should handle the KeyDown
or KeyUp
event of the TextBox
control. This way, your event handler will not be triggered if the user presses the escape key outside of the textbox.
如果您希望该行为特定于某个控件,例如清除当前具有焦点的文本框中的文本,那么您应该处理该控件的KeyDown
orKeyUp
事件TextBox
。这样,如果用户在文本框外按下转义键,则不会触发您的事件处理程序。
In some situations you might want to prevent child controls from handling the same event that you've just handled. You can use the SuppressKeyPress
property on the KeyEventArgs
class to control this behavior:
在某些情况下,您可能希望阻止子控件处理您刚刚处理的同一事件。您可以使用类SuppressKeyPress
上的KeyEventArgs
属性来控制此行为:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape key pressed");
// prevent child controls from handling this event as well
e.SuppressKeyPress = true;
}
}
回答by Ognyan Dimitrov
You have to switch the form property "KeyPreview" to trueor your events will not be fired. Handling these events alone will not do anything even though the events are correct. It will look to you like nothing really happens even though you have subscribed the proper event handlers.
您必须将表单属性“KeyPreview”切换为 true,否则您的事件将不会被触发。即使这些事件是正确的,单独处理这些事件也无济于事。即使您订阅了正确的事件处理程序,它也会看起来像什么都没有发生。
回答by JxDarkAngel
With Event KeyPress...
使用事件按键...
//Escape
if (e.KeyChar == '')
{
this.DialogResult = DialogResult.Cancel;
e.Handled = true;
}
回答by Spratly
You can use KeyUp event too. I prefer it though.
您也可以使用 KeyUp 事件。我更喜欢它。
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
if (e.Key == Key.Escape) {
//WHAT WILL HAPPEN INSERT HERE
}
}
回答by Hessy SharpSabre
First in Properties do > KeyPreview : True
首先在 Properties do > KeyPreview : True
Then :
然后 :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
//call your method here
}
}
回答by Jon
In case someone is looking for how to do this in a console application
如果有人正在寻找如何在控制台应用程序中执行此操作
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}