moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.77k stars 245 forks source link

AttributeError: 'Key' object has no attribute 'char' #494

Closed keisanng closed 2 years ago

keisanng commented 2 years ago

Description Anytime I press a key that pynput does not seem to like: ctrl, alt, win, tab, scrLk, shift etc. the listener breaks & returns error: AttributeError: 'Key' object has no attribute 'char'

Platform and pynput version Windows 10 Pro, pynput 1.7.6

To Reproduce pynput is located in a subdirectory & is handling the keyboard for a tkinter application, here is some of my code:

On Press:

def on_press(key):
  if key.char == (A1_bind):
    global active_A1
    if active_A1 == 0:
      mixer.Channel(0).play(mixer.Sound(A1_sound)) 
      A1.config(bg=pad_active, fg=pad_active)
      banklights.itemconfig(bank_a_light, fill=banklight_active)
      print("Pad A1 Triggered")
      active_A1 = 1]

On Release:

def on_release(key):
  if key.char == (A1_bind):
    global active_A1
    if active_A1 == 1:
      if pad_sustain == "False":
        mixer.Channel(0).stop()
        A1.config(bg=pad, fg=pad)
        banklights.itemconfig(bank_a_light, fill=banklight)
        active_A1 = 0
      else:
        A1.config(bg=pad, fg=pad)
        banklights.itemconfig(bank_a_light, fill=banklight)
        active_A1 = 0

Keyboard Listener:

listener = keyboard.Listener(
  on_press=on_press,
  on_release=on_release)
listener.start()
moses-palmer commented 2 years ago

Thank you for your report.

The key parameter passed to listeners is not necessarily a KeyCode instance---please consult the documentation. You cannot expect the argument to have the char attribute.