vb.net 检查是否按下了 DELETE 键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5589130/
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
Check if DELETE key is pressed?
提问by Voldemort
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
What is the keyValue I need for checking for the DELETE key using e.keyValue?
使用 e.keyValue 检查 DELETE 键需要什么 keyValue?
回答by Andy White
The KeyEventArgsobject contains a member called "KeyCode" that you can compare to the "Keys" enumeration.
该KeyEventArgs对象包含一个名为“会员邀请码”,你可以比较的“钥匙”枚举。
Note that certain keys may not raise the KeyDown event if they are handled by the default windowing system. I'm not sure, and I can't check it right now, but you may not get the KeyDown event for keys like Tab, Delete, Enter, etc.
请注意,如果某些键由默认窗口系统处理,则它们可能不会引发 KeyDown 事件。我不确定,我现在无法检查,但您可能无法获得 Tab、Delete、Enter 等键的 KeyDown 事件。
You can usually do something like this (this is C#, not VB, but should be similar):
你通常可以做这样的事情(这是 C#,不是 VB,但应该是类似的):
public void MyControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
// delete was pressed
}
}
回答by Hungry Mind
If you set KeyPreviewproperty of form then form will receive key events before the event is passed to the control that has focus. For example if you have textboxes and buttons on the form, normally they(the control that has focus) will capture the key press event. So make sure to set KeyPreview=true
如果您设置 表单的KeyPreview属性,则表单将在事件传递给具有焦点的控件之前接收键事件。例如,如果表单上有文本框和按钮,通常它们(具有焦点的控件)将捕获按键事件。所以一定要设置KeyPreview=true
Use this to capture the key code.
使用它来捕获关键代码。
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Delete Then
'todo
End If
End Sub
回答by mtrbean
Compare e.keyValue
with Keys.Delete
比较e.keyValue
有Keys.Delete