Solved

Send keys to DOS window

Posted on 1997-03-27
3
313 Views
Last Modified: 2012-06-27
I am having virtually no success in using SendKeys to control an FTP session in a DOS window.  I built a Pause routine that supposedly should stop the entry of characters for a given number of seconds until the remote server (ftp server) responds.  

Is there a 16-bit Windows API call that I could use to send keystrokes to a DOS window?
0
Comment
Question by:tccook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
dirkmartin earned 50 total points
ID: 1424787
Here's the way to send keystrokes to a DOS window.  It pertains to the Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0

This demonstrates a technique you can use to to send keystrokes to a Microsoft MS-DOS-based application from a Microsoft Visual Basic application running on Microsoft Windows 3.x or Microsoft Windows 95. This method does not work with Microsoft Windows NT.

The 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 the SendKeys function 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.

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 are 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:

Private Sub Command1_Click()

      'Ensure that progname is set to the titlebar of Visual Basic while
      ' running.
      progname = "Project1 - Microsoft Visual Basic [run]"
      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 can interrupt the processing. The AppActivate call should be
   placed in a timer with the appropriate interval set, and the timer
   should be 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 "MS-DOS Prompt".

10.Enter the keystrokes to send into the DosKeys text box (for
example, DIR).

11.Click the Send Keys button. The keystrokes are 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
 

Expert Comment

by:gdigior
ID: 1424788
While you can use a work-around method to drive your DOS program
using a variation on SENDKEYS, SENDKEYS is woefully anemic as an
intra-application communication tool.  This is because SendKeys
operates on the current window only.

Since you are using DOS-based FTP, may I suggest an alternative
way to drive it:

   In your VB program:

       1. Create an ASCII text file to use as input to the FTP
          program. In this file, you will provide the commands
          that you want the FTP program to execute. Just remember
          to put a <CR><LF> pair after each line.

          For this example, assume you call the ASCII FTP
          command file "JUNK.DAT."
         
          For example, your file may look like the following to:
                  1. open a connection
                  2. logon as the appropriate user
                  3. Switch to ASCII mode transfer
                  4. receive a file
                  5. close the FTP session

                O 130.11.10.9
                User Bunky
                ASCII ON                
                GET /unix-directory/filename
                QUIT  

       2. Create a companion DOS one-line batch file to execute FTP and
          load your input file "JUNk.TXT". An example of the
          one-line batch file follows:

               FTP < JUNK.DAT

I think that you will find this method of using FTP much cleaner
than using SENDKEYS. For other DOS programs, though, I would suggest
that you stick with the SENDKEY method mentioned by a previous poster.

Good luck,
OASYSCO
0
 

Expert Comment

by:gdigior
ID: 1424789
While you can use a work-around method to drive your DOS program
using a variation on SENDKEYS, SENDKEYS is woefully anemic as an
intra-application communication tool.  This is because SendKeys
operates on the current window only.

Since you are using DOS-based FTP, may I suggest an alternative
way to drive it:

   In your VB program:

       1. Create an ASCII text file to use as input to the FTP
          program. In this file, you will provide the commands
          that you want the FTP program to execute. Just remember
          to put a <CR><LF> pair after each line.

          For this example, assume you call the ASCII FTP
          command file "JUNK.DAT."
         
          For example, your file may look like the following to:
                  1. open a connection
                  2. logon as the appropriate user
                  3. Switch to ASCII mode transfer
                  4. receive a file
                  5. close the FTP session

                O 130.11.10.9
                User Bunky
                ASCII ON                
                GET /unix-directory/filename
                QUIT  

       2. Create a companion DOS one-line batch file to execute FTP and
          load your input file "JUNk.TXT". An example of the
          one-line batch file follows:

               FTP < JUNK.DAT

I think that you will find this method of using FTP much cleaner
than using SENDKEYS. For other DOS programs, though, I would suggest
that you stick with the SENDKEY method mentioned by a previous poster.

Good luck,
OASYSCO
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

624 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