Il mio primo articolo si concentrerà sulla creazione di un semplice script in Python, avviabile solo con la diretta esecuzione (doppio click sul file .py), utile per cliccare ripetutamente su uno o più determinati punti dello schermo, sulle finestre, bottoni, banner, etc. Una particolarità di questo script è quella di “sentire” gli input provenienti dalla tastiera anche se la finestra non è “a fuoco” cioè anche se non è l’attuale finestra in primo piano e attiva.
In breve, vengono creati due thread che comunicano mediante un buffer condiviso (come nel classico problema produttore/consumatore):
- t_keyboard: si occupa di ascoltare gli eventi della tastiera (i tasti premuti). È il produttore e inserisce i caratteri letti nel buffer
- t_mouse: gestisce il mouse, catturandone la posizione, spostandolo e facendolo cliccare. Si tratta del consumatore.

Le librerie/moduli accessorie sono:
- PyWin32
- PyHook
- SendKeys


"""
Project Name:....Aut0 P0int & Click v1.0
Author:..........Mauro "Baba" Mascia
Date:............2009/02
Thanks to:
  - Peter Parente
  - "BlueKitties" for the article "Mouse/Keyboard control scripting module."
  - prof. Matteo Pradella

"""
from ctypes import*
from SendKeys import*
from pyHook import *
from time import strftime
from time import sleep
from threading import Thread
from threading import Lock
import pythoncom
import sys

"""
    Create a thread that handle the keyboard
    All key pressed are stored in a shared buffer
"""
class t_keyboard(Thread):
    def __init__(self, buff):
        Thread.__init__(self)
        self.buff = buff

    def run(self):
        def OnKeyboardEvent(event):
            char = chr(event.Ascii)

            if char in ['r', 'd', 'q']:
                if not self.buff.put(char):
                    print "full buffer"

            if event.Key == 'Escape':
                sys.exit()

            return True

        hm = HookManager()
        hm.KeyDown = OnKeyboardEvent
        hm.HookKeyboard()   # set the hook

        # Wait forever and print keys
        pythoncom.PumpMessages()

"""
    Create a thread that handle the mouse
"""
class t_mouse(Thread):
    def __init__(self, buff):
        Thread.__init__(self)
        self.buff = buff

    def run(self):
        class POINT(Structure):
            _fields_ = [("x", c_ulong),("y", c_ulong)]

        #Gets the current mouse position
        def getpos():
            global pt
            pt = POINT()
            windll.user32.GetCursorPos(byref(pt))
            return pt.x, pt.y

        #Calculate how many minutes are now
        def minNow():
            h_now = int(strftime("%H")) #hours
            m_now = int(strftime("%M")) #mins
            t_now = h_now * 60 + m_now  #time in mins
            return t_now

        #Moves the cursor to x,y cordinate
        def move(x,y):
            windll.user32.SetCursorPos(x,y)

        #Clicks in the actual mouse position
        def click():
            LEFTDOWN   = 0x00000002
            LEFTUP     = 0x00000004
            windll.user32.mouse_event(LEFTDOWN,0,0,0,0)
            windll.user32.mouse_event(LEFTUP,0,0,0,0)

        print "Punta e clicca fino alle:"
        while True:
            h_end = int(raw_input("ora:"))
            if(h_end <= 24 and h_end > 0):
                break
        while True:
            m_end = int(raw_input("min:"))
            if(m_end <= 60 and m_end >= 0):
                break

        t_end = h_end*60+m_end

        print "\n------\nPremi:\n"
        print "- \'r\' per impostare le coordinare del mouse"
        print "- \'d\' per cominciare la riproduzione"
        print "- \'q\' per arrestare la riproduzione"
        print "- Esc per uscire"
        print "\n------\n"

        coords = []
        while True:
            char = self.buff.get() #reads from buffer

            if char == 'r':
                x, y = getpos()  #gets current position
                print "Catturata coordinata X:%d,Y:%d" %(x,y)
                coords.append((x, y))
            elif char == 'd' and len(coords)>0:
                print "Riproduzione..."
                while minNow() < t_end:
                    for x,y in coords:
                        move(x,y)
                        click()
                        sleep(0.08)
                    if self.buff.get() == 'q':
                        print "stopped"
                        break
                break
            else:
                """
                    sleep for a while if char isnt "r" or "d"
                    this is foundamental to release CPU's resources
                """
                sleep(0.001)

class Buffer(object):
    def __init__(self, size) :
        self.data = [None for x in xrange(size)]
        self.size = size
        self.head = self.tail = 0
        self.the_lock = Lock()

    def put(self, x):
        self.the_lock.acquire()
        if self.data[self.tail] != None :
            self.the_lock.release()
            return False # full
        self.data[self.tail] = x
        self.tail = (self.tail+1) % self.size
        self.the_lock.release()
        return True

    def get(self) :
        self.the_lock.acquire()
        if self.data[self.head] == None :
            self.the_lock.release()
            return None # empty
        v = self.data[self.head]
        self.data[self.head] = None
        self.head = (self.head+1) % self.size
        self.the_lock.release()
        return v

print "Aut0 P0int & Click v1.0\n"
buf = Buffer(100)
keyboard = t_keyboard(buf)
mouse = t_mouse(buf)
keyboard.start()
mouse.start()

One Response to “Auto Punta e Clicca in Python”

  • Anonymous:

    yeah grande Baba, ora non resta che insegnargli quando si apre un popup malefico, così ce lo chiude al volo :) dave.

Leave a Reply

Stats
Categories