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.