keystrokes

This is a discussion on keystrokes within the Windows Programming forums, part of the Platform Specific Boards category; hey i wantto send keystrokes to a different window, found SendMessage() function in another thread and I made this: Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91

    keystrokes

    hey i wantto send keystrokes to a different window, found SendMessage() function in another thread and I made this:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
    	HWND notepad = FindWindow(NULL, "Untitled - Notepad");
    
    	SendMessage(notepad, "this is a test", NULL, NULL);
    
    	return 0;
    }
    for some reason it is not sending that keystroke to that hwnd

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This really should be on the Windows Programming board.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    allright sorry, can any mods move it to the win32 section

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Sure. .
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > for some reason it is not sending that keystroke to that hwnd
    Perhaps because it sends single keys, and not strings.
    Use a loop to send each char (as an appropriate keystroke)

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    allright, i did what u said and still it is not working
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
    	char chars[] = "abc";
    	HWND notepad = FindWindow(NULL, "Untitled - Notepad");
    
    	for(int a = 0; a<3; a++)
    	{
    		SendMessage(notepad, WM_CHAR, chars[a], NULL);
    	}
    
    	return 0;
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Try
    1. Reading the manual, rather than just randomly passing values which seem to keep the compiler happy.
    For example, the last parameter would seem to need other things than NULL.
    It also might be an assumption on your part that key codes are the same thing as ASCII characters.

    2. Check for errors and status returns on ALL your calls. How do you know if you even found a window?

  8. #8
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    notepad is not NULL

    iv got so far...
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
    	char chars[] = "abc";
    	HWND notepad = FindWindow(NULL, "Untitled - Notepad");
    	
    	if(notepad == NULL)
    	{
    		printf("error! HWND notepad = NULL\n");
    	}
    
    	for(int a = 0; a<4; a++)
    	{
    		SendMessage(notepad, WM_CHAR, chars[a], NULL);
    		PostMessage(notepad, WM_KEYDOWN, VK_RETURN, 0);
    		PostMessage(notepad, WM_KEYDOWN, VK_SPACE, 0);
    	}
    
    	return 0;
    }
    Last edited by apsync; 12-25-2004 at 03:00 PM.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    anyone ? :P

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You cannot send WM_CHAR messages to the main notepad window, you'll have to send the WM_CHAR messages to the Edit Control that is a child window to the notepad window.

    I just tried this and it works.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    any code for example ?

  12. #12
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    This isn't geared toward your problem...but in the FindWindow(...) function, maybe take out the "Untitled - " part (ala: FindWindow(NULL, "Notepad"))

  13. #13
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Be glad to post some code. All right so we know that the space where you type in Notepad is an edit class, therefore a child window of Notepad. Thus all we have to do is:

    1. query for the Notepad window
    2. query for Notepad's child window
    3. sendmessages to notepad's child window

    Pretty easy, right? Heres the code:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
    	char chars[] = "abc";
    	HWND hwndNotepad, hwndChild; 
    	
                    //query for notepad
    	hwndNotepad = FindWindow(NULL, "Untitled - Notepad");
    	
    	if(hwndNotepad == NULL)
    	{
    		printf("error! HWND notepad = NULL\n");
    		return(0);
    	}
    
    	//query for the child window(edit control)
                    hwndChild = GetWindow(hwndNotepad, GW_CHILD);
    
                    //send our message
    	if(!(hwndChild == NULL)) {
    		for(int a = 0; a<4; a++) {
    
    			SendMessage(hwndChild, WM_CHAR, (WPARAM)chars[a], 1);
    		}
    	}
    
    	return 0;
    }
    Happy coding!

  14. #14
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I just wrote a quick, dirty little app for you that is a more general purpose method, and probably a little bit more than you're seeking, but you might find it quite useful. Instead of just searching for a single window by it's title (which is an extremely limiting process), this enumerates all windows. Then, given an HWND, you can use GetClassName() or GetWindowText(), et cetra to see if it's an application window you want to deal with. If so, it's children are enumerated until your target is acquired. Then it's just a matter of what you want to do to it. Enjoy.
    Code:
    #include <windows.h>
    #include <tchar.h>
    
    // use some default text if none is supplied on the command line
    static LPCTSTR DEFAULT_MSG = _T("This text was automatically inserted.");
    
    // a container to pass to arguments - the string and it's length
    struct StringMsg {
      int nLength;
      LPCTSTR pszText;
    };
    
    // iteratively process each child window of the pass HWND
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
      // first we determine the classname 
      TCHAR szClassName[32];
      GetClassName(hwnd, szClassName, 32);
    
      // if it is not an edit window, we'll just continue with
      // the next child window
      if (_tcsncmpi(szClassName, _T("EDIT"), 4) != 0)
        return TRUE;
    
      // otherwise we auto-type something into the edit window and
      // cease the enumeration  
      StringMsg *pMsg = (StringMsg*)lParam;
      for (int i = 0; i < pMsg->nLength; ++i)
        SendMessage(hwnd, WM_CHAR, pMsg->pszText[i], 0);
    
      return FALSE;
    }
    
    // this is called for every top-most HWND
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
      TCHAR szClassName[32];
      GetClassName(hwnd, szClassName, 32);
    
      // determine if it is our target application window
      if (_tcsncmpi(szClassName, _T("NOTEPAD"), 7) == 0)
        // if it is, we'll enumerate it's children and let that
        // enumeration handle finding the target control and doing
        // our dirty work, then stop looking
        if (!EnumChildWindows(hwnd, EnumChildProc, lParam))
          return FALSE;
    
      // otherwise we just keep looking
      return TRUE;
    }
    
    // program entry point
    // pass a string on the command line or let the default text
    // be used 
    int _tmain(int argc, TCHAR **argv) {
      StringMsg sm;
    
      // argument? use it  
      if (argc > 1) {
        sm.nLength = _tcslen(argv[1]);
        sm.pszText = argv[1];
      }
      // none? use ours
      else {
        sm.nLength = _tcslen(DEFAULT_MSG);
        sm.pszText = DEFAULT_MSG;
      }  
    
      // enumerate all the top-level windows
      EnumWindows(EnumWindowsProc, (LPARAM)&sm);  
    
      return 0;
    }

  15. #15
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I am the luckiest man ever to live. Not only have I found my one true love, but from the time my eyes open until the time they close, every single day for the rest of my life for every life for the rest of time, she is by my side for me to touch, smell, cherish, and love. Every morning is the first morning of the rest of my life, and with every beat of my heart and each breath in my lungs I know that somehow I am lucky enough to be living a dream I wish never to wake from.
    If she's that hot, you gotta post some pics, dude.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fake keystrokes to another process
    By Nota in forum Windows Programming
    Replies: 20
    Last Post: 01-26-2009, 10:56 PM
  2. Sending keystrokes to another view
    By azeemanwer in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2007, 10:41 AM
  3. Send keystrokes to a running program via batch file
    By ganjamon in forum Windows Programming
    Replies: 2
    Last Post: 08-16-2005, 08:08 AM
  4. Tracking number of keystrokes
    By thecow in forum Windows Programming
    Replies: 4
    Last Post: 06-04-2003, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21