Skip to content

Commit

Permalink
Make winmouse generate double click events
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Oct 7, 2017
1 parent 99f71a5 commit bc8d989
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
- To avoid depending on X the Linux parts reads raw device files (`/dev/input/input*`) but this requries root.
- Other applications, such as some games, may register hooks that swallow all key events. In this case `mouse` will be unable to report events.
"""
# TODO
# - infinite wait
# - mouse.on_move
import time as _time

import platform as _platform
Expand Down
12 changes: 12 additions & 0 deletions mouse/_winmouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class MSLLHOOKSTRUCT(Structure):
DispatchMessage = user32.DispatchMessageA
DispatchMessage.argtypes = [LPMSG]

GetDoubleClickTime = user32.GetDoubleClickTime

# Beware, as of 2016-01-30 the official docs have a very incomplete list.
# This one was compiled from experience and may be incomplete.
WM_MOUSEMOVE = 0x200
Expand Down Expand Up @@ -128,8 +130,12 @@ class MSLLHOOKSTRUCT(Structure):

init = lambda: None

previous_button_event = None # defined in global scope
def listen(queue):

def low_level_mouse_handler(nCode, wParam, lParam):
global previous_button_event

struct = lParam.contents
# Can't use struct.time because it's usually zero.
t = time.time()
Expand All @@ -144,6 +150,12 @@ def low_level_mouse_handler(nCode, wParam, lParam):
button = {0x10000: X, 0x20000: X2}[struct.data]
event = ButtonEvent(type, button, t)

if (event.event_type == DOWN) and previous_button_event is not None:
if event.time - previous_button_event.time <= GetDoubleClickTime() / 1000.0:
event = ButtonEvent(DOUBLE, event.button, event.time)

previous_button_event = event

queue.put(event)
return CallNextHookEx(NULL, nCode, wParam, lParam)

Expand Down

0 comments on commit bc8d989

Please sign in to comment.