Skip to content

Commit

Permalink
Fix a potential bug with mouse button emulation on Windows
Browse files Browse the repository at this point in the history
Re: #185.

If an error occurs when emulating mouse button events with the
SendInput() function and the primary mouse button was set by the
user to the right button, then that setting would not be restored,
leaving our left-handed user understandably confused the next time
they use the mouse.  This fixes that potential bug by swapping the
primary button back in a 'finally' block instead.
  • Loading branch information
drmfinlay committed May 30, 2021
1 parent 037fa43 commit 9823902
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions dragonfly/actions/mouse/_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ class ButtonEvent(BaseButtonEvent):

def execute(self, window):
# Ensure that the primary mouse button is the *left* button before
# sending events.
# sending events.
primary_changed = windll.user32.SwapMouseButton(0)

# Prepare and send the mouse events.
zero = pointer(c_ulong(0))
inputs = [MouseInput(0, 0, flag[1], flag[0], 0, zero)
for flag in self._flags]
array = make_input_array(inputs)
send_input_array(array)

# Swap the primary mouse button back if it was previously set to
# *right*.
if primary_changed:
windll.user32.SwapMouseButton(1)
try:
# Prepare and send the mouse events.
zero = pointer(c_ulong(0))
inputs = [MouseInput(0, 0, flag[1], flag[0], 0, zero)
for flag in self._flags]
array = make_input_array(inputs)
send_input_array(array)
finally:
# Swap the primary mouse button back if it was previously set
# to *right*.
if primary_changed:
windll.user32.SwapMouseButton(1)

0 comments on commit 9823902

Please sign in to comment.