5 Replies - 1146 Views - Last Post: 08 February 2010 - 10:58 AM Rate Topic: -----

#1 theChameleon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 07-July 09

how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 04:10 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: how to send keystrokes to an arbitrary application?

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 04:56 AM

The API call you need is SendMessage. The link is here.

Hope that helps.

This post has been edited by Martyn.Rae: 08 February 2010 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 theChameleon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 07-July 09

Re: how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 05:17 AM

View PostMartyn.Rae, on 08 February 2010 - 03:56 AM, said:

The API call you need is SendMessage. The link is here.

Hope that helps.



i wrote the following:

void tryPost(){
	cout<< "tryPost()" << "\n";
	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 do anything... what am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#4 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 06:11 AM

Can it be that, when you run your code, your own application is the Foregroundwindow, since it has the focus?
So the messages are sent to your own application, instead of the other window.
If you know the name (title) of the window you want to send messages to, you could try to use a FindWindow(); call to get a handle on the window you want.
documentation: http://msdn.microsof...499(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

#5 theChameleon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 07-July 09

Re: how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 06:22 AM

View PostFerencn, on 08 February 2010 - 05:11 AM, said:

Can it be that, when you run your code, your own application is the Foregroundwindow, since it has the focus?
So the messages are sent to your own application, instead of the other window.
If you know the name (title) of the window you want to send messages to, you could try to use a FindWindow(); call to get a handle on the window you want.
documentation: http://msdn.microsof...499(VS.85).aspx


no, the app i was interested in was in the foreground.

UPDATE

the code is now

void tryPost(){
	cout<< "tryPost()" << "\n";
	HWND foregroundWindow = GetForegroundWindow();
	POINT pt;
	pt.x = 1;
	pt.y = 1;
	char testChar [2];
	testChar[0] = 'h';
	testChar[1] = 'i';
	for (int i = 0; i < strlen(testChar); i++){
		SendMessage(ChildWindowFromPoint( foregroundWindow, pt ), WM_CHAR, testChar[i], NULL);
	}
}



but it works for only Notepad.exe. what im trying to do is to make it enter stuff into whichever application im interested in. this can include textfields in browser, flash applications, IDE, etc.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_taylorc8*


Reputation:

Re: how to send keystrokes to an arbitrary application?

Posted 08 February 2010 - 10:58 AM

The function you need is SendInput I have done this before, and I have working code that does it.

Why don't I just make a library of all my stuff and put it on this website sometime?

SendInput();
the third parameter is the size of an INPUT structure.
look it up on MSDN
Was This Post Helpful? 0

Page 1 of 1