回 帖 发 新 帖 刷新版面

主题:在VB中keypress中能否响应左右方向键?

在VB中keypress中能否响应左右方向键?

回复列表 (共6个回复)

沙发

不能。必须用KeyDown和KeyUp来处理。

板凳

在KeyPress事件中只支持非控制键。

3 楼

keypress事件主要识别ANSI 字符集中可打印的键盘字符,并且返回的是ASCII码,它不能识别功能键、编辑键、定位键以及任何这些键和键盘换档键的组合,也不能识别键盘按键状态,而这些需要KeyDown和KeyUp事件识别,KeyDown和KeyUp事件返回的是键盘虚拟码及键盘换档键的状态.左右方向键的键盘虚拟码为vbKeyLeft和vbKeyRight,参考程序:
Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
   Dim ShiftDown, AltDown, CtrlDown, Txt
   ShiftDown = (Shift And vbShiftMask) > 0
   AltDown = (Shift And vbAltMask) > 0
   CtrlDown = (Shift And vbCtrlMask) > 0
   If KeyCode = vbKeyLeft Then   ' 显示键组合.
   If ShiftDown And CtrlDown And AltDown Then
      Txt = "SHIFT+CTRL+ALT+Left."
   ElseIf ShiftDown And AltDown Then
      Txt = "SHIFT+ALT+Left."
   ElseIf ShiftDown And CtrlDown Then
      Txt = "SHIFT+CTRL+Left."
   ElseIf CtrlDown And AltDown Then
      Txt = "CTRL+ALT+Left."
   ElseIf ShiftDown Then
      Txt = "SHIFT+Left."
   ElseIf CtrlDown Then
   Txt = "CTRL+Left."
   ElseIf AltDown Then
      Txt = "ALT+Left."
   ElseIf SHIFT = 0 Then
      Txt = "Left."
   End If
   Text1.Text = "You pressed " & Txt
   End If
End Sub

4 楼

简单的说应该是
Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyDown then
.........
   if Keycode=vbKeyUp then
.............
End sub
   

5 楼

简单的说应该是
Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyLeft then
.........
   if Keycode=vbKeyRight then
.............
End sub
   

6 楼

我也觉得楼上的楼上,太复杂了!

我来回复

您尚未登录,请登录后再回复。点此登录或注册