Running Python clicker based on keylogger inputs on the background in Windows -
i wanted write program in python windows act clicker, in according key user presses click made @ known location on screen. used automated option selection list in webpage. have clicking part working, wanted able make several clicks during execution, if there quiz multiple lists 1 after another.
one option make while loop getch()
msvcrt
. thing after click outside cmd window no longer selected, rather window destination point located. therefore, script stops being active , user cannot choose location. workaround click cmd window return focus , able more clicks. solve this, necessary create service or, according @sanju, thread.
the other option use keylogger such pyhook, seems way go. however, problem window want use in, webpage in flash or animations engine, causes error users have found using keylogger example in skype , being described here. in case, happens webpage , either when click made on window or when key pressed window selected.
my base code presented below, click(...)
contain coordinates argument being omitted simplicity. in case, 0 ends program , there 3 options being chosen numbers 1-3.
import msvcrt, win32api, win32con def click(x,y): win32api.setcursorpos((x,y)) win32api.mouse_event(win32con.mouseeventf_leftdown,x,y,0,0) win32api.mouse_event(win32con.mouseeventf_leftup,x,y,0,0) key=0 while key!=b'0': key=msvcrt.getch() if key==b'1': click(...) elif key==b'2': click(...) elif key==b'3': click(...)
the attempts below try implement @sanju's suggestion, first whole while inside thread , queue
, both not working expected...
import threading, msvcrt, win32api, win32con def mythread(): key=0 while key!=b'0': key=msvcrt.getch() if key==b'1': ... def click(x,y): ... threading.thread(target=mythread,args=[]).start()
.
import queue, threading, msvcrt, win32api, win32con def mythread(key): while key.get()!=b'0': key.put(msvcrt.getch()) if key.get()==b'1': ... def click(x,y): ... key=queue.queue() key.put(0) threading.thread(target=mythread,args=[key]).start()
the other attempt uses pyhook, it's still facing aforementioned issue.
import pyhook, pythoncom, win32api, win32con def onkeyboardevent(event): if event.key=='numpad1': ... def click(x,y): ... hm=pyhook.hookmanager() hm.keydown=onkeyboardevent hm.hookkeyboard() pythoncom.pumpmessages()
all need here move click part thread , share user input using shareble object such queue. sounds overkill , that's way keep tasks in background.
and btw, have many gui application frameworks available in python tkinter ,wxpython can ease objective.
Comments
Post a Comment