即使窗口在后台运行,如何检测按键?

时间:2018-09-27 12:10:07

标签: python python-3.x python-requests

我从无聊开始就为GT制作了这个小型自动农场机器人,但是问题是,除了任务管理器之外,我没有其他方法可以停止它。所以我在想,无论前景位于哪个窗口,我的程序有没有办法检测是否按下了任何键?

这是我现在正在使用的代码:

import pyautogui
import sys
from pynput.mouse import Controller
from sys import exit
from time import sleep
from msvcrt import getch
mouse = Controller()
move = int()
move = 0
collections = int()
collections = 0

input("Hover your mouse over FIRST BLOCK and press ENTER.")
b1 = mouse.position

input("Hover your mouse over SECOND BLOCK and press ENTER.")
b2 = mouse.position

input("Hover your mouse over PUNCH and press ENTER.")
p = mouse.position

input("Hover your mouse over COLLECT moving direction and press ENTER.")
c1 = mouse.position

input("Hover your mouse over RETURN moving direction and press ENTER.")
c2 = mouse.position


pt = float(input("Insert punch TIME (ftank 1.0; pepper 1.2; pball 2.6)"))




try:
    while True:
        pyautogui.click(b1)#Block 1
        pyautogui.dragTo(b2, duration = 0.25)#Block 2
        pyautogui.mouseDown(p)#Punch
        print("Punch Start.")
        sleep(pt)
        pyautogui.mouseUp()#Stop Punch
        move = move + 1 #Count loops
        print("Loop ", move, " Complete.")
        if move >= 150: #Gem Collection
            print("Collecting...")
            pyautogui.mouseDown(c1) #Collect Gems
            sleep(0.137)
            print("Finished.")
            pyautogui.mouseDown(c2) #Return to original position
            sleep(0.8)
            pyautogui.mouseUp()#Stop Moving
            move = 0 #Reset Counter
            collections  = collections + 1
            print("Finished ", collections,". Collecting Loop.")

except KeyboardInterrupt:
    sys.exit()

0 个答案:

没有答案