Python Forum
How to trigger a function by clicking the left mouse click?
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to trigger a function by clicking the left mouse click?
#1
I want to start off by saying that I am new to Python so I am sorry if this question is going to sound stupid to you.

I am just looking for an easy way to trigger a function whenever I press the left click of my mouse. Could anyone illustrate me how to achieve this? Examples are greatly appreciated.

My code now:

import win32api
import win32con
import time
from random import randint
#import pygame

#pygame.init()

def mouseClick():
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
   time.sleep(0.005)
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)


while True :
   if win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0):
       time.sleep(0.03)
       mouseClick()
       time.sleep(0.3)
That's what I've done so far, but that doesn't work as python starts clicking thousands times per second.
Reply
#2
win32api.mouse_event doesn't check the state of something, it just does that thing.  So in your while loop, you're not checking if the left mouse button is down, you're just setting it to be down over and over.

Instead of mouse_event, you should probably use GetAsyncKeyState(), and instead of the event code MOUSEEVENTF_LEFTDOWN, you should use VK_LBUTTON (the mouse is part of the "virtual keyboard", thus the prefix VK).

Here's a small sample:
>>> def is_mouse_down():
...   key_code = win32con.VK_LBUTTON
...   state = win32api.GetAsyncKeyState(key_code)
...   return state != 0
...
# now for some intense mouse-clicking to test
>>> for _ in range(10):
...   print(is_mouse_down())
...   time.sleep(1)
...
False
False
True
False
True
True
False
True
False
True
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Non repetitive mouse click Sartre 5 1,375 Apr-22-2023, 06:46 PM
Last Post: deanhystad
  PyQT5 - align left frohr 7 3,835 May-07-2022, 09:56 PM
Last Post: deanhystad
  how to mouse click a specific item in pygame? Frankduc 5 1,661 May-03-2022, 06:22 PM
Last Post: Frankduc
  How to trigger for loop after while quest 2 1,488 Mar-22-2022, 11:34 PM
Last Post: quest
  How did one column get left-justified? Mark17 6 1,874 Feb-26-2022, 11:55 PM
Last Post: deanhystad
Exclamation Phyton Metin2 Game Left Click not working. Help anonym35 0 1,491 Feb-02-2022, 07:01 PM
Last Post: anonym35
  Clicking Every Page and Attachment on Website shoulddt 1 1,697 Jul-09-2021, 01:08 PM
Last Post: snippsat
  Pandas, How to trigger parallel loop Mekala 4 2,648 Oct-29-2020, 12:58 PM
Last Post: Mekala
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Move mouse and click in particular position biprabu 3 2,440 Sep-01-2020, 08:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020