943,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1082
  • C++ RSS
Feb 8th, 2010
0

how to send keystrokes to an arbitrary application?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheGhost is offline Offline
23 posts
since Oct 2007
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
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
Last edited by VilePlecenta; Feb 8th, 2010 at 7:24 am. Reason: extra
Reputation Points: 49
Solved Threads: 6
Junior Poster
VilePlecenta is offline Offline
106 posts
since Jan 2010
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
the thing is, i dun understand whatever's said in the MSDN website. lol.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheGhost is offline Offline
23 posts
since Oct 2007
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
Click to Expand / Collapse  Quote originally posted by TheGhost ...
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.


C++ Syntax (Toggle Plain Text)
  1. Syntax
  2.  
  3. WM_KEYDOWN // Window message sent to window procedure
  4.  
  5. WPARAM wParam; // Word Parameter( DWORD )
  6. LPARAM lParam; // Long Parameter ( long )
  7.  
  8.  
  9. Parameters
  10.  
  11. wParam // holds the key number - see into Virtual keys for the list
  12.  
  13. lParam
  14. 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.
  15.  
  16. 0-15
  17. 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.
  18. 16-23
  19. Specifies the scan code. The value depends on the OEM.
  20. 24
  21. 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.
  22. 25-28
  23. Reserved; do not use.
  24. 29
  25. Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
  26. 30
  27. 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.
  28. 31
  29. Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
Reputation Points: 49
Solved Threads: 6
Junior Poster
VilePlecenta is offline Offline
106 posts
since Jan 2010
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
from your program in the link, i wrote the following

C++ Syntax (Toggle Plain Text)
  1. HWND foregroundWindow = GetForegroundWindow();
  2. char testChar [2];
  3. testChar[0] = 'h'; testChar[1] = 'i';
  4. for(int i = 0; i < strlen(testChar); i++){
  5. SendMessage(foregroundWindow, WM_CHAR, testChar[i], NULL);
  6. }

but it doesnt work... why?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheGhost is offline Offline
23 posts
since Oct 2007
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
kk, w.e. app you're sending it to is receiving the characters, but most likely chooses not to accept them as input.
Reputation Points: 49
Solved Threads: 6
Junior Poster
VilePlecenta is offline Offline
106 posts
since Jan 2010
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
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).
Last edited by TheGhost; Feb 8th, 2010 at 8:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheGhost is offline Offline
23 posts
since Oct 2007
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
Child Windows, The hwnd you are getting is not the topmost.
its practicly a window within a window ( which I used the ChildWindowFromPoint() )
Last edited by VilePlecenta; Feb 8th, 2010 at 9:03 am.
Reputation Points: 49
Solved Threads: 6
Junior Poster
VilePlecenta is offline Offline
106 posts
since Jan 2010
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
AH! that did it.

many thanks.

EDIT:

ah, it worked for Notepad but...

it doesnt work for text fields inside the browser, CodeBlocks IDE and others... =\
Last edited by TheGhost; Feb 8th, 2010 at 9:16 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheGhost is offline Offline
23 posts
since Oct 2007
Feb 8th, 2010
0
Re: how to send keystrokes to an arbitrary application?
Click to Expand / Collapse  Quote originally posted by TheGhost ...
AH! that did it.

many thanks.

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.
Reputation Points: 49
Solved Threads: 6
Junior Poster
VilePlecenta is offline Offline
106 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: no memory to process 1Gb file? string bad allocation
Next Thread in C++ Forum Timeline: Can I increament the Space?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC