Click here to Skip to main content
15,300,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i'm a beginner/novice to programming.
i like programming but it seems that programming does not run in my blood :P

I know there's are a lot of articles regarding keystrokes here but i wanted to ask for help for a certain app im working on, a simple win32 EXE file. probably written in C but i dont know how to start?

i want to send a keysroke to say, notepad. first i have two programs open, 1.notepad and 2.the app im working on say a form/API as active with a button "send", once send is clicked it will enable the notepad and send say letter "v" to notepad pause for while maybe 3 secs. then sends "SPACE" then pauses again for 3 secs. then send again "v". lets say its a loop. it will only end when i press "end" on the keyboard or "F12".

the notepad will have the values below,

v v v v v v v v v v v v .... until i end the application.

something like that.

I found an article and a sample code below below by Ali, but i was hopping it could be modified to add the pause, once i click on send keystroke using his sample, the letter will go to notepad which i was looking for, but it sends all added keys at once. i wish you could help me add the 3-5 sec delayed before sending the next keys and loop the added keys until i click done or end the keystroke app.

Sending Keystrokes to another Application in C#[^]


Thanks in advance.
Posted
Updated 27-Jan-14 20:59pm
v3

The only reliable universal way of sending keystrokes to external application is using the low-level Windows API SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

—SA
   
v3
Comments
pasztorpisti 28-Jan-14 15:50pm
   
+5, your statement holds true but most of the time simply sending keyboard/mouse messages to the target window works even if the target window isn't focused/foregrounded. Some applications switch mode as a result of WM_ACTIVATE/WM_ACTIVATEAPP/WM_FOCUS and in those cases posting messages may not work.
Sergey Alexandrovich Kryukov 28-Jan-14 16:02pm
   
Thank you. And as you know, some application's controls don't even handle those WM_s (WPF). So, only performance of low-level keystroke, starting from the moment things can be activated/focused, can help...
—SA
On Windows I have used a very primitive technique successfully with several applications in the past. I've just tested it and it still works on my Windows 7. First you have to find the handle of the window that has to receive the keystrokes. This is often not the main window but one of the child windows (for example and editbox or a button...). Then you post/send messages to this child window (preferably post). You can simulate keys/mouse by sending WM_KEYDOWN, WM_KEYUP, WM_CHAR, WM_LBUTTONDOWN, ... messages to the window. This technique often has several advantages over the SendKey() api, for example you don't have to put the focus on your target application but it isn't guaranteed to work. Most applications will probably work with it though.

Here is a piece of C/C++ code that does exactly what you wanted with notepad (at least on my windows 7):
C++
int main()
{
    HWND main_hwnd = FindWindowW(L"Notepad", NULL);
    if (!main_hwnd)
        return 1;
    HWND edit_hwnd = GetWindow(main_hwnd, GW_CHILD);
    if (!edit_hwnd)
        return 1;
    do 
    {
        wchar_t wnd_class[0x100];
        if (GetClassNameW(edit_hwnd, wnd_class, sizeof(wnd_class)/sizeof(wnd_class[0])))
        {
            if (0 == wcsicmp(L"Edit", wnd_class))
                break;
        }
        edit_hwnd = GetWindow(edit_hwnd, GW_HWNDNEXT);
    } while (edit_hwnd);

    if (!edit_hwnd)
        return 1;

    for (;;)
    {
        PostMessageW(edit_hwnd, WM_CHAR, 'v', 0);
        Sleep(3000);
        if (!IsWindow(edit_hwnd))
            break;
        PostMessageW(edit_hwnd, WM_CHAR, ' ', 0);
        Sleep(3000);
        if (!IsWindow(edit_hwnd))
            break;
    }

    return 0;
}

Note: I used the Spy++ utility to find out the window class of the main window ("Notepad") and to find out that the edit control is its direct child hwnd.
   
Comments
Sergey Alexandrovich Kryukov 28-Jan-14 16:03pm
   
Unfortunately, it all works only for special cases, when the controls really do what you expect. It won't work, on, say, a WPF editor.
—SA
pasztorpisti 28-Jan-14 16:07pm
   
That's true. I didn't think about WPF but that is indeed a case where this probably doesn't work, maybe if the target window is the main window...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900