Hi everyone,
I am working on a project (just started) in PYTHON and I want to create a keylogger for 2 languages english and greek. For this I have created the below (it is under "construction") code.

from pynput.keyboard import Key, Listener
from langdetect import detect
from pynput import keyboard

def on_press(key):
    global string

    if key == keyboard.Key.esc: #if button escape is pressed close the program
        listener.stop()
    elif key == keyboard.Key.space:
        print(string)
        string=""
    else:
        string = ''.join([string,str(key).replace("'","")])

string=""
controller = keyboard.Controller()
# Collect events until released

listener = keyboard.Listener(on_press=on_press)
listener.start()

I run it and all good until I change the language. (please see the screenshots+explanations).

1.png

2.png

In the first picture I started typing in english and the printed result was in english but when I changed in greek the printed result remained english.
In the second picture I started in greek and the printed result was in greek, but when I change in english the result remains greek.

How can I solve this problem in order to take the same language result after an input language change?
Any help or advice would be great!
Thanks in advance.

This is strictly a guess. I'd want to see what langdetect is doing. Also read and try implementing the code on the URL to follow after the text:
"To enforce consistent results, call following code before the first language detection:"
https://pypi.org/project/langdetect/

Thanks for the feedback a lot.
I think that the problem is not in the langdetect and I think that there is a problem with the listener.
I tried stopping the active listener when language is changed and starting a new one but nothing got fixed.
I thought that starting a new listener would change the result and make it equivalent to the input languge ( :( didn;t work).

A few things.

  1. https://www.reddit.com/r/learnpython/comments/f2f78n/python_hook_pynput_restart_thread_after_specific/ seems to noted same issue. Doesn't seem to be just you.
  2. https://github.com/moses-palmer/pynput/issues looks very active. If you feel this is a bug, try there.

I take it you added the "following code" I linked to and it did nothing.

commented: Yes I posted this on Reddit but nobody helped me :( +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.