We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Keyhooks in DOS-Prompt??

BenLee asked
Medium Priority
Last Modified: 2013-12-03
How can I receive keystrokes from a DOS-Prompt or a DOS-program with my Visual Basic 5.0 program.
Is there an API-Call or something like this?? I used the API Message Functions to recieve the keystrokes, but it doesen't work. How can I find out, witch key was pressed in the DOS prompt or the DOS program??? Is it possible??
Watch Question

Answer comming.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

You should be able to set a hook on the DOS proccess using SetWidowsHookEx().  You would use


Thread ID should be the thread for the DOS process you are hooking.  I'm not sure how you get that thread ID, because I'm not sure how you know the DOS process you are interested.  Hopefully you already have an answer to this, if not, explain how you know which DOS prompt you want to hook.

the HookProc should be declared as

LRESULT CALLBACK HookProc(int code,WPARAM wParam,LPARAM lParam)
   if (code < 0)
      return CallNextHookEx(code,wParam,lParam);
   if  (code = HC_ACTION)
      // look at virtual code in wParam and repeat count in lParam.
   return 1;

Woops! You asked for visual basic and I gave you C.  Well, the idea should be the same, you still need to use the SetWindowsHookEx() procedure.  You just have to use it in basic Visual basic syntax.

nietod, have you actually tested your proposed solution?

There was a similar question by somebody not long ago (sorry, forgot the reference) and the combined wizdom of the experts reached the conclusion that WH_KEYBOARD hooks do not work for command-line windows under NT.

I'd be happy to be proven wrong.

No.  I guess I didn't make it clear enough with the word "should", but I've never tried this.  From what I've read about in Microsoft's documentation about input is handled, it should work.  But their documentation is often simplified or just plain wrong.

Found the reference (qid=8630028164).

tcola asked:

--- begin quote ---

Right now I have a program in Visual C++ that will execute certain commands after X minutes a system idle. The system is considered "idle" by my program when no keyboard or mouse activity is occuring.

The program works fine in pure windows, but when users sit in an MS-DOS Box for these X amount of minutes my windows program will not detect any mouse/keyboard activity (even if someone is pounding the keys).

Right now, I set a hook to the keyboard and mouse through a dll.
I want my program to detect any mouse/keyboard activity regardless of if the user is typing into the MS-DOS Box or not.

--- end quote ---

I suggested to use a VxD for Win95.

Frankly, I'm curious myself.

The hook in that case is a system wide hook (not a hook for a specific process).  Perhaps a hook for a specific DOS process will work.  Yeah, its a long shot, but its easier to test than to write a VxD.  

Why should be there be a difference?  The underlying mechanism for system-wide hooks and per-thread hooks seems to be the same.

Why?  Mostly, wishful thinking.  It is possible that for a system-wide hook, the operating system checks the process to see if it is a DOS process and, if so, does not pass it to the hook procedure assuming that windows applications aren't interested in  DOS applications.  However, given a hook for a specific process, it might guess that the hooking program obviously wants to hook the specified program regardless of whethor or not it is a DOS program so it might call the hook procedure.

Sounds logical?  Yeah, I know.  If I could reject my own answer, I would have already.

But then we would lose the forum for exchanging ideas :-)

BTW, strange that we haven't heard from BenLee.

I noticed that too.  For 400 points you would think he is desperate for an answer.  For a 50 question I check for answers every hour, but I'm cheap.


I think it should work!! Thanks nietod!!

You should try it BEFORE accepting my answer.  It might not work and you are out 400 points!  If it doesn't work I would be happy to try to help you, but you would do better with help from more experts.  

If it doesn't work, and if the experts exchange customer service people can arrange it, I would be willing to give the points back to have the question reopened.  (Not that I'm eager to loose the points, but I'm sure you want a working solution.)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.