Welcome, guest | Sign In | My Account | Store | Cart

Run script from cmd.

Python, 42 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-

'''
Click counter

Left click: +1
Right click: -1
Middle click: Reset
'''

import pyHook
import pythoncom

print(__doc__)

click = 0

def left_down(event):
    global click
    click += 1
    print(click)
    return True

def right_down(event):
    global click
    click -= 1
    print(click)
    return True    

def middle_down(event):
    global click
    click = 0
    print(click)
    return True     

hm = pyHook.HookManager()
hm.SubscribeMouseLeftDown(left_down)
hm.SubscribeMouseRightDown(right_down)
hm.SubscribeMouseMiddleDown(middle_down)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

2 comments

Tom Snelling 11 years, 9 months ago  # | flag

Here is my version of the Click Counter, using only Tkinter which is a default module, unlike pyHook and pythoncom are.

from Tkinter import *
import os

click = 0

def left_down(event):
    global click
    os.system('cls')
    click += 1
    print('Clicks: ' + str(click))
    return True

def right_down(event):
    global click
    os.system('cls')
    click -= 1
    print('Clicks: ' + str(click))
    return True

def middle_down(event):
    global click
    os.system('cls')
    click = 0
    print('Clicks: ' + str(click))
    return True

root = Tk()

root.title('Click Here')

root.minsize(height=300, width=300)

root.bind("<Button-1>", left_down)
root.bind("<Button-3>", right_down)
root.bind("<Button-2>", middle_down)

root.mainloop()
Krystian Rosiński (author) 11 years, 9 months ago  # | flag

Thanks! The difference is that you're counting clicks only within a Tkinter window. I use my script for counting objects in complex CAD drawings, PDF files, etc. -- just clicking.