Sending Keystrokes from Visual Basic to an MS-DOS Application

ID Number: Q77394

1.00

WINDOWS

Summary:

The "Microsoft Visual Basic: Language Reference" version 1.0 manual

states that the SendKeys function cannot be used to send keystrokes to

a non-Windows application. One method to work around this limitation

is presented below.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

The Visual Basic SendKeys function can send keystrokes to the

currently active window (as if the keystrokes had been typed at the

keyboard). The "Microsoft Visual Basic: Language Reference" version

1.0 manual states that you are not allowed to send keystrokes to a

non-Windows application. This is correct, but you can place text on

the Clipboard and use SendKeys to paste that text into an MS-DOS

application that is running in a window (or minimized as an icon.)

To run an MS-DOS application in a window, you MUST be running in

Windows 386 enhanced mode. You must also make sure that the MS-DOS

application's PIF file has been set to display the application in a

window rather than full screen. Use the Windows PIF Editor to make

this modification, if necessary.

An example of sending keystrokes to an MS-DOS session running in a

window is given below:

1. Start a DOS session (running in a window).

2. Start Visual Basic.

3. Enter the following into the general declarations section of the

form:

Dim progname As String

4. Draw two labels on the form. Change the first label's caption to

"Dos App Title." Change the second label's caption to "Keys to

send."

5. Draw two text boxes on the form (next to each of the previously

drawn labels). Delete the default contents of these text boxes.

These controls will be used to allow the user to enter the MS-DOS

application window title and the keystrokes to send to it. Change

the CtlName property of these text boxes to "DosTitle" and

"DosKeys," respectively.

6. Draw a command button on the form and change its caption to "Send

keys."

7. Attach the following code to the command button click procedure:

progname = "Microsoft Visual Basic"

clipboard.Clear

clipboard.SetText DosKeys.Text + Chr$(13) 'Append a <CR>

AppActivate DosTitle.Text

SendKeys "% ep", 1

AppActivate progname

If the text that you send is the DIR command or another command that

takes time, the AppActivate immediately following the sendkeys will

interrupt the processing. This AppActivate should be placed in a

timer with the appropriate interval set and the timer enabled in the

command_click procedure. The timer should be disabled before

exiting the timer.

8. Run the program.

9. Enter the window title of the MS-DOS application into the DosTitle

text box. The default window title for an MS-DOS session is "DOS."

If you would like to change the window title of an MS-DOS

application, you should use the PIF Editor.

10. Enter the keystrokes to send into the DosKeys text box (for

example, "DIR").

11. Click on the Send Keys command button. The keystrokes will be sent

to the Clipboard and then pasted into the DOS window.

If this technique is used in a compiled Visual Basic program, you

should change the progname assignment from "Microsoft Visual Basic" to

the executable file name. Also, if you would like to see the text

being placed onto the Clipboard, you can open the Windows Clipboard

viewer.

Additional reference words: 1.00