1、用户按下特定的按键,有三个事件可以读取按键,KeyPress、KeyDown、KeyUp。当用户按下并松开一个键时,三个事件的发生顺序:KeyDown、KeyPress、KeyUp。
keydown的e参数返回keycode(键盘代码),而keypress的e参数返回keychar(键盘符号)。
用户同时按下了ctrl键和C键的代码示例:e.Control = True And e.KeyCode = Keys.C
如果想在窗体级别处理键盘事件,比如textbox控件只接收数字,应在KeyPress事件中将KeyPressEventArgs.Handled 属性设置为 true。
示例代码: If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True ’表示键盘已经处理过该事件
End If
MSDN提供的方法:
' Boolean flag used to determine when a character other than a number is entered.
Private nonNumberEntered As Boolean = False
' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
' Initialize the flag to false.
nonNumberEntered = False
' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back Then
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = True
End If
End If
End If
'If shift key was pressed, it's not a number.
If Control.ModifierKeys = Keys.Shift Then
nonNumberEntered = true
End If
End Sub 'textBox1_KeyDown
' This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
Handles textBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If nonNumberEntered = True Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
End If
End Sub 'textBox1_KeyPress
解释:首先在KeyDown事件中检测键值是否是数字和退格键,如果不是,将变量nonNumberEntered标记为True,其次在KeyPress事件中根据变量值决定是否处理键盘事件。
2、窗体的keypreview 属性:获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。如果设置为True,keydown、keypress、keyup三个事件首先由窗体级别处理,处理完后再由具有焦点的控件处理。如果你想在程序中按F1键调入指定的帮助功能,可以先设置可以preview属性为true,然后再在窗体的keydown事件中使用以下代码:
If e.KeyCode = Keys.F1 Then
'此处加入打开帮助的代码。
MsgBox("你需要什么帮助?")
End If
检测是否同时按下了Ctrl键及鼠标左键的示例代码:
'定义变量ctrlTrue指示用户是否按下了Ctrl键
Private ctrlTrue As Boolean = False
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
'按下了Ctrl键
If e.Control = True Then
ctrlTrue = True
End If
End Sub
Private Sub TextBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left And Me.ctrlTrue Then
MsgBox("按下了鼠标左键及Ctrl键")
End If
End Sub
3、按Enter键移动到下一个控件:首先是设置好窗体的Tab键次序,再是将要处理的KeyDown添加到Handles后面。
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
'用户按下Enter键后可以移动到下一个控件上。
If e.KeyCode = Keys.Enter Then
Dim p As Control
p = CType(sender, TextBox).Parent ‘找到具有焦点控件的父容器
p.SelectNextControl(ActiveControl, True, True, True, True)
‘如果控件位于多个父容器中,控件类型也不相同,也许这样的语句比较合适:
‘Me.SelectNextControl(sender, True, True, True, True)
’P就不必定义,也不转换到textbox类型
End If
End Sub