Python Forum
How to not open the context menu if a mouse gesture is executed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to not open the context menu if a mouse gesture is executed?
#1
I have a program that is working, but it opens the context menu after simulating the commands. (That's because it's done with the right mouse button).

Is there a way to not open the context menu if a mouse gesture is executed?

from pynput.mouse import Listener, Button
from pynput.keyboard import Key, Controller
import time

is_right_button_pressed = False
initial_x = 0
initial_y = 0
min_movement = 20
max_x_deviation = 10
max_y_deviation = 10

keyboard = Controller()

right_button_pressed_time = 0

def on_click(x, y, button, pressed):
    global is_right_button_pressed, initial_x, initial_y, right_button_pressed_time

    if button == Button.right:
        if pressed:
            is_right_button_pressed = True
            initial_x = x
            initial_y = y
            right_button_pressed_time = time.time()
        else:
            if is_right_button_pressed:
                is_right_button_pressed = False
                elapsed_time = time.time() - right_button_pressed_time
                if elapsed_time <= 2:
                    x_diff = initial_x - x
                    y_diff = initial_y - y

                    if abs(y_diff) <= max_y_deviation:
                        if x_diff > min_movement: 
                            keyboard.press(Key.alt)
                            keyboard.press(Key.left)
                            keyboard.release(Key.left)
                            keyboard.release(Key.alt)
                        elif x_diff < -min_movement: 
                            keyboard.press(Key.alt)
                            keyboard.press(Key.right)
                            keyboard.release(Key.right)
                            keyboard.release(Key.alt)
                    if abs(x_diff) <= max_x_deviation:
                        if y_diff > min_movement:
                            keyboard.press(Key.home)
                            keyboard.release(Key.home)
                        elif y_diff < -min_movement:
                            keyboard.press(Key.end)
                            keyboard.release(Key.end)
                        if y_diff < -min_movement and x_diff > min_movement:
                            keyboard.press(Key.win)
                            keyboard.press('d')
                            keyboard.release('d')
                            keyboard.release(Key.win)

def on_press(key):
    if key == Key.menu:
        return False

with Listener(on_click=on_click) as listener:
    with Listener(on_press=on_press) as context_listener:
        listener.join()
        context_listener.join()
I want that the context menu does not appear if a mouse gesture action is performed.
Larz60+ write Aug-21-2023, 02:49 AM:
Added the tags for you this time. Please use bbcode tags on future posts. See link in deanhystad's moderation note below
deanhystad write Aug-20-2023, 07:16 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
from pynput.mouse import Listener, Button
from pynput.keyboard import Key, Controller
import time

is_right_button_pressed = False
initial_x = 0
initial_y = 0
min_movement = 20
max_x_deviation = 10
max_y_deviation = 10

keyboard = Controller()

right_button_pressed_time = 0
dragging_right_button = False

def on_click(x, y, button, pressed):
    global is_right_button_pressed, initial_x, initial_y, right_button_pressed_time, dragging_right_button

    if button == Button.right:
        if pressed:
            is_right_button_pressed = True
            initial_x = x
            initial_y = y
            right_button_pressed_time = time.time()
        else:
            if is_right_button_pressed:
                is_right_button_pressed = False
                elapsed_time = time.time() - right_button_pressed_time
                if elapsed_time <= 2:
                    x_diff = initial_x - x
                    y_diff = initial_y - y

                    if abs(x_diff) > min_movement or abs(y_diff) > min_movement:
                        dragging_right_button = True

                    if abs(y_diff) <= max_y_deviation:
                        if x_diff > min_movement: 
                            keyboard.press(Key.alt)
                            keyboard.press(Key.left)
                            keyboard.release(Key.left)
                            keyboard.release(Key.alt)
                        elif x_diff < -min_movement: 
                            keyboard.press(Key.alt)
                            keyboard.press(Key.right)
                            keyboard.release(Key.right)
                            keyboard.release(Key.alt)
                    if abs(x_diff) <= max_x_deviation:
                        if y_diff > min_movement:
                            keyboard.press(Key.home)
                            keyboard.release(Key.home)
                        elif y_diff < -min_movement:
                            keyboard.press(Key.end)
                            keyboard.release(Key.end)
                        if y_diff < -min_movement and x_diff > min_movement:
                            keyboard.press(Key.win)
                            keyboard.press('d')
                            keyboard.release('d')
                            keyboard.release(Key.win)

def on_release(x, y, button):
    global dragging_right_button
    if button == Button.right:
        dragging_right_button = False

def on_press(key):
    if key == Key.menu:
        return False

def suppress_right_click(event):
    if event.button == Button.right and dragging_right_button:
        return False
    return True

with Listener(on_click=on_click, on_release=on_release) as listener:
    with Listener(on_click=suppress_right_click, on_press=on_press) as context_listener:
        listener.join()
        context_listener.join()
Don't work, can help me ?
Reply
#3
The pynput listener does have a flag for suppressing events. It doesn't look like there is a way to turn this on and off.

Start by limiting what events are suppressed.

https://pynput.readthedocs.io/en/latest/faq.html

If this gives you want you want, great. If you want to pass along some events that are getting suppressed, you can mimic them using pynput. I wouldn't think that pynput will capture events for mouse events it creates.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PROBLEM] Removing IDLE from context menu WebSpider 1 479 Sep-28-2023, 03:35 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,311 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 902 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  with open context inside of a recursive function billykid999 1 588 May-23-2023, 02:37 AM
Last Post: deanhystad
  Context-sensitive delimiter ZZTurn 9 1,515 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,354 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,048 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  open the html page from the django dropdown menu? shams 2 3,338 Jul-17-2021, 08:10 AM
Last Post: shams
  I don't get the order of how things are executed with main() pythonrookie 5 2,591 Jul-21-2020, 01:35 PM
Last Post: pythonrookie
  Script works when executed from command prompt but not when executed in SDP Tippex 0 2,019 Apr-07-2020, 04:26 PM
Last Post: Tippex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020