|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Screen & System Metrics keybd_event: Navigate Form Controls using Enter |
|
Posted: | Friday January 03, 2003 |
Updated: | Monday December 26, 2011 |
Applies to: | VB4-32, VB5, VB6 |
Developed with: | VB6, Windows XP |
OS restrictions: | None |
Author: | Peter Surcouf, MSDN, VBnet - Randy Birch |
Related: |
keybd_event: Manipulating the Desktop |
Prerequisites |
None. |
|
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. |
BAS Module Code |
None for this demo, however the declares below can be placed here as Public if an application-wide solution is desired. |
|
Form Code |
Add the following code to the form containing a few text boxes, and other controls you chose. For the demo, to cut down the code, control arrays were used for the textboxes and checkboxes. The single command button (Command1) exits the demo, and its tabstop is taken out of the tab order in the load event.: |
|
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 |
Comments |
That's it. Run the app and press enter to move through the controls. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |