0

I want to handle action double click by clicking left_mouse 2 times.Between 2 click-time,I sleep 100ms

SendInput(LEFT_CLICK...);
Sleep(100);
SendInput(LEFT_CLICK...);

It works OK in my PC,but not works correctly in virtual machine May be,There is a delay-time when machine do function "SendInput"

Eventhough I remove "Sleep(100)",It just clicks 2 times and doesn't "double click" as I want.

How to handle double click exactly in this case

Please suggest me anyway to do it

Thanks,

quanrock
  • 89
  • 2
  • 10

1 Answers1

1

Btw you should specify what environment you're working in and make your code a bit more detailed. Using SendInput is one option, I don't know what you're trying to do exactly, but I'll give you two more options to try for simulating clicks. Something like this would works fine (I code in python but it should be the same idea):

def leftClick(x=0, y=0):
    win32api.SetCursorPos((x,y)) #set the cursor to where you wanna click
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) #generate a mouse event
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    return True

def doubleClick(x=0, y=0):
    leftClick(x,y)
    leftClick(x,y)

You could sleep for 50 ms in between time.sleep(0.05) but it works for me without it and I've tested in a vm.

Another option if you want to perform silent clicks without having to move the cursor, you can send a message to the window where you want to click knowing the window handle (hwnd), here I assume you pass the handle as a parameter.

def leftClick(x=0, y=0, hwnd):
   lParam = win32api.MAKELONG(x,y) # create a c long type to hold your click coordinates
   win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lparam) # send a message to the window that the mouse left button is down.
   win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, lparam) # send a message to the window that the mouse left button is up.
   return True

def doubleClick(x=0, y=0, hwnd):
   leftClick(x,y, hwnd)
   leftClick(x,y, hwnd)

or you can send the message WM_LBUTTONDBLCLK up to you.

TheCodingGent
  • 110
  • 2
  • 10