Results 1 to 5 of 5

Thread: Send Keys to Specified Application

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    28

    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.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    28

    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.

  3. #3
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    28

    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); */
    }
    Last edited by Chazwazza; Sep 24th, 2006 at 09:50 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    28

    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); */
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width