0

I want to use mouse listener in Pynput Module to monitor the click event. Ant according to the event status, it will trigger one function. But it seems something wrong with the event status.

    from pynput.keyboard import Controller, Key, Listener
    from pynput import mouse
    from pynput import keyboard
    import pythoncom
    from ctypes import *


    flag = False
    def on_right_click(x, y, button, pressed):
            global flag
            # boolFlag = False
            button_name = ''
            if button == mouse.Button.right:
                    user32 = windll.LoadLibrary('user32.dll')
                    if flag == True:
                            user32.BlockInput(False)
                            flag = False
                    else:
                            user32.BlockInput(True)
                            flag = True
                    print(flag)
                    button_name = 'Right Button'
            elif button == mouse.Button.left:
                    button_name = 'Left Button'
            if pressed:
                    print('{0} is pressed at {1}, {2}'.format(button_name, x, y))
            if not pressed:
                    return False

    while True:
            with mouse.Listener(on_click=on_right_click) as listener:
                    listener.join()

Normally, Flag will change once and it will trigger the BlockInput function, but below is the real results:

    True
    Right Button is pressed at 3478, 303
    False

So, why the flag is changed twice?

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50
Eric Gong
  • 107
  • 1
  • 1
  • 10

1 Answers1

0

System creates two events

  • one when you start pressing button - offten called "press"
  • one when you stop pressing button - offten called "release".

In other programs "click" is run only with one events (I'm not sure but it can be "release") but here listener runs the same function for both events. So finally you can see two messages.

But for keyboard's listener uses separated method for press and release.

Both mouse's events are used to drag/move elements on screen.

furas
  • 134,197
  • 12
  • 106
  • 148
  • You mean that, mouse listener cannot be used separately for press and release? Do you have any method to separate the event? – Eric Gong Jul 09 '19 at 05:39