CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2009
    Posts
    8

    can't send WM_KEYDOWN correctly?

    Hi,everyone!

    I'm tring to write a program to send key press message to other program(such as notepad).I mean if I press "M" in my program,this "M" will append to the notepad text entry.

    here is my code:
    hwnd = FindWindow(NULL,"Untitled - Notepad");
    if(hwnd!=0){
    char buffer[100] = "";
    GetWindowText(hwnd,buffer,50);
    MoveWindow(hwnd,0,0,500,500,TRUE);
    SendMessage(hwnd,WM_KEYDOWN,VK_NUMPAD0,0);
    SendMessage(hwnd,WM_KEYUP,VK_NUMPAD0,0);
    printf("%d\n%s\n",hwnd,buffer);

    }else{
    printf("%d\n",hwnd);
    printf("failure\n");
    }


    After running this program,the MoveWidnow() function worked,but there isn't any character in notepad text entry...
    What should I do to solve this problem?
    Any suggestion is appreciated!
    Last edited by linkstack; April 9th, 2010 at 10:35 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: can't send WM_KEYDOWN correctly?

    Use SendInput API instead.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2010
    Posts
    11

    Re: can't send WM_KEYDOWN correctly?

    Consider keybd_event() function, it emulates keyboard events, but it will work only if your window is focused.

    Use WM_SETTEXT message to add text into that window, it has not to be focused.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: can't send WM_KEYDOWN correctly?

    Quote Originally Posted by davidk View Post
    Consider keybd_event() function, it emulates keyboard events, but it will work only if your window is focused.
    keybd_event is obsolete and should not be used in Win32 applications. SendInput API should be used instead.
    Victor Nijegorodov

  5. #5
    Join Date
    Sep 2009
    Posts
    8

    Re: can't send WM_KEYDOWN correctly?

    thanks for your reply,but when I'm compiling the program,following error occurs:

    KEYBDINPUT undefined

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: can't send WM_KEYDOWN correctly?

    Have a look at this thread
    Victor Nijegorodov

  7. #7
    Join Date
    Sep 2009
    Posts
    8

    Re: can't send WM_KEYDOWN correctly?

    sorry for my no reply.

    now I can use SendInput(),but after I invoked the sendinput(),there isn't anything happend in notepad

    this is my code:
    KEYBDINPUT kb={0};
    kb.wVk = 0x46;
    kb.wScan = 0;
    kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4


    INPUT input = {0};
    input.type = INPUT_KEYBOARD;
    input.ki = kb;


    SendInput(1,&input,sizeof(input));

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: can't send WM_KEYDOWN correctly?

    Quote Originally Posted by linkstack View Post
    sorry for my no reply.

    now I can use SendInput(),but after I invoked the sendinput(),there isn't anything happend in notepad

    this is my code:
    Code:
    KEYBDINPUT kb={0};
    	kb.wVk    =  0x46;
        kb.wScan = 0;
        kb.dwFlags = KEYEVENTF_UNICODE; // KEYEVENTF_UNICODE=4
    
    
    	INPUT input = {0};
    	input.type = INPUT_KEYBOARD;
        input.ki = kb;
    
    
    	SendInput(1,&input,sizeof(input));
    From MSDN:
    KEYEVENTF_UNICODE Windows 2000:
    If specified, the system synthesizes a VK_PACKET keystroke. The wVk parameter must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For more information, see the Remarks section.
    Victor Nijegorodov

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

Featured