TAB键对应的ASCII码是9,判断不要写在keydown事件里面,要写在keypress事件里 Private Sub Text1_KeyPress(KeyAscii As Integer) MsgBox KeyAscii End Sub对话框弹出的返回值是 9 Private Sub Text1_KeyPress(KeyAscii As Integer) if keyascii=9 then 按下TAB键需要执行的代码 else 没有按下TAB键需要执行的代码 end if End Sub
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer GetKeyState(vbKeyTab)
Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 9 Then SendKeys " " SendKeys " " SendKeys " " SendKeys " " End If End Sub
dick8125可以把回复写的清楚一点吗?小弟没看懂具体用法
Private Declare Function GetKeyState Lib "User32" (ByVal nVirtKey As Long) As Integer Const VK_TAB = &H9 Sub text1_LostFocus() Dim iRetVal As Integer iRetVal = GetKeyState(VK_TAB) If iRetVal = 1 Then ' tab键按下 Text1.SetFocus End If End Sub
呵呵,标准解法: Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 9 Then Text1.SelText = Chr(9) KeyCode = 0 End If End Sub 不要动不动就用API,又危险又容易出错,兼容性也容易出问题。
const C_TAB=" " '注意是四个空格 Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 9 Then Text1.Text =Text1.Text+C_TAB KeyCode = 0 End If End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub对话框弹出的返回值是 9
Private Sub Text1_KeyPress(KeyAscii As Integer)
if keyascii=9 then
按下TAB键需要执行的代码
else
没有按下TAB键需要执行的代码
end if
End Sub
if keyascii=9 then
text1=text1 + chr(9)
keyascii=0 '
else
'dddddd
end if
End Sub
用hook试试吧
GetKeyState(vbKeyTab)
If KeyAscii = 9 Then
SendKeys " "
SendKeys " "
SendKeys " "
SendKeys " "
End If
End Sub
Const VK_TAB = &H9
Sub text1_LostFocus()
Dim iRetVal As Integer
iRetVal = GetKeyState(VK_TAB)
If iRetVal = 1 Then ' tab键按下
Text1.SetFocus
End If
End Sub
将失去焦点的消息拦截下来
在对其进行编程就可以了
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 Then
Text1.SelText = Chr(9)
KeyCode = 0
End If
End Sub
不要动不动就用API,又危险又容易出错,兼容性也容易出问题。
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 Then
Text1.Text =Text1.Text+C_TAB
KeyCode = 0
End If
End Sub