How do I send keystrokes to other applications?
about how to programmatically send Ctrl+alt+del keystrokes?
How do I invoke the. NET framework from an MFC application?
I want to write an application that can write information to a form in another application by clicking the key. Should I send wm_keydown and wm_keyup messages? Is there a better way?
Sending Wm_keydown and WM_KEYUP messages may work, but SendInput is a specially designed API function for this purpose. It uses input structure array parameters to synthesize inputs including keystrokes and mouse events, and each input structure array element corresponds to an entry event-keystroke or mouse action. The INPUT structure contains a union type whose members are Mouseinput,keybdinput (or hardwareinput, emulated bread ovens). For keystrokes, the keybdinput structure is as follows:
struct KEYBDINPUT {
WORD wVk; // virt key code
WORD wScan; // hw scan code
DWORD dwFlags; // flags—see doc
DWORD time; // time stamp, 0 = dflt
ULONG_PTR dwExtraInfo; // app-defined
};
So sending a keystroke to another application is actually creating an INPUT array with each array element corresponding to a keystroke (bouncing and pressing) and then calling the SendInput function. In order to demonstrate its actual use, I wrote a small program called typematic, you just press a hot key, you can quickly put your name, address, phone number or other information into the form. This is an ideal thing for online shoppers. When you run Typematic for the first time, the dialog box shown is shown in Figure 1:
Figure 1 typematic Initial dialog box
Press the "OK" button to enter the hidden state. You can then press <winkey>+t to reactivate the typematic, displaying the dialog box shown in Figure 2:
Figure 2 is reactivated typematic.
You can see that the dialog box shows a list of abbreviated information. Type "n" to represent the name, "a" represents the address, and Typematic sends the corresponding string to the current form or application. These abbreviations are defined in a static table, and you can change them to your own information:
struct ABBREV {
TCHAR key;
LPCTSTR text;
} MYABBREVS[] = {
{ _T(''n''),_T("Elmer Fudd") },
{ _T(''a''),_T("1 Bunny Way") },
...
{ 0,NULL}
};