Jump to content
  • Advertisement
Sign in to follow this  
Ripiz

[VC++] How to hook and send input?

This topic is 3155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. How to hook application, and then send input to it? It has to work always, no matter window is active or not, focused or not, it should work even if it's minimized. However I wasn't able to find on Google anything useful. Maybe anyone have idea how to do that? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
You basically can't, at least not in the general case.

* You can use SendInput, but that pushes events into Window's global input queue, so the application that ends up getting them is the one which is activated at the time.

* You can use PostMessage to post the appropriate input messages (WM_MOUSEMOVE, WM_KEYUP, WM_CHAR, WM_KEYDOWN) to the application, however this isn't nice - if the application then calls Windows API functions to get the input state (like GetCursorPos or GetKeyState), it's going to see the real input state, not the one you want it to see. See here. EDIT: I suppose you could then use standard IAT patching technique to intercept these functions, but you'd have to know exactly what functions were used, and keep track of the fake input state yourself to be able to return it.

* You can use AttachThreadInput to merge your input queue with that of the applications, and then use SendInput or whatever to send input when you are the active window - then it also goes to the application as well. However you need to be the active window at the time, and there are complications with using this function which means that it's not really a viable alternative.

* If the application is using DirectInput, there are methods to send input programatically.

See Also

[Edited by - mattd on October 27, 2009 11:38:25 PM]

Share this post


Link to post
Share on other sites

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow){
HWND window = FindWindow(NULL,"Untitled - Notepad"); //it succeeds to find window
BOOL res;
while(true){
res = PostMessage(window,0x31,0,0); //returns 0
res = PostMessage(window,WM_CHAR,'1',0); //returns 1
}
}


However it doesn't write anything in my Notepad, no matter it's activated or not. What I've done wrong?

Share this post


Link to post
Share on other sites
Quote:
Original post by Ripiz
*** Source Snippet Removed ***
However it doesn't write anything in my Notepad, no matter it's activated or not. What I've done wrong?


You need to send the message to the right window - in this case it's the edit control child window of Notepad.

Try doing
window = FindWindowEx(window, NULL, "EDIT", NULL);
before you send them.

See this document.

Share this post


Link to post
Share on other sites
Thank you!!! Works perfectly. But because of infinite fast loop now I have a lot of sound stuck and repeating XD XD XD XD XD


Nevermind, Notepad was just target for testing, it doesn't work with the program I need =/ I think I needs DirectInput, Spy++ doesn't show anything when I use keyboard.

[Edited by - Ripiz on October 28, 2009 3:11:49 AM]

Share this post


Link to post
Share on other sites
Are you sure you've got the right window to send messages to? You could use GetGUIThreadInfo and see what the application's hwndFocus is.

If you can say, what is the application?

Share this post


Link to post
Share on other sites
It does find window, but I wasn't able to specify control child.
It's BYOND community (www.byond.com), wanted to make program which would send keys into it, while it's in the background either minimized. Most games a lot of control childs, and I need to focus on map child, I wasn't able to focus to it, also I suspect it uses DirectInput, but all control are set up using HWND.

In case you, or anyone else decide to download it in order to help me, downloading and installing isn't enough, you will have to connect to random game in order to test it. Just make sure it's not text based =) However I haven't seen those for a while now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!