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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pynput Library not Working with Windows jacknewport 1 923 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 650 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 568 Jan-07-2022, 06:18 PM
Last Post: deanhystad
  Rename Multiple files in directory to remove special characters nyawadasi 9 3,797 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
Star NameError – function doesn't recognize imported modules Sir 4 2,221 Dec-01-2020, 06:36 AM
Last Post: Sir
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 3,906 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,051 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Writing a basic shift code djwilson0495 2 1,513 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  Remove escape characters / Unicode characters from string DreamingInsanity 5 8,518 May-15-2020, 01:37 PM
Last Post: snippsat
  Solve Pynput Problem when changing language? ppel123 0 1,787 Feb-19-2020, 03:38 PM
Last Post: ppel123

Forum Jump:

User Panel Messages

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