Skip to content

Instantly share code, notes, and snippets.

@mamrehn
Created November 18, 2022 13:13
Show Gist options
  • Save mamrehn/165b12e22c4e4ca0bb5cf27c7f01e440 to your computer and use it in GitHub Desktop.
Save mamrehn/165b12e22c4e4ca0bb5cf27c7f01e440 to your computer and use it in GitHub Desktop.
Pin mouse cursor to top-right corner
import time
from datetime import datetime
import pyautogui as autogui
autogui.FAILSAFE = False
# autogui.FAILSAFE = True # DEV mode: move mouse to upper left corner to exit
def has_point_moved(p1, p2, threshold = 5):
x_diff = abs(p1.x - p2.x)
y_diff = abs(p1.y - p2.y)
if (x_diff + y_diff) > threshold:
return True
return False
autogui.moveTo(x=autogui.size().width - 5, y=5, duration=2.0, tween=autogui.easeInQuad)
original_cursor_position = autogui.position()
current_cursor_position = original_cursor_position
screen_width = autogui.size().width
screen_height = autogui.size().height
start_time = datetime.now()
while (datetime.now() - start_time).seconds < 200:
is_diff = has_point_moved(original_cursor_position, current_cursor_position)
if is_diff:
# https://pyautogui.readthedocs.io/en/latest/mouse.html#tween-easing-functions
autogui.moveTo(x=screen_width - 5, y=5, duration=2.0, tween=autogui.easeInQuad)
original_cursor_position = autogui.position()
time.sleep(0.25)
current_cursor_position = autogui.position()
if current_cursor_position.x > (screen_width - 10) and current_cursor_position.y > (screen_height - 10):
answer = autogui.prompt(text='Wollen Sie das Programm beenden?', title='Wirklich beenden?' , default='Nein')
if answer is not None and answer.lower().strip() == 'ja':
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment