Bottom Page

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

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
Larz60+ wrote Jul-17-2019, 09:10 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Remove escape characters / Unicode characters from string DreamingInsanity 5 386 May-15-2020, 01:37 PM
Last Post: snippsat
  Solve Pynput Problem when changing language? ppel123 0 328 Feb-19-2020, 03:38 PM
Last Post: ppel123
  Check for a special characters in a column and flag it ayomayam 0 198 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  import pynput Sherlock42 2 429 Feb-08-2020, 04:01 PM
Last Post: Sherlock42
  Player object wont recognize collision with other objects. Jan_97 3 339 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  How to recognize space or enter as one-character input? Mark17 5 390 Oct-17-2019, 08:19 PM
Last Post: jefsummers
  problems with python script and special characters last08 1 696 Mar-29-2019, 09:28 AM
Last Post: Kebap
  How to recognize, what functions are filled in of the Python interpreter stack? AlekseyPython 3 624 Mar-13-2019, 12:14 PM
Last Post: AlekseyPython
  Variable defined but python wont recognize it. FWendeburg 3 812 Feb-19-2019, 10:43 PM
Last Post: woooee
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 489 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython

Forum Jump:

Users browsing this thread: 1 Guest(s)