-5

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 pythoncom, pyHook 

def OnMouseEvent(event): #triggers mouseClick function
    mouseClick(event)
    return True

def mouseClick(event):

    if event.MessageName == "mouse left up": # makes sure that's the event I'm looking for
        a = True # disables hm.MouseAll
        return a
        time.sleep(0.01)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        time.sleep(0.005)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        time.sleep(0.01)
        a = False # enables hm.MouseAll back
        return a


a = False      
# create a hook manager
hm = pyHook.HookManager()
# set the hook
hm.HookMouse()
# wait forever
pythoncom.PumpMessages()
# watch for all mouse events
while True:
    if a == False:
        hm.MouseAll = OnMouseEvent # Triggers OnMouseEvent function       
    else:
        pass
Beter
  • 314
  • 4
  • 20

1 Answers1

1

What I understand is that you already have a function ready. I would use Pygame package for this.

for event in pygame.event.get():    
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        nameoffunction()

changing the number 1 in event.button == 1: will change that what mouse button is clicked.

EDIT 1

run = 1 
while run == 1:
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        print("Message")

PyGame requires a window so if you just want to test a click I would then use...

EDIT 2

if win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0):
    print("Message")

x,y = x and y starting coordinates for box corner 0,0 = Other corner for box (i think) know if mouse is inside of the "box" and you click it should work (I haven't got win32api installed so haven't ran any tests with this)

SnootierBaBoon
  • 127
  • 1
  • 13
cmdtvt
  • 71
  • 1
  • 10
  • I don't know why but I still don't get what I want. I don't have my function ready yet, but I tried this out setting a print as result of the if. The problems is that nothing gets printed when I click my left mouse click D: – Beter Jun 29 '17 at 00:36
  • I posted my code in another answer, would be great if you could take a look at it. – Beter Jun 29 '17 at 00:38
  • Ok so first you wanna make a basic while loop and put the 'for event' thing inside that. So the code runs again and again. `run = 1 while run == 1: for event..... ` – cmdtvt Jun 29 '17 at 00:41
  • Then just replace nameoffunction() with print("Messsage") – cmdtvt Jun 29 '17 at 00:43
  • I've done what you said but still not working. I updated my code in the other answer. – Beter Jun 29 '17 at 00:48
  • Oh and you don't have window. If you decide to use pygame it requires that you have a window created. – cmdtvt Jun 29 '17 at 00:48
  • I don't wanna trigger this function when clicking in a pygame window, yet I want it to be triggered in any Windows' window. – Beter Jun 29 '17 at 00:53
  • I think I got 2 problems actually. The first one is that if I put 0,0 instead of x,y and then run the code my PC starts freaking out for whatever reason. The second one is that the function that has to be triggered calls the win32api.mouse_event again, which might be an issue but I am not sure. – Beter Jun 29 '17 at 01:05
  • I got the function ready now, I can post the whole code if you want me to. – Beter Jun 29 '17 at 01:06
  • Yes pelace i think i'm gonna have the same problem in few days :P – cmdtvt Jun 29 '17 at 01:07
  • Apparently python starts clicking thousands of times per second, that's why my PC freaks out... I don't know why that happens though. – Beter Jun 29 '17 at 01:25
  • hmmm try removing win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) – cmdtvt Jun 29 '17 at 01:41
  • Nope, still not working. I think if win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0): is being the issue here, python starts clicking even if the function does not get triggered – Beter Jun 29 '17 at 01:48