Python Forum
Pynput doesn't recognize shift button and special characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pynput doesn't recognize shift button and special characters
#1
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
Reply


Messages In This Thread
Pynput doesn't recognize shift button and special characters - by VirtualDreamer - Jul-17-2019, 11:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy xml content from webpage and save to locally without special characters Nik1811 14 596 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  Problem with "from pynput" LFreitas 1 129 Mar-24-2024, 02:23 AM
Last Post: deanhystad
  Pynput - One content on screen, another in the variable Valjean 4 882 Sep-18-2023, 06:00 PM
Last Post: Valjean
Question Special Characters read-write Prisonfeed 1 581 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  python cant recognize PIP siubikYT 2 708 Jul-19-2023, 06:30 PM
Last Post: Lahearle
  Find a shift between 2 pictures TazFleck 0 1,103 Jan-18-2023, 09:56 PM
Last Post: TazFleck
  Pynput Library not Working with Windows jacknewport 1 2,243 Mar-26-2022, 07:09 PM
Last Post: snippsat
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 1,095 Jan-07-2022, 06:18 PM
Last Post: deanhystad
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,232 Feb-16-2021, 09:49 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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