How to not open the context menu if a mouse gesture is executed?

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.

All you need to know in order to answer questions like this yourself is the appropriate terminology.

In the world of GUI design and processing, we say that an event is “suppressed” when a handler doesn’t pass it on to do whatever it would normally do (in this case, opening the context menu from a right-click). This is because “the event is passed on” is our default expectation. (That’s usually what we want; for things to work the other way around, we’d constantly have to re-create the events to pass on explicitly.)

I put pynput suppress into a search engine and got the answer right off the top from Stack Overflow:

But other searches work too, e.g. pynput prevent default action at DuckDuckGo .

1 Like

I read the forum you sent me and I tried to put the ( suppress=True ) in my prgogram but it just locks the mouse.
In my case it uses the keyboard and the mouse, it reads the mouse movement and then identifies which keys it needs to press.
What I would like is when I release the right mouse button, it doesn’t open the context menu.

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 :frowning: