Controling other applications, by publishing events from host application.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Controling other applications, by publishing events from host application.

  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Controling other applications, by publishing events from host application.

    Hi,
    I'm working on a test project in which I'm planning to control " Need For Speed " game from another application..

    What I actually meant is , you might have played nfs game where the car movement is controlled by "Arrow keys/ Navigation keys "...... So I'm planning to simulate this key press event melodramatically based on some algorithm, and expecting NFS to react to it.

    Here is my questions.
    I can simulate key press event but , how do i need to publish it , so that other applications gets a notification that a key is pressed....

    Thanks in advance.

    Regards,
    Vijay.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: Controling other applications, by publishing events from host application.

    Did you try SendInput API?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Hi Victor,
    No I didn't try it. Ok i'll check on that.

    Regards,
    Vijay.

  4. #4
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Hi,
    I'm trying to use SendInput() Win32 API in my application to simulate keyboard left arrow key press event.
    From the help document I had written below code. but this is not working , can someone help regarding the same.
    Also I'm not using any UNICODE characters.


    int _tmain(int argc, _TCHAR* argv[])
    {
    INPUT output[2];
    while(1)
    {
    // Left key down
    output[0].type = INPUT_KEYBOARD;
    output[0].ki.wVk = VK_LEFT;
    output[0].ki.time = 0;
    output[0].ki.dwExtraInfo = 0;

    // Left arrow key up
    output[1].type = INPUT_KEYBOARD;
    output[1].ki.wVk = VK_LEFT;
    output[1].ki.dwFlags = KEYEVENTF_KEYUP ;
    output[1].ki.time = 0;
    output[1].ki.dwExtraInfo = 0;

    int retval1 = SendInput(2, output, sizeof(INPUT));

    Sleep(500);
    }
    return 0 ;
    }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: Controling other applications, by publishing events from host application.

    1. Define "not working".
    2. Make sure the App you want to send keys is active before calling SendInput. SetForegroundWindow is the function to call to make it active.
    3. Have a look at:
    http://msdn.microsoft.com/en-us/magazine/cc163867.aspx
    http://www.codeguru.com/forum/showthread.php?t=377393
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Hi Victor,

    Here are the answers for your queries.

    1) Define "not working".
    When I run this program, and bring any other application into foreground say " MS world " and place cursor in editor area, which already has some characters typed, the cursor should move to "Left" once in every half a second , and this is not observed.

    2. Make sure the App you want to send keys is active before calling SendInput. SetForegroundWindow is the function to call to make it active.

    Since I want some other apps (car race ) to respond to key events, I need not call "SetForegroundWindow " in my host app. The destination app which is expected to react to key events is brought to foreground , by setting the focus on the respective form.

    Yes, Thanks for the links. Kindly let me know am I missing anything in this.

    Regards,
    Vijay

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: Controling other applications, by publishing events from host application.

    Quote Originally Posted by VijayDandur View Post
    Code:
    	int retval1 = SendInput(2, output, sizeof(INPUT));
    What is the value of retval1?
    Victor Nijegorodov

  8. #8
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Return value is "2" which is successful.

  9. #9
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Also, could you please let me know what is "KEYEVENTF_EXTENDEDKEY" and "KEYEVENTF_SCANCODE"

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: Controling other applications, by publishing events from host application.

    Please, see MSDN article KEYBDINPUT Structure
    Victor Nijegorodov

  11. #11
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Hi Victor,
    Thanks for the info.

    With the below changes, the code is working for me.

    INPUT output[2];
    ZeroMemory(&output, sizeof(output));

    Regards,
    Vijay.

  12. #12
    Join Date
    Feb 2009
    Posts
    24

    Re: Controling other applications, by publishing events from host application.

    Hi Victor,

    The program is working as expected with the above code changes, but I'm facing another issue.

    The intent of writing this code is to send events to another program by running this.

    So, if I start this host program and bring any "car race game " which expects "arrow key events" to foreground the other application (car race or some other game) will not react to this.

    but the same program when executed for windows world document, I can see that the cursor moves to left once in half a minute.



    So what do I need to do to resolve this.



    Regards,

    Vijay.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center