Bottom Page

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
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Group files according to first few characters in filename python_newbie09 7 162 Aug-02-2019, 06:34 AM
Last Post: cvsae
  Hollow triangle-drawing characters param error. phob0s 4 118 Jul-31-2019, 08:18 AM
Last Post: phob0s
  Asyncio StreamReader read method doesn't respect timeout when using SSL dukadahake 0 86 Jul-24-2019, 11:55 AM
Last Post: dukadahake
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 354 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  Paste Special with multiple arguments with Dispatch CaptainCsaba 3 214 Jun-20-2019, 07:27 PM
Last Post: Larz60+
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 388 Jun-17-2019, 06:45 PM
Last Post: perfringo
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 249 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 229 May-24-2019, 07:04 PM
Last Post: Larz60+
  Python requests module doesn not return status_code palo173 5 288 May-21-2019, 09:22 AM
Last Post: buran
  o365 special subject mail download issue anna 3 228 May-16-2019, 07:16 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)