Created
May 7, 2015 01:58
-
-
Save icarofreire/d743209a80c303e45966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: latin1 -*- | |
try: | |
import Xlib | |
from Xlib import X, display | |
from Xlib.ext.xtest import fake_input | |
except ImportError: | |
print 'Instalar a biblioteca python-xlib' | |
sys.exit(1) | |
import time | |
import os | |
import sys | |
import os.path | |
class robo_mouse: | |
d, s, root = None, None, None | |
def __init__(self): | |
self.d = display.Display() | |
self.s = self.d.screen() | |
self.root = self.s.root | |
# move o cursor do mouse para a coordenada x, y; | |
def mover_mouse(self, x, y): | |
self.root.warp_pointer(x, y) | |
self.d.sync() | |
# efetua um click do mouse; | |
def clicar(self): | |
fake_input(self.d, X.ButtonPress, 1) | |
self.d.sync() | |
fake_input(self.d, X.ButtonRelease, 1) | |
self.d.sync() | |
# clica com o botao direito do mouse; | |
def click_botao_direito(self): | |
fake_input(self.d, X.ButtonPress, 3) | |
self.d.sync() | |
fake_input(self.d, X.ButtonRelease, 3) | |
self.d.sync() | |
# move o cursor do mouse e efetua um click; | |
def mover_clicar(self, x, y): | |
mover_mouse(x, y) | |
clicar() | |
# esperar n segundos; | |
def esperar_s(self, segundos): | |
time.sleep(segundos) | |
# esperar n minutos; | |
def esperar_m(self, minutos): | |
time.sleep(minutos*60) | |
class robo_key: | |
display, shifted = None, None | |
def __init__(self): | |
self.display = Xlib.display.Display() | |
self.shifted = ('~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '{', '}', '?', '+', '"', '<', '>', ':') | |
# converter uma string para seus keycodes; | |
def string2codes(self, string): | |
'''convert a string to a series of keycodes''' | |
codes = [] | |
for letter in string: | |
if letter.isupper() or letter in self.shifted: | |
# hack! < doesn't print properly | |
key = self.display.keysym_to_keycode(ord(letter)) | |
if key == 94: key = 25 | |
# tuple indicates key with modifier pressed | |
codes.append((50,key)) | |
else: | |
# int means regular keycode | |
codes.append(self.display.keysym_to_keycode(ord(letter))) | |
if codes[-1] == 0: | |
# bad hack for now, make a newline | |
codes[-1] = 36 # return key | |
return codes[:-1] | |
def escape_lines(self, text): | |
# precisa escapar todos os metacaracteres | |
text = text.replace('"','\\\"') | |
text = os.popen('/bin/echo -e "%s"' % text).read() | |
return text | |
def escrever_texto(self, text): | |
new_text = self.escape_lines(text) | |
# se a expansão shell não produziu nada, não envia nada | |
if new_text.isspace() and not text.isspace(): return | |
for char in self.string2codes(new_text): | |
self.send_key(char) | |
def send_key(self, keycode): | |
'''Send a KeyPress and KeyRelease event''' | |
if type(keycode) == tuple: | |
# send with modifier | |
fake_input(self.display, X.KeyPress, keycode[0]) | |
fake_input(self.display, X.KeyPress, keycode[1]) | |
self.display.sync() | |
time.sleep(0.01) | |
fake_input(self.display, X.KeyRelease, keycode[1]) | |
fake_input(self.display, X.KeyRelease, keycode[0]) | |
self.display.sync() | |
else: | |
# send without modifier | |
fake_input(self.display, X.KeyPress, keycode) | |
self.display.sync() | |
time.sleep(0.01) | |
fake_input(self.display, X.KeyRelease, keycode) | |
self.display.sync() | |
class log: | |
arquivo, file = None, None | |
# Será criado um arquivo caso ele não exista; | |
# Será escrito nele caso o arquivo já exista; | |
def __init__(self, arquivo): | |
self.arquivo = arquivo | |
if os.path.isfile(arquivo) == False: | |
self.file = open(arquivo, "w") | |
else: self.file = open(arquivo, "a+") | |
def escrever(self, texto): | |
self.file.write(texto+"\n") | |
self.file.close() | |
#-----------------------------------------------------------------------# | |
""" | |
Exemplo de uso key: | |
kr = robo_key() | |
kr.escrever_texto("Meu E-mail: [email protected]<Python> ?! ^^Xip;)") | |
# Exemplo de uso mouse: | |
r = robo_mouse() | |
r.esperar_s(2)# 2 segundos | |
r.mover_mouse(1328, 613) | |
r.clicar() | |
r.clicar() | |
# exemplo de uso log: | |
l = log("teste") | |
l.escrever("teste 1234 7567 [3]") | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment