This
demo is based on a msnews newsgroup post by Peter Surcouf showing how the
keybd_event API can be used to cause movement between controls when the
user presses Enter (instead of Tab).
keybd_event is a preferred method over using SendKeys "{TAB}" as a
workaround to a bug in some Windows versions causing the CapsLock or
NumLock lights to come on or flash when a combination of actions
prevents SendKeys from performing its action cleanly, or from
inadvertently locking the keyboard.
Microsoft advises against using SendKeys in some circumstances. KB
article Q276346 (SendKeys Function Locks Keyboard on Windows 2000) and
Q297108 (Visual Basic SendKeys Causes Incorrect Keyboard Status or
Freezes Keyboard) covers the details. |
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const VK_TAB = &H9
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Sub Form_Load()
Command1.TabStop = False
End Sub
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
KeyAscii = 0 'suppress the beep
keybd_event VK_TAB, 0, 0, 0 'send a tab
End If
End Sub
Private Sub Text1_GotFocus(Index As Integer)
With Text1(Index)
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Sub Check1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
KeyAscii = 0
keybd_event VK_TAB, 0, 0, 0
End If
End Sub
|