0

I am trying to use the restriction of if(key == Key.left or key == Key.up): to log into log.txt only the keys Key.up and Key.left. I wanted to add a dictionary so that Key.up is U and Key.left is L. Since then I have a error: when I use keylogger.py it says File "keylogger.py", line 27, in writeLog keydata = keydata.replace("'", "") UnboundLocalError: local variable 'keydata' referenced before assignment. And when I use keylogger2.py if(key == Key.left or key == Key.up): it doesn't cause restriction that I wanted.

keylogger.py:

from pynput.keyboard import Listener, Key

#setting the log file location
logFile = "/home/diego/log.txt"

def writeLog(key):
    '' '
     This function will be responsible for receiving the key pressed.
     via Listener and write to log file
     '' '

    # dictionary with keys to translate
    translate_keys = {
        "Key.up": "U",
        "Key.left": "L",
    }
    if(key == Key.left or key == Key.up):
#convert the keystroke to string
      keydata = str(key)

#removing single quotes delimiting characters
    keydata = keydata.replace("'", "")

    for key in translate_keys:
         #key gets dictionary key translate_keys
         #replace key with its value (translate_keys [key])

        keydata = keydata.replace(key, translate_keys[key])

    #open the log file in append mode
    with open(logFile, "a") as f:
        f.write(keydata)

#open the Keyboard Listener and listen for the on_press event
#when the on_press event occurs, call the writeLog function
with Listener(on_press=writeLog) as l:
    l.join()

keylogger2.py modified with dictionary inserted


from pynput.keyboard import Listener, Key


logFile = "/home/diego/log.txt"

def writeLog(key):
    translate_keys = {
        "Key.up": "U",
        "Key.left": "L",
    }
    if(key == Key.left or key == Key.up):

      keydata = str(key)

    keydata = keydata.replace("'", "")

    for key in translate_keys:
        keydata = keydata.replace(key, translate_keys[key])

    with open(logFile, "a") as f:
        f.write(keydata)

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

How can I make the restriction if(key == Key.left or key == Key.up): work in conjunction with the dictionary translate_keys = {"Key.up": "U","Key.left": "L",} ?

7beggars_nnnnm
  • 697
  • 3
  • 12
  • 1
    do you mean instead of using `Key.left` you want to check against your dictionary? – gold_cy Jan 02 '20 at 18:59
  • @aws_apprentice Thanks! I want to register only the keys Key.left and Key.up, how much has been resolved in https://stackoverflow.com/questions/59568552/make-specific-key-method-constraint-on-pynput-listener-work-in-conjunction-with?. But now instead of registering in my log.txt `Key.left` or `Key.up` I want to register `L` and `U` respectively, so for that I am using the dictionary. – 7beggars_nnnnm Jan 02 '20 at 19:02
  • 1
    sorry I don't quite understand you, you want to write the `L` and `U` to the log file instead of all that extra logic you have where you trim the quotes and other stuff? – gold_cy Jan 02 '20 at 19:05
  • @aws_apprentice Initially I want to register the keypress only and only of the `Key.up` and `Key.left` keys but I also want to convert their name. Then inside my log.txt `Key.up` will be registered as `U` and 'Key.left` will be registered as `L`. – 7beggars_nnnnm Jan 02 '20 at 19:07

2 Answers2

1

You can remove the quotes and compare the objects directly as shown below

def writeLog(key):
    translate_keys = {
        Key.up: "U",
        Key.left: "L",
    }
    if key in translate_keys:
        with open(logFile, "a") as f:
            f.write(translate_keys[key])
gold_cy
  • 13,648
  • 3
  • 23
  • 45
1

You can just do this:

translate_keys = {
    str(Key.up): "U",
    str(Key.left): "L"
}
if(key == Key.left or key == Key.up):
    keydata = translate_keys[str(key)]

    #open the log file in append mode
    with open(logFile, "a") as f:
        f.write(keydata)

marcos
  • 4,473
  • 1
  • 10
  • 24
  • 1
    you can just compare equality between the objects, there is no need to convert to strings, furthermore since we are checking presence in the dictionary we don't need an _explicit_ or condition – gold_cy Jan 02 '20 at 19:14
  • 1
    The problem is that i'm not familiar with `Key` object, so that's why i said convert it to `str` so it can be hashable, so thanks. @aws_apprentice – marcos Jan 02 '20 at 19:16
  • @Marcos ThankX your answer also worked correctly. I will treat the answer of the aws_apprentice as having eliminated unnecessary code. – 7beggars_nnnnm Jan 02 '20 at 19:27
  • 1
    @DiegoBneiNoah thanks, his answer is better. Good luck. – marcos Jan 02 '20 at 19:29