Related
Hi everyone,
I am working on an app and trying to create a corrector and I am using keyboard module for the correction.
I have created two classes, one keyboard monitor, which reads pressed events and displays them to screen, and one which suppresses them.
What I want to achieve is while the app corrects user input, every text that is typed by the user is suppressed and saved in a variable, for later use.
I am struggling on the synchronization of all this.
import keyboard
import threading
import time
lock_for_listening_to_keyboard = threading.Lock()
#########################################################
def delete_and_write(times_to_delete, word_to_write):
global lock_for_listening_to_keyboard
time.sleep(2)
print("OK")
# for i in range(50):
# keyboard.write('*')
# for i in range(times_to_delete+1):
# keyboard.press_and_release('backspace')
# for i,char in enumerate(word_to_write):
# keyboard.write(char.upper())
# for i in range(40):
# keyboard.write('*')
# keyboard.write(' ')
#########################################################
class keyboard_not_suppressed_monitor(threading.Thread):
def __init__(self, threadID, keyboardSupress):
threading.Thread.__init__(self)
self.threadID = threadID
self.fstring = ""
self.counter_for_key_presses = 0
self.suppressed = False
def run(self):
while(True):
event = keyboard.read_event(suppress=self.suppressed)
if (event.event_type == keyboard.KEY_DOWN):
# print("Key pressed = {} + suppress = {}".format(event.name, self.suppressed))
if (event.name == "space"):
suppressed_monitoring = keyboard_suppressed_monitor(2, self.fstring, self.counter_for_key_presses, None)
suppressed_monitoring.start()
suppressed_monitoring.join()
print("RETURNED TO MAIN MONITOR")
self.counter_for_key_presses = 0
self.fstring = ""
elif (event.name in "abcdefghijklmnopqrstuvwxyz"):
self.fstring = ''.join([self.fstring, event.name])
self.counter_for_key_presses += 1
class keyboard_suppressed_monitor(threading.Thread):
def __init__(self, threadID, fstring, counter_for_key_presses, keyboardSupress):
threading.Thread.__init__(self)
self.threadID = threadID
self.fstring = fstring
self.counter_for_key_presses = counter_for_key_presses
self.suppressed = True
self.done = False
self.temp = ""
def stop(self):
self._is_running = False
def run(self):
self.temp = self.fstring
self.fstring = ""
thread_delete_and_rewrite = threading.Thread(
target = delete_and_write, args=(self.counter_for_key_presses, self.temp))
thread_delete_and_rewrite.start()
# thread_delete_and_rewrite.join() # join is not here
print("RETURNED FROM THE REWRITER")
while(True):
print("STATE OF THREAD IS : {}".format(thread_delete_and_rewrite.is_alive())) print("IN THE WHILE TRUE")
event = keyboard.read_event(suppress=self.suppressed)
if (event.event_type == keyboard.KEY_DOWN):
print("KEYS PRESSED WHILE SUPPRESSED = {}".format(event.name))
if (event.name == "space"):
print("THE STRING ENTERED ")
self.temp = self.fstring
self.fstring = ""
thread_delete_and_rewrite = threading.Thread(
target = delete_and_write, args=(self.counter_for_key_presses, self.temp))
thread_delete_and_rewrite.start()
self.counter_for_key_presses = 0
self.fstring = ""
elif (event.name in "abcdefghijklmnopqrstuvwxyz"):
self.fstring = ''.join([self.fstring, event.name])
self.counter_for_key_presses += 1
# thread_delete_and_rewrite.join() # join is not here
print("SUPPRESSED ENDED")
self._is_running = False
if __name__ == "__main__":
kb = keyboard_not_suppressed_monitor(1, None)
kb.start()
kb.join()
I am trying to return the control when the function of correction is done, but can't make it work.
Any help is appreciated.
* UPDATE 1*
I made one class for all and works almost perfect.
I have one little problem that when the second while loop ends it requires a key press to exit. Is there a solution to this.
import keyboard
import threading
import time
lock_for_listening_to_keyboard = threading.Lock()
global running_suppressed_monitor
running_suppressed_monitor = False
#########################################################
def delete_and_write(times_to_delete, word_to_write):
global running_suppress_monitor
print("---Deleting & Rewrite Started---")
time.sleep(2)
running_suppressed_monitor = False
print("---Deleting & Rewrite Ended---")
# for i in range(times_to_delete+1):
# keyboard.press_and_release('backspace')
# for i,char in enumerate(word_to_write):
# keyboard.write(char.upper())
# keyboard.write(' ')
def write_the_suppressed_string(string):
keyboard.write(string)
#########################################################
class keyboard_monitor(threading.Thread):
def __init__(self,thread_name, threadID, word_typed, keyboard_suppress, counter_for_key_pressed):
threading.Thread.__init__(self)
self.name = thread_name
self.threaID = threadID
self.fstring = word_typed
self.counter_for_key_presses = counter_for_key_pressed
self.suppressed = keyboard_suppress
self.temp = ""
def stop(self):
self._is_running = False
def run(self):
if (self.suppressed is False):
while(True):
event = keyboard.read_event(suppress = self.suppressed)
if (event.event_type == keyboard.KEY_DOWN):
if (event.name == "space"):
suppressed_monitor = keyboard_monitor("suppressed_monitor", 2, self.fstring, True, self.counter_for_key_presses)
suppressed_monitor.start()
suppressed_monitor.join()
print("RETURNED TO MAIN MONITOR")
self.counter_for_key_presses = 0
self.fstring = ""
elif (event.name in "abcdefghijklmnopqrstuvwxyz"):
self.fstring = ''.join([self.fstring, event.name])
self.counter_for_key_presses += 1
elif (self.suppressed is True):
self.temp = self.fstring
self.fstring = ""
thread_delete_and_rewrite = threading.Thread(
target = delete_and_write, args=(self.counter_for_key_presses, self.temp))
thread_delete_and_rewrite.start()
running_suppressed_monitor = True
# while(thread_delete_and_rewrite.is_alive()):
while(running_suppressed_monitor):
event = keyboard.read_event(suppress=self.suppressed)
if (event.event_type == keyboard.KEY_DOWN):
print("KEYS PRESSED WHILE SUPPRESSED = {}".format(event.name))
if (event.name == "space"):
self.temp = self.fstring
self.fstring = ""
thread_delete_and_rewrite = threading.Thread(
target = delete_and_write, args=(self.counter_for_key_presses, self.temp))
thread_delete_and_rewrite.start()
# thread_delete_and_rewrite.join()
self.counter_for_key_presses = 0
self.fstring = ""
elif (event.name in "abcdefghijklmnopqrstuvwxyz"):
self.fstring = ''.join([self.fstring, event.name])
self.counter_for_key_presses += 1
# NO thread_delete_and_rewrite.join()
# NO thread_delete_and_rewrite.join()
if (thread_delete_and_rewrite.is_alive() is False):
break
thread_delete_and_rewrite.join()
print("SELF.FSTRING = {}".format(self.fstring))
print("BEFORE END OF SUPPRESSED MONITOR")
if (self.fstring != ""):
thread_write = threading.Thread(
target = write_the_suppressed_string, args=(self.fstring, ))
thread_write.start()
thread_write.join()
print("SUPPRESSED ENDED")
self._is_running = False
if __name__ == "__main__":
kb_not_suppressed = keyboard_monitor("not_suppressed_monitor", 1, "", False, 0)
kb_not_suppressed.start()
kb_not_suppressed.join()
So I made a little game that prints everything to the console which consists of some functions, a couple of lists and two extra text files with stats. The standalone game works fine, however, when I stick all the functions in a class and then call the class (so it works with the layout I made using Kivy), the error "name "getValue" is not defined" comes up.
I am confused because putting the functions into a class were the only change I have made. What did I miss?
I know that this would be very vague and unclear without the full code so here it is:
original working file
import time
import random
import os
import operator
import tkinter
import sys
homeIp = "1.1.1.1"
ipNum = homeIp
workingDir = "home/"
found = []
ports = {"ssh":[True,22],"sql":[True,1433],"http":[True,80],"ftp":[True,21]}
admin = True
def load(text,delay):
print(text, end="")
for x in range(0,delay-1):
time.sleep(0.5)
print(".", end="")
print(".")
def createIpList():
global found
for x in range(0,random.randint(1,3)):
found.append(str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255)))
def connect(ip):
global ipNum
load("trying to connect",8)
if ip in found:
print("connected to "+ip)
ipNum = ip
setPorts()
createIpList()
admin = False
else:
print("ip not found")
def scan():
load("scanning for devices",6)
for x in found:
print(x)
def setPorts():
ports["ssh"][0] = [random.choice([True,False])]
ports["sql"][0] = [random.choice([True,False])]
ports["http"][0] = [random.choice([True,False])]
ports["ftp"][0] = [random.choice([True,False])]
def probe():
print("SSH\t"+str(ports["ssh"][1])+"\t"+str(ports["ssh"][0]).strip("[").strip("]")+"\n"+
"SQL\t"+str(ports["sql"][1])+"\t"+str(ports["sql"][0]).strip("[").strip("]")+"\n"+
"HTTP\t"+str(ports["http"][1])+"\t"+str(ports["http"][0]).strip("[").strip("]")+"\n"+
"FTP\t"+str(ports["ftp"][1])+"\t"+str(ports["ftp"][0]).strip("[").strip("]")+"\n"+
"ALL PORTS MUST BE FALSE TO PORTHACK")
def shutDown():
load("Shutting down ",4)
quit()
def clearScreen():
os.system('cls')
def giveHelp():
print("COMMANDS :")
for key in commands0:
print(key)
for key in commands1:
print(key+" [ ]")
def openPort(portName,portNum):
if portNum == ports[portName][1]:
load("opening port "+str(portNum),10)
ports[portName][0] = False
print("port "+str(portNum)+" opened")
else:
print("Wrong port number")
def portHack():
shut = False
for port in ports:
if str(ports[port][0])== "[True]":
shut = True
if shut == True:
print("Can not operate port hack - all ports must be open")
else:
admin = True
print("You are now system admin")
def inbox():
file = open("emails.txt","r")
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email")))+"###":
ended = False
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email"))+1)+"###":
ended = True
if ended == False:
print(line)
file.close()
def editValue(var,val):
originalLines = []
file = open("saveData.txt","r")
for line in file:
if len(line)>1:
originalLines.append(line)
file.close()
for x in range(0,len(originalLines)):
if originalLines[x].split("=")[0].strip()==var:
newVal = (float(originalLines[x].split("=")[1].strip())+val)
originalLines[x] = var+"="+str(newVal)
file = open("saveData.txt","w")
for item in originalLines:
file.write(item+"\n")
file.close
def reply():
load("SENDING",3)
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def sysSpecs():
print("CPU\t\t"+str(getValue("cpu"))+"GHz\n"+
"RAM\t\t"+str(getValue("ram"))+"GB\n"+
"PSU\t\t"+str(getValue("psu"))+"W\n"+
"STORAGE\t\t"+str(getValue("storage"))+"GB")
if getValue("gpu") == 0:
print("GPU\t\tNONE")
else:
print("GPU\t\t"+str(getValue("gpu"))+"GHz")
def market():
category = input("WELCOME TO AMAZING\n\nCategories\n->CPU\n->RAM\n->storage\n->PSU\n->GPU\n\n[Press ENTER to leave]\n")
measurement={"CPU":"GHz","RAM":"GB","storage":"GB","PSU":"W","GPU":"GHz"}
sortedParts = (sorted(parts[category].items(), key=operator.itemgetter(1)))
print("ITEM\t\t\t|"+measurement[category]+"\t|PRICE\n==================================")
for item in sortedParts:
if len(item[0])>=8:
print(item[0]+"\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
else:
print(item[0]+"\t\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
def buy(item):
exist = False
itemCat = ""
for category in parts:
for part in parts[category]:
if part == item:
exist = True
itemCat = category
if exist == False:
print("ITEM NOT IN STOCK")
else:
if parts[itemCat][item][1] <= getValue("money"):
print("PURCHASED")
editValue(itemCat.lower(),float(parts[itemCat][item][0])-getValue(itemCat.lower()))
else:
print("INSUFFICIENT BALANCE")
def balance():
print(getValue("money"))
def eventCheck(command):
if int(getValue("email")) == 0:
inbox()
editValue("email",1)
inbox()
if command[0] == "reply":
if int(getValue("email")) == 1:
editValue("email",1)
inbox()
print("$100 recieved")
editValue("money",100)
elif int(getValue("email")) == 3:
if ipNum != homeIp:
if admin == True:
print("CONGRATS")
if int(getValue("email")) == 2:
if getValue("cpu")>=1.5:
editValue("email",1)
def main():
command = input(ipNum+":"+workingDir+">").split(' ')
try:
if len(command) == 1:
commands0[command[0]]()
elif len(command) == 2:
if command[0] == "sshCrack":
commands1[command[0]]("ssh",int(command[1]))
elif command[0] == "sqlInjection":
commands1[command[0]]("sql",int(command[1]))
elif command[0] == "webServerWorm":
commands1[command[0]]("http",int(command[1]))
elif command[0] == "ftpSpoof":
commands1[command[0]]("ftp",int(command[1]))
else:
commands1[command[0]](command[1])
except:
print("",end="")
eventCheck(command)
parts = {"CPU":{"LEG_1":[1.5,100],"LEG_2":[2,200],"AMP_Mk3":[2.5,300],"Inzel_Pontium":[3,500],"Inzel_Eye7":[3.5,700],"AMP_String_Cutter":[4,999]},
"RAM":{"InsaneX_2":[2,40],"Revenger_4":[4,60],"InsaneX_8":[8,100],"Korsair_16":[16,160],"Z.SKILL_FORK":[32,400]},
"storage":{"Sumsing_10":[10,20],"Queenston_50":[50,50],"EastDigital_100":[100,75],"SinDisk_250":[250,120],"CrazyX_500":[500,200],"Sumsing_TB":[1000,499]},
"PSU":{"Korsair_Feather":[200,50],"Korsair_Beefy":[500,130],"Korsair_Ultra":[800,200]},
"GPU":{"ZTX_1050":[2,100],"ZTX_1060":[4,200],"ZTX_1070":[8,400],"ZTX_1080":[16,800]}}
commands0 = {"scan":scan,"probe":probe,"shutdown":shutDown,"cls":clearScreen,"help":giveHelp,"portHack":portHack,"inbox":inbox,
"reply":reply,"market":market,"sysSpecs":sysSpecs,"balance":balance}
commands1 = {"connect":connect,"sshCrack":openPort,"sqlInjection":openPort,"webServerWorm":openPort,"ftpSpoof":openPort,"buy":buy}
setPorts()
createIpList()
eventCheck([""])
while True:
main()
file where the only change is that all functions are put into a class which is then called:
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color, Ellipse, Rectangle
from kivy.uix.widget import Widget
import time
import random
import os
import operator
import sys
homeIp = "1.1.1.1"
ipNum = homeIp
workingDir = "home/"
found = []
ports = {"ssh":[True,22],"sql":[True,1433],"http":[True,80],"ftp":[True,21]}
admin = True
class MainWindow(BoxLayout):
def load(text,delay):
print(text, end="")
for x in range(0,delay-1):
time.sleep(0.5)
print(".", end="")
print(".")
def createIpList():
global found
for x in range(0,random.randint(1,3)):
found.append(str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255)))
def connect(ip):
global ipNum
load("trying to connect",8)
if ip in found:
print("connected to "+ip)
ipNum = ip
setPorts()
createIpList()
admin = False
else:
print("ip not found")
def scan():
load("scanning for devices",6)
for x in found:
print(x)
def setPorts():
ports["ssh"][0] = [random.choice([True,False])]
ports["sql"][0] = [random.choice([True,False])]
ports["http"][0] = [random.choice([True,False])]
ports["ftp"][0] = [random.choice([True,False])]
def probe():
print("SSH\t"+str(ports["ssh"][1])+"\t"+str(ports["ssh"][0]).strip("[").strip("]")+"\n"+
"SQL\t"+str(ports["sql"][1])+"\t"+str(ports["sql"][0]).strip("[").strip("]")+"\n"+
"HTTP\t"+str(ports["http"][1])+"\t"+str(ports["http"][0]).strip("[").strip("]")+"\n"+
"FTP\t"+str(ports["ftp"][1])+"\t"+str(ports["ftp"][0]).strip("[").strip("]")+"\n"+
"ALL PORTS MUST BE FALSE TO PORTHACK")
def shutDown():
load("Shutting down ",4)
quit()
def clearScreen():
os.system('cls')
def giveHelp():
print("COMMANDS :")
for key in commands0:
print(key)
for key in commands1:
print(key+" [ ]")
def openPort(portName,portNum):
if portNum == ports[portName][1]:
load("opening port "+str(portNum),10)
ports[portName][0] = False
print("port "+str(portNum)+" opened")
else:
print("Wrong port number")
def portHack():
shut = False
for port in ports:
if str(ports[port][0])== "[True]":
shut = True
if shut == True:
print("Can not operate port hack - all ports must be open")
else:
admin = True
print("You are now system admin")
def inbox():
file = open("emails.txt","r")
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email")))+"###":
ended = False
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email"))+1)+"###":
ended = True
if ended == False:
print(line)
file.close()
def editValue(var,val):
originalLines = []
file = open("saveData.txt","r")
for line in file:
if len(line)>1:
originalLines.append(line)
file.close()
for x in range(0,len(originalLines)):
if originalLines[x].split("=")[0].strip()==var:
newVal = (float(originalLines[x].split("=")[1].strip())+val)
originalLines[x] = var+"="+str(newVal)
file = open("saveData.txt","w")
for item in originalLines:
file.write(item+"\n")
file.close
def reply():
load("SENDING",3)
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def sysSpecs():
print("CPU\t\t"+str(getValue("cpu"))+"GHz\n"+
"RAM\t\t"+str(getValue("ram"))+"GB\n"+
"PSU\t\t"+str(getValue("psu"))+"W\n"+
"STORAGE\t\t"+str(getValue("storage"))+"GB")
if getValue("gpu") == 0:
print("GPU\t\tNONE")
else:
print("GPU\t\t"+str(getValue("gpu"))+"GHz")
def market():
category = input("WELCOME TO AMAZING\n\nCategories\n->CPU\n->RAM\n->storage\n->PSU\n->GPU\n\n[Press ENTER to leave]\n")
measurement={"CPU":"GHz","RAM":"GB","storage":"GB","PSU":"W","GPU":"GHz"}
sortedParts = (sorted(parts[category].items(), key=operator.itemgetter(1)))
print("ITEM\t\t\t|"+measurement[category]+"\t|PRICE\n==================================")
for item in sortedParts:
if len(item[0])>=8:
print(item[0]+"\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
else:
print(item[0]+"\t\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
def buy(item):
exist = False
itemCat = ""
for category in parts:
for part in parts[category]:
if part == item:
exist = True
itemCat = category
if exist == False:
print("ITEM NOT IN STOCK")
else:
if parts[itemCat][item][1] <= getValue("money"):
print("PURCHASED")
editValue(itemCat.lower(),float(parts[itemCat][item][0])-getValue(itemCat.lower()))
else:
print("INSUFFICIENT BALANCE")
def balance():
print(getValue("money"))
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def eventCheck(command):
if int(getValue("email")) == 0:
inbox()
editValue("email",1)
inbox()
if command[0] == "reply":
if int(getValue("email")) == 1:
editValue("email",1)
inbox()
print("$100 recieved")
editValue("money",100)
elif int(getValue("email")) == 3:
if ipNum != homeIp:
if admin == True:
print("CONGRATS")
if int(getValue("email")) == 2:
if getValue("cpu")>=1.5:
editValue("email",1)
def main():
command = input(ipNum+":"+workingDir+">").split(' ')
try:
if len(command) == 1:
commands0[command[0]]()
elif len(command) == 2:
if command[0] == "sshCrack":
commands1[command[0]]("ssh",int(command[1]))
elif command[0] == "sqlInjection":
commands1[command[0]]("sql",int(command[1]))
elif command[0] == "webServerWorm":
commands1[command[0]]("http",int(command[1]))
elif command[0] == "ftpSpoof":
commands1[command[0]]("ftp",int(command[1]))
else:
commands1[command[0]](command[1])
except:
print("",end="")
eventCheck(command)
parts = {"CPU":{"LEG_1":[1.5,100],"LEG_2":[2,200],"AMP_Mk3":[2.5,300],"Inzel_Pontium":[3,500],"Inzel_Eye7":[3.5,700],"AMP_String_Cutter":[4,999]},
"RAM":{"InsaneX_2":[2,40],"Revenger_4":[4,60],"InsaneX_8":[8,100],"Korsair_16":[16,160],"Z.SKILL_FORK":[32,400]},
"storage":{"Sumsing_10":[10,20],"Queenston_50":[50,50],"EastDigital_100":[100,75],"SinDisk_250":[250,120],"CrazyX_500":[500,200],"Sumsing_TB":[1000,499]},
"PSU":{"Korsair_Feather":[200,50],"Korsair_Beefy":[500,130],"Korsair_Ultra":[800,200]},
"GPU":{"ZTX_1050":[2,100],"ZTX_1060":[4,200],"ZTX_1070":[8,400],"ZTX_1080":[16,800]}}
commands0 = {"scan":scan,"probe":probe,"shutdown":shutDown,"cls":clearScreen,"help":giveHelp,"portHack":portHack,"inbox":inbox,
"reply":reply,"market":market,"sysSpecs":sysSpecs,"balance":balance}
commands1 = {"connect":connect,"sshCrack":openPort,"sqlInjection":openPort,"webServerWorm":openPort,"ftpSpoof":openPort,"buy":buy}
setPorts()
createIpList()
eventCheck([""])
class MainApp(App):
def build(self):
Window.clearcolor = (1,1,1,1)
return MainWindow()
if __name__ == '__main__':
MainApp().run()
Just so you know, the whole point of putting it in a class is so I can use the layout I created using the kv design language in a different piece of code. If it is necessary, I can post my .kv file that is opened by the program.
My attempt is as below, it captures well when i'm typing, but sucks if multiple key pressed. It only capture the first pressed key.
I removed all unrelated to keylogging.
import win32console
import win32gui
import pythoncom, pyHook
import threading
import pickle
class keylogger(object):
def init(self, ifPrintDetail = False):
self.keylog_enable = False
self.ifPrintDetail = ifPrintDetail
self.log = ''
def KeyEvent(self, event):
if self.keylog_enable:
self.log += event.Key
if self.ifPrintDetail:
print ('MessageName:',event.MessageName )
print ('Message:',event.Message)
print ('Time:',event.Time)
print ('Window:',event.Window)
print ('WindowName:',event.WindowName)
print ('Ascii:', event.Ascii, chr(event.Ascii) )
print ('Key:', event.Key)
print ('KeyID:', event.KeyID)
print ('ScanCode:', event.ScanCode)
print ('Extended:', event.Extended)
print ('Injected:', event.Injected)
print ('Alt', event.Alt)
print ('Transition', event.Transition)
print ('---')
elif event.MessageName == 'key down':
print(event.Key, end='')
def threadkeylog(self):
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
# create a hook manager object for both key up and down
self.hm=pyHook.HookManager()
#i want both up and down key event
self.hm.KeyDown = self.KeyEvent
self.hm.KeyUp = self.KeyEvent
# set the hook
self.hm.HookKeyboard()
#start sending messages, this seems not to stop except WM_QUIT
pythoncom.PumpMessages()
def go(self):
#build and start a thread
self.keylog_enable = True
self.threadkl = threading.Thread(target = self.threadkeylog)
self.threadkl.start()
def pause(self):
self.keylog_enable = False
def save(self):
pickle.dump(self.log, open('keylog.txt', 'wb'))
You may use it with
kl = keylogger()
kl.init()
kl.go()#build and run a thread
kl.pause()#"pause" the thread
kl.save()#will save what you have typed to a file in working directory
print(pickle.load(open('keylog.txt', "rb")))#take a look at it
I do this to collect key log of myself playing car racing game, for training data of my machine learning project. So, say, if I control my racing car with simple "WASD" buttons, I would probably hold "W" and "A" to turn left and accelerate. So, I would like to have both of them captured at the same time, but those keys conflicts, and it capture only one character.
Using ctypes alone you can make quite an effective keylogger.
ctypes allows you to check if a key is currently pressed, or toggled.
If you set up a dictionary where all the keys are virtual keycodes and all of the values are the keycode's respective string character then you can iterate through the dictionary, and check if the key is pressed.
Having another dictionary called TrackedKeys which is empty, you can just set TrackedKeys[key] = The bool returned by the ctypes function which sees if a key is pressed when you are ticking in your "main loop" (A while true in the main python file).
then when you update the value of trackedkeys before doing so you can see if the value in trackedkey is different from the value returned by ctypes checking if a key is pressed, and if it is then call a function and pass either "key up" or "key down" to that function.
import threading, time
from ctypes import *
class Thread():
def __init__(self, addressOf, args):
self.terminate = False
self.Instance = threading.Thread(target=addressOf, args=args)
self.Instance.daemon = True
self.Instance.start()
VKStr = {}
VKStr[0x01] = "LEFT_MOUSEE"
VKStr[0x02] = "RIGHT_MOUSE"
VKStr[0x03] = "MIDDLE_MOUSE"
VKStr[0x08] = "BACKSPACE"
VKStr[0x09] = "TAB"
VKStr[0x0D] = "ENTER"
VKStr[0x10] = "SHIFT"
VKStr[0x11] = "CTRL"
VKStr[0x12] = "ALT"
VKStr[0x14] = "CAPSLOCK"
VKStr[0x18] = "ESCAPE"
VKStr[0x20] = " "
VKStr[0x25] = "LEFT_ARROW"
VKStr[0x26] = "UP_ARROW"
VKStr[0x27] = "RIGHT_ARROW"
VKStr[0x28] = "DOWN_ARROW"
VKStr[0x2C] = "PRINT_SCREEN"
VKStr[0x30] = "0"
VKStr[0x31] = "1"
VKStr[0x32] = "2"
VKStr[0x33] = "3"
VKStr[0x34] = "4"
VKStr[0x35] = "5"
VKStr[0x36] = "6"
VKStr[0x37] = "7"
VKStr[0x38] = "8"
VKStr[0x39] = "9"
VKStr[0x41] = "a"
VKStr[0x42] = "b"
VKStr[0x43] = "c"
VKStr[0x44] = "d"
VKStr[0x45] = "e"
VKStr[0x46] = "f"
VKStr[0x47] = "g"
VKStr[0x48] = "h"
VKStr[0x49] = "i"
VKStr[0x4A] = "j"
VKStr[0x4B] = "k"
VKStr[0x4C] = "l"
VKStr[0x4D] = "m"
VKStr[0x4E] = "n"
VKStr[0x4F] = "o"
VKStr[0x50] = "p"
VKStr[0x51] = "q"
VKStr[0x52] = "r"
VKStr[0x53] = "s"
VKStr[0x54] = "t"
VKStr[0x55] = "u"
VKStr[0x56] = "v"
VKStr[0x57] = "w"
VKStr[0x58] = "x"
VKStr[0x59] = "y"
VKStr[0x5A] = "z"
ShiftEquivs={}
ShiftEquivs[0x30] = ")"
ShiftEquivs[0x31] = "!"
ShiftEquivs[0x32] = "\""
ShiftEquivs[0x33] = "£"
ShiftEquivs[0x34] = "$"
ShiftEquivs[0x35] = "%"
ShiftEquivs[0x36] = "^"
ShiftEquivs[0x37] = "&"
ShiftEquivs[0x38] = "*"
ShiftEquivs[0x39] = "("
ActiveKeys = {}
def StringToVK(string):
for key, value in VKStr.items():
if value == string:
return key
def VKToString(VK):
return VKStr[VK]
def IsKeyPressed(VK_KEYCODE):
if type(VK_KEYCODE) == str:
try:
VK_KEYCODE = StringToVK(VK_KEYCODE)
except:
raise Exception("Exception caught in sub: 'IsKeyPressed' arg VK_KEYCODE is invalid")
return
return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x8000 != 0
def IsKeyToggled(VK_KEYCODE):
return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x0001 != 0
class KeyTracker:
def __init__(self):
self.tracking = False
self.tracked_string_concat = ""
self.file_open = False
def StartTracking(self):
self.tracking = True
def StopTracking(self):
self.tracking = False
self.CompileData()
def KeyDown(self, key):
if self.tracking and VKToString(key) != "SHIFT":
if IsKeyToggled(StringToVK("CAPSLOCK")):
self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper()
elif IsKeyPressed(StringToVK("SHIFT")):
shiftEquiv = False
try:
ShiftEquivs[key]
shiftEquiv = True
except:
pass
if shiftEquiv:
self.tracked_string_concat = self.tracked_string_concat + ShiftEquivs[key]
else:
self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper()
else:
self.tracked_string_concat = self.tracked_string_concat + VKToString(key)
def KeyUp(self, key):
if self.tracking and VKToString(key) == "SHIFT":
#self.tracked_string_concat = self.tracked_string_concat + VKToString(key)
pass
def UpdateKeyState(self, key, state):
def SetKeyState(key, state):
ActiveKeys[key] = state
if state == True:
self.KeyDown(key)
elif state == False:
self.KeyUp(key)
keyExists = False
try:
ActiveKeys[key]
keyExists = True
except:
pass
if keyExists:
if ActiveKeys[key] != state:
SetKeyState(key, state)
else:
SetKeyState(key, state)
def CompileData(self):
try:
file = open("logger_data.txt", "a")
file.write("\n")
file.write("-"*15)
file.write("\n")
file.write(self.tracked_string_concat)
file.close()
except:
pass
def TrackData(self, time_length): #timeLength in seconds
KeyTracker.StartTracking()
time.sleep(time_length)
KeyTracker.StopTracking()
KeyTracker = KeyTracker()
t = Thread(KeyTracker.TrackData, [5])
while True:
for key, key_name in VKStr.items():
KeyTracker.UpdateKeyState(key, IsKeyPressed(key))
change the argument passed to the thread stored in the variable t to change how long your keylogger records data for, 5 is just a short testing value.
I wrote a program which implements caesar-cipher with threads and queues with python. I would like to change all the threading work with multiprocessing in my code and I have no idea how to do it. I would appreciate if you could explain where & how to start the implementation. Here is the code:
import threading
import Queue
import sys
import string
lock = threading.Lock()
def do_work(in_queue, out_queue, shift):
while True:
lock.acquire()
item = in_queue.get()
result = caesar(item, shift)
out_queue.put(result)
in_queue.task_done()
lock.release()
def caesar(plaintext, shift):
plaintext = plaintext.upper()
alphabet = string.ascii_uppercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Duzgun giriniz: '<filename>.py s n l'")
sys.exit(0)
else:
s = int(sys.argv[1])
n = int(sys.argv[2])
l = int(sys.argv[3])
work = Queue.Queue()
results = Queue.Queue()
myfile=open('metin.txt','r')
text_data=myfile.read() # <=== here load file
index=0
for i in xrange(n):
t = threading.Thread(target=do_work, args=(work, results, s))
t.daemon = True
t.start()
for i in range(0, len(text_data), l):
work.put(text_data[index:index + l])
index += l
work.join()
index=0
output_file=open("crypted"+ "_"+ str(s)+"_"+str(n)+"_"+str(l)+".txt", "w")
for i in range(0, len(text_data), l):
output_file.write(results.get())
index += l
sys.exit()
You can save yourself some code and move to the standard multiprocessing.Pool implementation.
import multiprocessing
import sys
import string
import itertools
# for non-forking systems like Windows
def worker(args):
# args: (text, shift)
return caesar(*args)
# for forking systems like linux
def forking_worker(args):
# args: ((start_index, end_index), shift)
return caesar(text_data[args[0][0]:args[0][1], args[1])
def caesar(plaintext, shift):
plaintext = plaintext.upper()
alphabet = string.ascii_uppercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Duzgun giriniz: '<filename>.py s n l'")
sys.exit(0)
else:
s = int(sys.argv[1])
n = int(sys.argv[2])
l = int(sys.argv[3])
pool = multiprocessing.Pool() # todo: change number of cpus...
with open('metin.txt') as myfile:
text_data=myfile.read() # <=== here load file
# on a forking system so only pass index, not text to child
result = pool.map(forking_worker,
zip(((index, index + l)
for index in range(0, len(text_data), l)),
itertools.cycle([s])))
with open("crypted"+ "_"+ str(s)+"_"+str(n)+"_"+str(l)+".txt", "w") as output_file:
output_file.writelines(result)
I have written the code below to handle and collect perfomance counters (including the test lines (at the end of the code)).
However, when I call the function getAndReset(), it seems to reset the values to Zero before outputting the current value of lendingStats dictionary. This is despite the fact that within the function getAndReset() , the lendingStats value is updated first then stored in a variable theStats.
#!/usr/bin/python
import threading
import time, datetime,pytz
startTime = datetime.datetime.now(pytz.timezone('Africa/Nairobi')).strftime('%Y-%m-%d %H:%M:%S%z')
lendingStats = {'ussdRequests':{'getSubInfo':None,'loanRequests':None},'time':{'startTime':startTime,'currentTime':startTime,'last-reset-time:':startTime}}
lendingStatsNow = {'ussdRequests':{'getSubInfo':None,'loanRequests':None},'time':{'startTime':startTime,'currentTime':startTime,'last-reset-time:':startTime}}
subInfoCounter = dict()
loanRequestCounter = dict()
currentTime = startTime
lastResetTime = startTime
def synchronized(func):
func.__lock__ = threading.RLock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
#synchronized
def lastResetTimeFunc(action):
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
if action == "Get":
return lastResetTime
elif action == "Update":
lastResetTime = datetime.datetime.now(pytz.timezone('Africa/Nairobi')).strftime('%Y-%m-%d %H:%M:%S%z')
##synchronized
def getAndReset():
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
# Get the values to return
theStats = dict()
theStats = getLendingStats()
resetStats()
return theStats
#synchronized
def resetStats():
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
ussdSubInfoStatsFunc(action="Reset")
ussdLoanReqStatsFunc(action="Reset")
lastResetTimeFunc("Update")
#synchronized
def getLendingStats():
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
# Get the values
# lendingStats['ussdRequests']['getSubInfo'] = ussdSubInfoStatsFunc(action="Get")
# lendingStats['ussdRequests']['loanRequests'] = ussdLoanReqStatsFunc(action="Get")
# lendingStats['time']['last-reset-time'] = lastResetTimeFunc("Get")
# lendingStats['time']['currentTime'] = getCurrentTimeFunc()
returnValue = dict()
for KeyName in lendingStats.iterkeys():
returnValue[KeyName] = dict()
returnValue['ussdRequests']['getSubInfo'] = ussdSubInfoStatsFunc(action="Get")
returnValue['ussdRequests']['loanRequests'] = ussdLoanReqStatsFunc(action="Get")
returnValue['time']['last-reset-time'] = lastResetTimeFunc("Get")
returnValue['time']['currentTime'] = getCurrentTimeFunc()
returnValue['time']['startTime'] = lendingStats['time']['startTime']
lendingStats = returnValue
# Return the Values
return returnValue
#synchronized
def getCurrentTimeFunc():
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
currentTime = datetime.datetime.now(pytz.timezone('Africa/Nairobi')).strftime('%Y-%m-%d %H:%M:%S%z')
theTime = currentTime
return theTime
#synchronized
def ussdLoanReqStatsFunc(**kwargs):
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
if kwargs['action'] == "Add":
if loanRequestCounter.has_key(kwargs['respcode']):
if loanRequestCounter[kwargs['respcode']].has_key(kwargs['denom']):
currentcount = loanRequestCounter[kwargs['respcode']][kwargs['denom']]
loanRequestCounter[kwargs['respcode']][kwargs['denom']] = currentcount + 1
else:
loanRequestCounter[kwargs['respcode']][kwargs['denom']] = 1
else:
loanRequestCounter[kwargs['respcode']] = dict()
loanRequestCounter[kwargs['respcode']][kwargs['denom']] = 1
elif kwargs['action'] == "Reset":
for respCodeKeyName in loanRequestCounter.iterkeys():
for denomKeyName in loanRequestCounter[respCodeKeyName].iterkeys():
loanRequestCounter[respCodeKeyName][denomKeyName] = 0
elif kwargs['action'] == "Get":
theDict = loanRequestCounter
return theDict
#synchronized
def ussdSubInfoStatsFunc(**kwargs):
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
if kwargs['action'] == "Add":
if subInfoCounter.has_key(kwargs['respcode']):
currentcount = subInfoCounter[kwargs['respcode']]
subInfoCounter[kwargs['respcode']] = currentcount + 1
else:
subInfoCounter[kwargs['respcode']] = 1
elif kwargs['action'] == "Reset":
for keyname in subInfoCounter.iterkeys():
subInfoCounter[keyname] = 0
elif kwargs['action'] == "Get":
theDict = subInfoCounter
return theDict
def testSubInfoCounter(KeyName,numOfEvents):
i = 0
while i < numOfEvents:
ussdSubInfoStatsFunc(action="Add",respcode=KeyName)
i +=1
print ussdSubInfoStatsFunc(action="Get")
def testLoanReqCounter(respCodeT,theDenom,numOfEvents):
i = 0
while i < numOfEvents:
ussdLoanReqStatsFunc(action="Add",respcode=respCodeT,denom=theDenom)
i +=1
print ussdLoanReqStatsFunc(action="Get")
thread1 = threading.Thread(target = testLoanReqCounter("0","200",7767))
thread2 = threading.Thread(target = testLoanReqCounter("1","1000",55))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print "At the beginning:"
print getLendingStats()
print getAndReset()
testSubInfoCounter("9",7)
testSubInfoCounter("0",7000)
testLoanReqCounter("0","200",7767)
testLoanReqCounter("1","200",33)
testLoanReqCounter("0","1000",3928)
testLoanReqCounter("1","1000",55)
testLoanReqCounter("0","5000",492)
testSubInfoCounter("1",3000)
testSubInfoCounter("3",1000)
testSubInfoCounter("0",7000)
print "Cumulatively:"
print getAndReset()
# print getLendingStats()
# resetStats()
time.sleep(3)
print "After the Reset"
# print getLendingStats()
# resetStats()
print getAndReset()
testLoanReqCounter("0","200",7767)
testLoanReqCounter("1","200",33)
testLoanReqCounter("0","1000",3928)
testLoanReqCounter("1","1000",55)
testLoanReqCounter("0","5000",492)
print getLendingStats()
resetStats()
testSubInfoCounter("1",3230)
testSubInfoCounter("3",1901)
testSubInfoCounter("0",76887)
print getAndReset()
time.sleep(4)
print getAndReset()
# print getLendingStats()
# resetStats()
The problem here is that I needed to use a deepcopy as opposed to a simple assignment (which uses a simple copy).
Therefore the code that I needed to change was:
import copy
#synchronized
def getAndReset():
global lendingStats,subInfoCounter,loanRequestCounter,currentTime,lastResetTime
# Get the values to return
theStats = copy.deepcopy(getLendingStats())
resetStats()
return theStats