Solved

How to "SendKeys" to a Dos App?

Posted on 1997-10-04
4
445 Views
Last Modified: 2006-11-17
I'm running a Dos app in Win95.  I'd like
to be able to send some commands to it from
VB5.  How can I do this?

AppActivate & Sendkeys doesn't work...

Thanks,

Karsten Wilson
wilsonk@e-z.net
0
Comment
Question by:wilsonk
  • 2
4 Comments
 

Accepted Solution

by:
EyalB earned 80 total points
Comment Utility
From the MSDN:

SUMMARY

The "Microsoft Visual Basic: Language Reference" 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:
1.Start an MS-DOS session running in a window. 2.Start Visual Basic for Windows and start a new project. 3.Enter the following into the general declarations section of the form:



      Dim progname As String



4.Put two labels on the form. Change the first label's caption to "MS-DOS App Title." Change the second label's caption to "Keys to send." 5.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 "DosTitle" and "DosKeys" respectively. 6.Put a command button on the form, and change its caption to "Send keys." 7.Add the following code to the Command1 button click event procedure:

Sub Command1_Click()

      progname = "Microsoft Visual Basic"
      clipboard.Clear
      clipboard.SetText DosKeys.Text + Chr$(13)  ' Append a <CR>.
      AppActivate DosTitle.Text
      SendKeys "% ep", 1
      AppActivate progname



End Sub

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. 8.Run the program. 9.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 "DOS." If you would like to change the default window title, use the PIF Editor. 10.Enter the keystrokes to send into the DosKeys text box (for

    example, DIR).



11.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 "Microsoft Visual Basic" to the executable file name. Also, to see the text being placed onto the Clipboard, open the Windows Clipboard viewer.
0
 
LVL 3

Expert Comment

by:schworak
Comment Utility
It seems to me that this doesn't answer the original question. How to send key strokes to DOS not to a WINDOW.

What if the program you want to send keys to CAN'T be run in a window? Then what do you do?
0
 

Expert Comment

by:EyalB
Comment Utility
Well, it did since he accepted the answer. Anyway, there is no way to send keys to a dos app when its not windowed. The example I gave was taken from MSDN and used the clippboard for pressing the keys in dos. Clipboard functions are only available to windowed dos programs.

Eyal.
0
 

Expert Comment

by:jtwelsh
Comment Utility
I have executed the code perfectly!  It was a great help.  Thank you Eyal for the suggestion.  It has saved me hours of time!

Joe
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now