hi, i need to send keystrokes an application (including, "enter", "left arrow", "right arrow", etc).

but how do i do that? i do not see any short and useful examples to doing it.

all i have got at the current moment is to get the foreground window using

HWND foregroundWindow = GetForegroundWindow();

i see the PostMessage function takes in HWND, UINT, WPARAM and LPARAM... but i have no idea which is for what. can somebody pls explain and teach me how to send keystrokes to an arbitrary application?

thanks.

on msdn you can find information regarding SendMessage(), or keybd_event()

you can use WM_KEYDOWN or WM_CHAR

Edit: I also posted a simple console based app that sends words to a minimized notepad
http://www.daniweb.com/forums/thread256517.html

the thing is, i dun understand whatever's said in the MSDN website. lol.

the thing is, i dun understand whatever's said in the MSDN website. lol.

If you do not understand how a window procedure works, then i would recommend start studying there since it is too extensive to explain here.

I will however give you a little insight.

Syntax

    WM_KEYDOWN // Window message sent to window procedure

        WPARAM wParam; // Word Parameter( DWORD )
        LPARAM lParam; // Long Parameter ( long )
        

Parameters

    wParam // holds the key number - see into Virtual keys for the list

    lParam
        Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.

        0-15
            Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
        16-23
            Specifies the scan code. The value depends on the OEM.
        24
            Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
        25-28
            Reserved; do not use.
        29
            Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
        30
            Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
        31
            Specifies the transition state. The value is always zero for a WM_KEYDOWN message.

from your program in the link, i wrote the following

HWND foregroundWindow = GetForegroundWindow();
	char testChar [2];
	testChar[0] = 'h'; testChar[1] = 'i';
	for(int i = 0; i < strlen(testChar); i++){
		SendMessage(foregroundWindow, WM_CHAR, testChar[i], NULL);
	}

but it doesnt work... why?

kk, w.e. app you're sending it to is receiving the characters, but most likely chooses not to accept them as input.

kk, w.e. app you're sending it to is receiving the characters, but most likely chooses not to accept them as input.

hmm... this is odd. because i had Notepad, and then CodeBlocks at the foreground when calling the function...

i had this send message function called when i press "Enter" (using the GetAsyncKeyState() function)

the program told me the function to send message ran (via cout).

Child Windows, The hwnd you are getting is not the topmost.
its practicly a window within a window ( which I used the ChildWindowFromPoint() )

AH! that did it.

many thanks. :D

EDIT:

ah, it worked for Notepad but...

it doesnt work for text fields inside the browser, CodeBlocks IDE and others... =\

AH! that did it.

many thanks. :D

EDIT:

ah, it worked for Notepad but...

it doesnt work for text fields inside the browser, CodeBlocks IDE and others... =\

Have you tried changing the point?
Also you could enumerate all the child windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.