Skip to content

Instantly share code, notes, and snippets.

@zzhuolun
Created May 16, 2021 21:08
Show Gist options
  • Select an option

  • Save zzhuolun/20a25378c2967350abb2929c87b3f322 to your computer and use it in GitHub Desktop.

Select an option

Save zzhuolun/20a25378c2967350abb2929c87b3f322 to your computer and use it in GitHub Desktop.
import pyautogui, time, math
from matplotlib import pyplot as plt
import numpy as np
from numpy.polynomial import polynomial as P
from pynput import mouse, keyboard
def on_press1(key):
if key == keyboard.Key.esc:
return False # stop listener
try:
k = key.char # single-char keys
except:
k = key.name # other keys
if k == '1': # keys of interest
# self.keys.append(k) # store it in global-like variable
return False # stop listener; remove this if want more keys
COEF = [-1.25768188e-01, 4.32775032e-03, -3.49677682e-06]
while True:
listener = keyboard.Listener(on_press=on_press1)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
startx,starty = pyautogui.position()
print(f'start at ({startx}, {starty})')
listener = keyboard.Listener(on_press=on_press1)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
endx,endy = pyautogui.position()
print(f'desired endpoint at ({endx}, {endy})')
dist = math.sqrt((endx-startx)**2 + (endy-starty)**2)
t = COEF[0] + COEF[1]*dist + COEF[2]*dist**2
pyautogui.dragRel(1, 0, duration=t)
print(f'pressing time: {t}s ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment