• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Sending F1 to DOS Program from Windows Program

My windows application starts an old DOS application (the kind that you can tab around in and use various keys - psuedo windows looking).  Anyway, I need to tab to each field, enter values, and exit the program.  I can do all this while the DOS Command Window is minimized using:

::PostMessage(hDOS, WM_CHAR, Ch, NULL);
where hDOS is the handle to the DOS Command Window, and Ch is a key (e.g. VK_TAB).

I can not get the VK_F1 key to work though (the program needs F1 to exit the app).  Any ideas how I can do this?  Is it being eaten and not sent to the DOS program?

1 Solution
If the DOS application has the focus, then you can use keybd_event() to send the keys to the app.  This is much more reliable than using PostMessage(WM_CHAR)

Try this an let me know how it works.
You could do what nietod is suggesting, or you could try to use CreateProcess() to start your old app and TerminateProcess() to stop it...

But you should avoid using TerminateProcess() to terminate a process.  It shoudl be considered only a last resort.  The program will not be giving an opportunity to clean-up.  This is a very serious problem, because windows (especially non-NT) is not very good at cleanining up a 16-bit program.  (Not to mention that the clean-up it may do, will simply be clean-up that relates to windows itself, it won-t do any program-specific clean-up.)
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

DOS apps have some special requirements for 'special' keys (called extended).

You start it by sending a 0, then the exact key you want. for example the left-arrow would be 0, 75, while 75 would be a  alphanumeric character

I forgot the F1 key though :)

Also, I dont even know whether or not you have to do this manually or that windows handles this automatially.. but maybe you can do something with this info.

joeslowAuthor Commented:

The keybd_event seems to work pretty well.  I'm not quite sure what the Scan Code parameter does but the program has to work in other countries.  Does this look like the right thing to do?:

UINT SC = MapVirtualKey(VK_F1, 0);
::keybd_event(VK_F1, SC, 0, 0);

Also, I am using
to insure that the window has keyboard focus.  It seems to work even if the window is minimized.  Does this seem like a reliable way to do that?

Thank you,
>> I'm not quite sure what the Scan Code parameter
Usually nothing.  It indicates what key on the keyboard produced the keystroke--or at least what key you are "pretending" produced the keystroke.  This scan code changes for different types of keyboaard, (which means it often changes from language-to-language).  So the scan code is meaningless without a knowledge oft he keyboard layout, which in tern means it is pretty-much meaningless.  Virtually no software will ever use it.  (In a real keyboard event it is important to the keyboard driver, because it will convert the scan code into the keystroke it represents.  After that it is not important.)

Anyways the way you are obtaining the scan code is correct.  

make sure that every kebd_event() occurs in pairs.  That is, when you post a key down, you must later post a key up.  (You may have interveining key downs and key ups, though.)

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now