How to Emulate VB for Windows SendKeys Stmt in VB for MS-DOS
ID: Q94792
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
SUMMARY
It may sometimes be useful in a Text Box to have the ENTER key behave as if
it were the TAB key, shifting focus to the next control in the Tab order.
However, using the ENTER key to move the focus does not follow recommended
Windows-based application design guidelines. The ENTER key should be used
to process the default command or to process entered information.
However, given this information, if you still want to make the ENTER key
behave as if it were the TAB key, you can. In Microsoft Visual Basic for
Windows, you can use the SendKeys statement to put keystrokes in the
keyboard buffer as if you had typed them on the keyboard. You can easily
emulate pressing a TAB key this way. There is no equivalent statement built
in to Microsoft Visual Basic for MS-DOS.
To do the equivalent of the SendKeys statement in Microsoft Visual Basic
for MS-DOS, you can either use the Poke statement to place keystrokes
directly into the keyboard buffer or use Call Interrupt. However, to do
this
requires an 80286 computer or better.
MORE INFORMATION
The following steps show you how to use the Call Interrupt statement to put
a TAB key press into the keyboard buffer.
- Start VBDOS.EXE with the /L switch to load the default Quick library.
- From the File menu, choose New Form... (FORM1.FRM).
- Place two text boxes on the Form1 using the default names Text1 and
Text2.
- Exit from the Form Designer, and save all changes.
- In the module level of Form1, add the following $INCLUDE metacommand:
Rem $INCLUDE: 'VBDOS.BI'
- In the Code Window of Form1 add the following code:
Sub SendTab ()
Dim regs AS RegType
regs.ax = &H500 ' Subroutine to push character.
regs.cx = 15 * 256 ' 15 is scan code for a TAB character.
regs.cx = regs.cx + 9 ' 9 is ASCII code for TAB character.
Call Interrupt(&H16, regs, regs) ' Keyboard interrupt.
End Sub
- Add the following code to the Text1_KeyPress event procedure of Form1:
Sub Text1_KeyPress (KeyAscii AS INTEGER)
If KeyAscii = 13 Then ' 13 is the ASCII code for the Enter key
Call SendTab
KeyAscii = 0 ' Prevent beep
End If
End Sub
- Add the following code to the Text2_KeyPress event procedure of Form1:
Sub Text2_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then '13 is the ASCII code for the Enter key
Call SendTab
KeyAscii = 0 ' Prevent beep
End If
End Sub
- Press the F5 key to run the program. Then press the ENTER key to see
the focus shift from Text1 to Text2.
Adding Keyboard and Mouse Access
In cases where you want to implement this technique for use by both
keyboard and mouse, add the following code to your application to
actually commit or flush the keyboard buffer. Place two lines of code
at the bottom of the SendTab Sub procedure (see step 7 above) to make
the SendTab Sub procedure look like this:
Sub SendTab ()
Dim regs AS RegType
regs.ax = &H500 ' Subroutine to push character.
regs.cx = 15 * 256 ' 15 is scan code for a TAB character.
regs.cx = regs.cx + 9 ' 9 is ASCII code for TAB character.
Call Interrupt(&H16, regs, regs) ' Keyboard interrupt.
Dim regs2 AS RegType ' create clean variable
Call Interrupt(&H9, regs2, regs2) ' Keyboard interrupt.
End Sub
This causes the tab to occur even if the code is invoked by a mouse click.
For more information, query on the following words in the Microsoft
Knowledge Base:
(interrupt or poke) and keyboard and buffer
Additional query words:
VBmsdos 1.00
Keywords :
Version : MS-DOS:1.0
Platform : MS-DOS
Issue type :