簡體   English   中英

如何使用python腳本pyhook使用快捷鍵退出?

[英]How to quit using python script pyhook use shortcut keys?

這是,我打算做一個可以自動粘貼到聊天窗口中並可以用鼠標單擊以發送小腳本的罐。

但是,該腳本只能在運行停止之前運行。

我的問題是如何設置快捷鍵(例如F12),可以按F12停止腳本。

腳本代碼如下:

# _*_ coding:UTF-8 _*_
import win32api
import win32con
import win32gui
from ctypes import *
import time
import msvcrt
import threading
from time import sleep

stop = 2
def mouse_click(x=None,y=None):
    if not x is None and not y is None:
        mouse_move(x,y)
        time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)     
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)   
def mouse_move(x,y):
    windll.user32.SetCursorPos(x, y)

def baozou():
    global stop
for event in range(1,50):
    mouse_click(1150,665)
    win32api.keybd_event(17,0,0,0)  
    win32api.keybd_event(86,0,0,0)  
    win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) 
    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0) 
    mouse_click(1272,669)
    time.sleep(0.1)
    if stop == 1:
        sys.exit()

def Break():
    global stop
    if ord(msvcrt.getch()) == 123:
        stop = 1

if __name__ == "__main__":
    t = threading.Thread(target = baozou)
    f = threading.Thread(target = Break)
    t.start()
    f.start()

請幫幫我!

我設法解決了這個問題,我的工作代碼是這樣的:

# _*_ coding:UTF-8 _*_
import win32api
import win32con
import win32gui
from ctypes import *
import time
import msvcrt
import threading
from time import sleep
import sys
import ctypes.wintypes


EXIT = False
def mouse_click(x=None,y=None):
    if not x is None and not y is None:
        mouse_move(x,y)
        time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)     
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)   
def mouse_move(x,y):
    windll.user32.SetCursorPos(x, y)

class Hotkey(threading.Thread):  #創建一個Thread.threading的擴展類  

    def run(self):  
        global EXIT  #定義全局變量,這個可以在不同線程見共用。  
        user32 = ctypes.windll.user32  #加載user32.dll  
        if not user32.RegisterHotKey(None, 99, win32con.MOD_ALT, win32con.VK_F3):   # 注冊快捷鍵 alt + f3 並判斷是否成功。  
            raise                                                                  # 返回一個錯誤信息  
    #以下為判斷快捷鍵沖突,釋放快捷鍵  
        try:  
            msg = ctypes.wintypes.MSG()  
            while user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:  
                if msg.message == win32con.WM_HOTKEY:  
                    if msg.wParam == 99:  
                        EXIT = True  
                        return  
                user32.TranslateMessage(ctypes.byref(msg))  
                user32.DispatchMessageA(ctypes.byref(msg))  
            finally:  
                user32.UnregisterHotKey(None, 1)  


if __name__ == "__main__":
    hotkey = Hotkey()  
    hotkey.start()  
    for event in range(1,30):
        mouse_click(1150,665)
        win32api.keybd_event(17,0,0,0)  
        win32api.keybd_event(86,0,0,0)  
        win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) 
        win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0) 
        mouse_click(1272,669)
        if EXIT:
            sys.exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM