Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to read a mouse left click event in python?

For example, I would like to make a code like this:

while True:
[...]

   if <left mouse button is clicked>:
      print("Left Mouse has been clicked")

[...]

cv2.destroyAllWindows

I've the the "Mouse as a Paint-Brush", but it does not explain how to have python just recognize the mouse left click event.

I'd appreciate any help I can get.

How to read a mouse left click event in python?

For example, I would like to make a code like this:

while True:
[...]

   if <left mouse button is clicked>:
      print("Left Mouse has been clicked")

[...]

cv2.destroyAllWindows

I've the read the "Mouse as a Paint-Brush", but it does not explain how to have python just Python only recognize the mouse left click event.

I'd appreciate any help I can get.

How to read a mouse left click event in python?

For example, I would like to make a code like this:

while True:
[...]

   if <left mouse button is clicked>:
      print("Left Mouse has been clicked")

[...]

cv2.destroyAllWindows

I've read the "Mouse as a Paint-Brush", but it does not explain how to have Python only recognize the mouse left click event.

I'd appreciate any help I can get.

I'd tried modifying the code from the tutorial to suit my needs, but I can't seem to get the code to work:

def mouse(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        return True

[...]
cap = cv2.VideoCapture(0)

while True:
    [...]
       if cv2.setMouseCallback('frame', mouse)==True
         print("Left Mouse Button has been clicked")
    [...]

cv2.destroyAllWindows

How to read a mouse left click event in python?

For example, I would like to make a code like this:

while True:
[...]

   if <left mouse button is clicked>:
      print("Left Mouse has been clicked")

[...]

cv2.destroyAllWindows

I've read the "Mouse as a Paint-Brush", but it does not explain how to have Python only recognize the mouse left click event.

I'd appreciate any help I can get.

I'd tried modifying the code from the tutorial to suit my needs, but I can't seem to get the code to work:

def mouse(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        return True

[...]
cap = cv2.VideoCapture(0)

while True:
    [...]
     if cv2.setMouseCallback('frame', mouse)==True
   mouse)==True:
      print("Left Mouse Button has been clicked")
    [...]

cv2.destroyAllWindows

cv2.destroyAllWindows

How to read a mouse left click event in python?

For example, I would like to make a code like this:

while True:
[...]

   if <left mouse button is clicked>:
      print("Left Mouse has been clicked")

[...]

cv2.destroyAllWindows

I've read the "Mouse as a Paint-Brush", but it does not explain how to have Python only recognize the mouse left click event.

I'd appreciate any help I can get.

I'd tried modifying the code from the tutorial to suit my needs, but I can't seem to get the code to work:

def mouse(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        return True

[...]
cap = cv2.VideoCapture(0)

while True:
    [...]
    if cv2.setMouseCallback('frame', mouse)==True:
      print("Left Mouse Button has been clicked")
    [...]

cv2.destroyAllWindows

[Update]

I managed make progress by using the following code I modified from the more advanced example on the "Mouse as a Paint-Brush" tutorial. I modified the code for the purpose of having OpenCV recognize a mouse left-button click on a live webcam:

[...]
cap = cv2.VideoCapture(0)
drawing = False

def draw_circle(event,x,y,flags,param)
   global drawing
   if event == cv2.EVENT_LBUTTONDBCLK:
      drawing = True

while True:
  [...]
  drawing = False
  cv2.setMouseCallback('frame',draw_circle)

  if drawing == True:
     print("Left Mouse Button has been clicked.")
  [...]

cv2.destroyAllWindows