[RESOLVED] I'm able to move the mouse pointer and right click but left click doesn't work
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] I'm able to move the mouse pointer and right click but left click doesn't work

  1. #1
    Join Date
    Nov 2009
    Posts
    18

    [RESOLVED] I'm able to move the mouse pointer and right click but left click doesn't work

    Code:
    #define _WIN32_WINNT 0x0501
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    INPUT *mouse;
    
    mouse = new INPUT;
    mouse->type = INPUT_MOUSE;
    mouse->mi.dx = 65535;
    mouse->mi.dy = 65535;
    mouse->mi.mouseData = 0;
    mouse->mi.dwFlags = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
    mouse->mi.time = 0;
    mouse->mi.dwExtraInfo = 0;
    SendInput(1,mouse,sizeof(INPUT));
    
    mouse->mi.dx = 0;
    mouse->mi.dy = 0;
    mouse->mi.dwFlags = MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP;
    SendInput(1,mouse,sizeof(INPUT));
    
    return 0;
    }
    I do not know why right click works but not left click.

    EDIT: I use autohide so I clicked before the taskbar popped up. Problem resolved.
    Last edited by gregorian; December 6th, 2009 at 03:59 AM.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,960

    Re: RESOLVED - Able to move the mouse pointer and right click but left click doesn't

    Please explain your test case, and define "work" and "not work".
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,960

    Re: RESOLVED - Able to move the mouse pointer and right click but left click doesn't

    ...and contemplate a bit over this sample
    Code:
    #define _WIN32_WINNT 0x0501
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	INPUT mouse = {0};
    
    	mouse.type = INPUT_MOUSE;
    	mouse.mi.dx = 65535;
    	mouse.mi.dy = 65535;
    	mouse.mi.mouseData = 0;
    	mouse.mi.dwFlags = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
    	mouse.mi.time = 0;
    	mouse.mi.dwExtraInfo = 0;
    	SendInput(1, &mouse, sizeof(INPUT));  // move to the most right-bottom point
    
    	mouse.mi.dx = -10;
    	mouse.mi.dy = -10;
    	mouse.mi.dwFlags = MOUSEEVENTF_MOVE;
    	SendInput(1, &mouse, sizeof(INPUT));  // position it over the clock, a bit left and up
    
    	mouse.mi.dx = 0;
    	mouse.mi.dy = 0;
    	mouse.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    	SendInput(1, &mouse, sizeof(INPUT));  // press left button...
    
    	Sleep(40);                            // ...let Windows understand the action
    
    	mouse.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    	SendInput(1, &mouse, sizeof(INPUT));  // ...and release the button
    
    	return 0;
    }
    Last edited by Igor Vartanov; December 6th, 2009 at 03:26 PM.
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2009
    Posts
    18

    Re: I'm able to move the mouse pointer and right click but left click doesn't work

    I wanted to left click on the Show Desktop button. If it worked properly, the desktop would be visible.

    May I know why it's necessary for having an interval between the mouse down and mouse up events? Don't the two of them in succession imply a click? Do we have to give co-ordinate data for the mouse down and mouse up events? The program seems to work without them.

    The reason my program didn't work initially was because Autohide took a while to display the taskbar. Since the program was too fast, it ended up clicking on the desktop and as you'd expect nothing happened.

    Thank you for your help!

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,960

    Re: I'm able to move the mouse pointer and right click but left click doesn't work

    why it's necessary for having an interval between the mouse down and mouse up events?
    Well, I have a faint recollection about this point somewhere in the documentation, but not sure right now. Besides, it might be true for old versions, you know. Anyway, I think it's always better to completely imitate user's behavior. In real, your click isn't this swift, and there is always some interval between down and up.
    Do we have to give co-ordinate data for the mouse down and mouse up events?
    No we don't. It's just a habit about clearing any data that left the context where they had a sense.
    Best regards,
    Igor

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,243

    Re: [RESOLVED] I'm able to move the mouse pointer and right click but left click does

    Rather than using Sleep( ), set the time struct member.

    Code:
     
    mouse.mi.time = GetTickCount( );

  7. #7
    Join Date
    Jul 2012
    Posts
    3

    Re: [RESOLVED] I'm able to move the mouse pointer and right click but left click does

    I have almost the same case.
    I need to click on particulate point on desktop of my Windows XP. I write such code:

    Code:
    INPUT i;
    memset(&i, 0, sizeof(INPUT));
    
    i.type = INPUT_MOUSE;
    
    // msdn says about "normalized" coordinates, I recalculate it.
    // "Absolute" coordinates are: х = 66, у = 925.
    i.mi.dx = POS_X; // #define POS_X 3379
    i.mi.dy = POS_Y; // #define POS_Y 59199
    i.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_VIRTUALDESK;
    i.mi.mouseData = 0;
    i.mi.time = 0;
    
    SendInput(1, &i, sizeof(INPUT));
    i.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_VIRTUALDESK;
    SendInput(1, &i, sizeof(INPUT));
    It perfectly works with mouse clicks - left, right, no matter, I tried all. But - all this clicks are just in point where mouse cursor is, not at point with right coordinates. Of course, I can use SetCursorPos, and avoid the problem, but - why do not work SendInput with mouse movements?
    msdn says:

    If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface; coordinate (65535,65535) maps onto the lower-right corner. In a multimonitor system, the coordinates map to the primary monitor.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center