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

To simulate a keystroke

How can I send for instance ALT + 'a' keystroke from a program? I didn't find any API except keybd_event() which does not have a way to specify the ALT key.

Platform: WINDOWS NT 4.0 and KeyboardEventEx() API is not available.
  • 2
1 Solution
The way that I send keys is this simple function that I wrote up.  It SHOULD work on NT, but don't hold me to that!  ;-)

void SendKeys(BYTE vk, BYTE ch, HWND wind)
      keybd_event(vk, 0, 0, 0);
      keybd_event(ch, 0, 0, 0);
      keybd_event(ch, 0, KEYEVENTF_KEYUP, 0);
      keybd_event(vk, 0, KEYEVENTF_KEYUP, 0);

SendKeys(VK_ALT, 'a', hWnd);

What it does is set the window to the foreground so it can recieve keyboard input, then it sets the keyboard state of the first param to down.  Then it sets the second key.  Then it releases the second key, and then the first key.  Viola!  It should work for you.  I use it to send CTL messages to windows in Win95, it should work great for ALT messages too.  To get the hWnd of the window(if you don't already have it), use the FindWindow or FindWindowEx funtion.

>> it should work great for ALT messages too
not with VK_ALT (its not defined)!
use VK_MENU or 0x12 instead
LoL, ok!  Thanks for the tip!  Like I said, I've only used it for CTL keys.  =)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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