Microsoft KB Archive/77394

= VB3 How to Send Keystrokes from VB App to MS-DOS App =

Article ID: 77394

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q77394



SUMMARY
The &quot;Microsoft Visual Basic: Language Reference&quot; version 1.0 manual states that the SendKeys function cannot be used to send keystrokes to an MS-DOS-based application from a Windows-based application. This article gives a technique you can use to to send keystrokes to an MS-DOS-based application from a Visual Basic Windows-based application.



MORE INFORMATION
The Microsoft Visual Basic for Windows SendKeys function can send keystrokes to the currently active window as if the keystrokes had been typed at the keyboard. Although it is not possible to send keystrokes to an application that is not based on Windows by using SendKeys directly, you can place text on the Clipboard and use SendKeys to paste that text into an MS-DOS-based application that is running in a window or minimized as an icon.

To run an MS-DOS-based application in a window, you must be running in Windows 386 enhanced mode. You must also make sure that the MS-DOS-based 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.

Step-by-Step Example
The following shows by example how to send keystrokes to an MS-DOS session running in a window:

 Start an MS-DOS session running in a window. Start Visual Basic for Windows and start a new project.  Enter the following into the general declarations section of the form: Dim progname As String

 Put two labels on the form. Change the first label's caption to &quot;MS-DOS App Title.&quot; Change the second label's caption to &quot;Keys to send.&quot; Put two text boxes on the form next to each of the labels. Delete the default contents of these text boxes. These controls will be used to allow the user to enter the MS-DOS-based application's window title and the keystrokes to send to it. Change the Name property of these text boxes to &quot;DosTitle&quot; and &quot;DosKeys&quot; respectively. Put a command button on the form, and change its caption to &quot;Send keys.&quot;  Add the following code to the Command1 button click event procedure: Sub Command1_Click progname = &quot;Microsoft Visual Basic&quot; clipboard.Clear clipboard.SetText DosKeys.Text + Chr$(13) ' Append a <CR>. AppActivate DosTitle.Text SendKeys &quot;% ep&quot;, 1 AppActivate progname End Sub

</li></ol>

If the text that you send is the DIR command or another command that takes time, the AppActivate call immediately following the SendKeys call will interrupt the processing. The AppActivate call 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.


 * 1) Run the program.
 * 2) Enter the window title of the MS-DOS-based application into the DosTitle text box. The default window title for an MS-DOS session is &quot;DOS.&quot; If you would like to change the default window title, use the PIF Editor.
 * 3) Enter the keystrokes to send into the DosKeys text box (for example, DIR).
 * 4) Click the Send Keys button. The keystrokes will be sent to the Clipboard and then pasted into the MS-DOS window.

To use this technique in a compiled Visual Basic for Windows program, change the progname assignment from &quot;Microsoft Visual Basic&quot; to the executable file name. Also, to see the text being placed onto the Clipboard, open the Windows Clipboard viewer.

Keywords: kbenv KB77394

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.