Thread: Key Presses and Mouse Clicks

  1. #1

    Question Key Presses and Mouse Clicks

    How can I simulate key presses and mouse clicks through code?
    I'm using Visual C++ and I'm making a Win32 application
    NOT (!) using MFC.

    I know of the SendInput(.......) function that is supposed to do this.

    However, when I add it to my code and compile I get an error saying SendInput() is an unknown function although I included the header file for this function that was specified in the MSDN documentation.

    Is there another function I can use to do this or can anyone get the SendInput function to work?

    Last option - if anyone knows how to do this using MFC and no other way, please tell me.

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    This will probably work:
    case WM_KEYRIGHT:
    //do whatever it's supposed to do
    This is when the right arrow key is pressed. This goes in the message thing switch (nMsg). There are others to. Here's the mouse one:
    //do whatever it's supposed to do
    That goes in the same place. This is when the left mouse button is clicked.

  3. #3
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Have you considered the SendMessage function.
    It allows you to send a messages to your own meessage queue like this;

            case WM_CHAR: 
                switch (wParam) 
                    case 0x08:          // Backspace 
                    // Move the caret back one space, and then 
                    // process this like the DEL key. 
                        if (nCaretPosX > 0) 
                            SendMessage(hwnd, WM_KEYDOWN, 
                                VK_DELETE, 1L); 
                    case 0x09:          // Tab 
                    // Tab stops exist every four spaces, so add 
                    // spaces until the user hits the next tab. 
                            SendMessage(hwnd, WM_CHAR, ' ', 1L); 
                        } while (nCaretPosX % 4 != 0); 
    You get the idea.

    Here's the info on it.

    LRESULT SendMessage(
      HWND hWnd,      // handle to destination window
      UINT Msg,       // message
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter

    [in] Handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.

    [in] Specifies the message to be sent.

    [in] Specifies additional message-specific information.

    [in] Specifies additional message-specific information.

    Return Values
    The return value specifies the result of the message processing; it depends on the message sent.
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

  4. #4
    Join Date
    Jan 2002
    If you search the winuser.h header for the SendInput fn you will find it is defined within a #if (_WIN32_WINNT > 0x0400)....#endif block.

    So try #defining _WIN32_WINNT 0x0401 prior to #include <windows.h> (or something more appropriate to your os version).
    Although this should compile with no errors there is no guarantee the fn will work.

    I'd be curious to find out if 'SendInput' does work - and what os you are compiling on. Might be a useful addition to the FAQ.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with mouse bug
    By Magos in forum Windows Programming
    Replies: 4
    Last Post: 01-25-2003, 06:33 AM
  2. Detecting right mouse button
    By abrege in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2002, 09:36 PM
  3. Mouse Interrupts
    By linuxman132 in forum C Programming
    Replies: 9
    Last Post: 12-26-2001, 11:19 AM
  4. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of
    Replies: 16
    Last Post: 11-11-2001, 01:38 AM