Send Keys to Specified Application
I was wondering how I could send keys to a specified application of my choice, there doesn't seem to be an easy to use api for this, can anyone please shed some light on the situation. Eg, I would like to be able to send "Hello World" to "Notepad", or "b13" to "Counter-Strike" etc.
Re: Send Keys to Specified Application
Also, it needs to be able to be done that is not in focus (and I don't want it to be called into focus). Surely there must be some way to do this.
Re: Send Keys to Specified Application
Check out sendmessage, however to counter-strike I doubt it will work.
Your going to need to use wm_char as a constant too i believe
Re: Send Keys to Specified Application
I cannot get the following code to work, any suggestions?
Code:
#include <windows.h>
main()
{
HWND Notepad;
Notepad = FindWindow("Notepad",NULL);
SendMessage(Notepad, WM_KEYDOWN, VkKeyScan('c'), 0);
SendMessage(Notepad, WM_CHAR, VkKeyScan('c'), 0);
SendMessage(Notepad, WM_KEYUP, VkKeyScan('c'), 0);
}
I was thinking then maybe I had to send it to the Edit portion of the window (as metioned here, but that didn't work either as seen below.
Code:
#include <windows.h>
main()
{
HWND Notepad;
Notepad = FindWindow("Notepad",NULL);
SendMessage(Notepad, WM_KEYDOWN, VkKeyScan('c'), 0);
SendMessage(Notepad, WM_CHAR, VkKeyScan('c'), 0);
SendMessage(Notepad, WM_KEYUP, VkKeyScan('c'), 0);
/* HWND Notepad;
Notepad = FindWindow("Notepad",NULL);
SetForegroundWindow(Notepad);
keybd_event(VkKeyScan('C'), 1, 0, 0);
keybd_event(VkKeyScan('C'), 1, KEYEVENTF_KEYUP, 0); */
}
Re: Send Keys to Specified Application
YAY! I got it to work, I was stupid for not realising FindWindow doesn't find child windows.
Working code below.
Code:
#include <windows.h>
main()
{
HWND Notepad;
Notepad = FindWindow("Notepad",NULL);
HWND NotepadChild;
NotepadChild = FindWindowEx(Notepad, NULL, "Edit", NULL);
SendMessage(NotepadChild, WM_KEYDOWN, VkKeyScan('c'), 0);
SendMessage(NotepadChild, WM_CHAR, VkKeyScan('c'), 0);
SendMessage(NotepadChild, WM_KEYUP, VkKeyScan('c'), 0);
/* HWND Notepad;
Notepad = FindWindow("Notepad",NULL);
SetForegroundWindow(Notepad);
keybd_event(VkKeyScan('C'), 1, 0, 0);
keybd_event(VkKeyScan('C'), 1, KEYEVENTF_KEYUP, 0); */
}