Thread: (Windows) Send keyboard and mouse to another program?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    (Windows) Send keyboard and mouse to another program?

    I already know how to send mouse co-ordinates and click with the mouse (using SendInput()), however it does not work.

    I would also like to know how to send text (several words, though, not single keys) to other programs.

    As a side note, if anyone knows how to take words from an image (like text-recognition), I'd like to know that too.

    Thanks.

    I have this code to move and click the mouse:
    Code:
    int x, y;
    double fScreenWidth  = ::GetSystemMetrics(SM_CXSCREEN)-1;
    double fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN)-1;
    double fx = x*(65535.0f/fScreenWidth);
    double fy = y*(65535.0f/fScreenHeight);
    INPUT Input = {0};
    Input.type        = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE; // Move to the co-ordinates
    Input.mi.dx = fx;
    Input.mi.dy = fy;
    ::SendInput(1,&Input,sizeof(INPUT));                       // Send the input
    ::ZeroMemory(&Input,sizeof(INPUT));                        // Clear the memory used
    Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
    ::SendInput(1,&Input,sizeof(INPUT));                       // Send the input
    ::ZeroMemory(&Input,sizeof(INPUT));                        // Clear the memory used
    Input.type        = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
    ::SendInput(1,&Input,sizeof(INPUT));                       // Send the input
    ::ZeroMemory(&Input,sizeof(INPUT));                        // Clear the memory used
    and although I have included windows.h; my compiler claims that INPUT and INPUT_MOUSE have not been declared. This is quite confusing...
    Edit: My compiler is Dev-C++ 4.9.9.2
    Last edited by Chrisname; 06-08-2009 at 01:34 PM.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    The best way to do this is by getting a handle to the window in question and sending it message directly, your method involves inputting directly into the raw system input queue. If you are using Dev-C++ chances are you are including windows.h but since chances are you dont have the windows sdk it wont do anything. Post all the code.

    Do you have the windows platform sdk installed and the correct paths setup in Dev-C++.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    INPUT_MOUSE are declared constants declared in the header winuser.h which is included by windows.h. I have no clue what you are talking about valaris. It has nothing to do with any SDK.

    In WinUser.h it declares INPUT_MOUSE as
    Code:
    #define INPUT_MOUSE 0x00000000
    . You could just redefine it but it's still strange that it's not already defined since winuser.h is included inside of the windows.h. Another problem is INPUT which is a structure which you SHOULDN'T redefine. Never the less, in order for us to help you more, we would need to see how you've setup your include files.
    Last edited by computerquip; 06-12-2009 at 10:49 PM. Reason: Called INPUT a constant without noticing

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by computerquip View Post
    INPUT_MOUSE are declared constants declared in the header winuser.h which is included by windows.h. I have no clue what you are talking about valaris. It has nothing to do with any SDK.

    In WinUser.h it declares INPUT_MOUSE as
    Code:
    #define INPUT_MOUSE 0x00000000
    . You could just redefine it but it's still strange that it's not already defined since winuser.h is included inside of the windows.h. Another problem is INPUT which is a structure which you SHOULDN'T redefine. Never the less, in order for us to help you more, we would need to see how you've setup your include files.
    Apparently you've never heard of the windows sdk. Afterall, where do you think windows.h comes from? And yes more information would be needed to answer this question as was previously mentioned.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Valaris I'm aware. But constant definitions like INPUT_MOUSE, are defined outside of the library and inside of the header which doesn't require the library. That's what I'm trying to say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. how to handle keyboard and mouse events for all windows
    By manav-II in forum Windows Programming
    Replies: 3
    Last Post: 08-25-2008, 09:03 AM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. What would you like in a interface?
    By commanderrulz in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-29-2002, 05:29 AM