主题:在VB中keypress中能否响应左右方向键?
heavy
[专家分:0] 发布于 2003-09-07 15:34:00
在VB中keypress中能否响应左右方向键?
回复列表 (共6个回复)
沙发
llm9178 [专家分:13400] 发布于 2003-09-07 15:51:00
不能。必须用KeyDown和KeyUp来处理。
板凳
Eagle Twenty [专家分:10] 发布于 2003-09-07 19:14:00
在KeyPress事件中只支持非控制键。
3 楼
eerfaone [专家分:490] 发布于 2003-09-07 19:34:00
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 楼
kaixinsnow [专家分:30] 发布于 2004-02-19 22:09:00
简单的说应该是
Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown then
.........
if Keycode=vbKeyUp then
.............
End sub
5 楼
kaixinsnow [专家分:30] 发布于 2004-02-19 22:10:00
简单的说应该是
Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft then
.........
if Keycode=vbKeyRight then
.............
End sub
6 楼
lyamlf123 [专家分:940] 发布于 2004-05-16 15:08:00
我也觉得楼上的楼上,太复杂了!
我来回复