I am trying to import a keyboard listener into my class but keep getting a
NameError: name 'on_press' is not defined
Here is my code:
from pynput import keyboard
class game_code:
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
def check_key_press(self,key):
try: k = key.char
except: k = key.name
if k in ['up', 'down', 'left', 'right']:
self.key = keys.append(k)
return True
else:
return False
Also not 100% sure on how with statements work.
I got it working using the format of their documentation online:
https://pythonhosted.org/pynput/keyboard.html
from pynput.keyboard import Key, Listener
def on_press(key):
#print('{0} pressed'.format(
#key))
check_key(key)
def on_release(key):
#print('{0} release'.format(
# key))
if key == Key.esc:
# Stop listener
return False
def check_key(key):
if key in [Key.up, Key.down, Key.left, Key.right]:
print('YES')
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Related
I was working on a random python script but I've encountered the above error and cannot figure out how to work it out.
Code:
from pynput.keyboard import Key, Controller, Listener
import time
keyboard = Controller
key = "e"
toggle = Key.f6
toggled = False
def on_press(key):
print(f"on_press() triggered, key = {key}")
if key == toggle:
toggled = not toggled
def on_release(key):
print(f"on_release() triggered, key = {key}")
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
while True:
if toggled:
keyboard.press(key)
time.sleep(3)
keyboard.release(key)
time.sleep(1)
I am trying to hold down the alt key when I press it with pynput. I checked the documentation and could not find a list saying how to call all specific keys or detect all specific keys. So that sucks.
I've used their cookie-cutter code for detecting presses to try and detect keys:
from pynput.keyboard import Key, Listener
def on_press(key):
print('{0} pressed'.format(
key))
def on_release(key):
print('{0} release'.format(
key))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
but when I try to modify it like this...
from pynput import keyboard
def on_press(key):
try:
print('Alphanumeric key pressed: {0} '.format(
key.char))
except AttributeError:
print('special key pressed: {0}'.format(
key))
def on_release(key):
print('Key released: {0}'.format(
key))
if key == keyboard.Key.alt_1: <------ this
print('test')
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
It just quits when I attempt to press alt or any key I select for the if statement.
Ideally, I'd like to do this:
from pynput import keyboard
def on_release(key):
if key == keyboard.Key.alt_1:
keyboard.press.alt_1
# Collect events until released
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
but obviously, I can't even reach this point. Help?
I don't understand why you use alt_1 instead of alt.
And if you want to send some key then you have to use Controller()
This code works for me
from pynput.keyboard import Key, Listener, Controller
def on_press(key):
print(f'{key} pressed')
def on_release(key):
print(f'{key} released')
if key == Key.alt:
print('>>> I press ALT <<<')
controller.press(Key.alt)
if key == Key.esc:
# Stop listener
return False
#--- main ---
controller = Controller()
# Collect events until released
with Listener(on_press=on_press,on_release=on_release) as listener:
# ... other code ...
listener.join()
I'm trying to replace volume media keys with media next-prev keys. The problem is the key volume down or up gets pressed.
from pynput.keyboard import Key, Listener, Controller
keyboard = Controller()
def on_press(key):
if key == Key.media_volume_up:
keyboard.press(Key.media_down)
keyboard.press(Key.media_next)
if key == Key.media_volume_down:
keyboard.press(Key.media_volume_up)
keyboard.press(Key.media_previous)
def on_release(key):
pass
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
I tried to make it like this but it also triggers the previous if statement so it's looping.
from pynput.keyboard import Key, Listener, Controller
keyboard = Controller()
block = False
def on_press(key):
global block
if not block:
if key == Key.media_volume_up:
block = True
keyboard.press(Key.media_volume_down)
keyboard.press(Key.media_next)
block = False
if key == Key.media_volume_down:
block = True
keyboard.press(Key.media_volume_up)
keyboard.press(Key.media_previous)
block = False
def on_release(key):
pass
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
How can you see which button is pressed in pynput.
from pynput.keyboard import Key, Listener
def a(key):
print('{0} pressed'.format(
key))
if key == 'a':
print('ape')
with Listener(on_press = a) as listener:
listener.join()
Does not see work.
from pynput.keyboard import Listener
def a(key):
print(f"{key}, was pressed")
if key.char == "a":
print("foo")
with Listener(on_press=a) as listener:
listener.join()
Output:
q'q', was pressed
w'w', was pressed
a'a', was pressed
foo
s's', was pressed
However this will cause issues if you press the caps lock for example so I would place in a try except block:
try:
if key.char == 'a':
print("foo")
except AttributeError:
pass
i'm fairly new to python. This is my code till now.
from pynput.keyboard import Listener
counta = 0
def on_press(key):
if key == a:
print("a is pressed")
global counta
counta += 1
print(counta)
else:
print("keyboard pressed")
with Listener(on_press=on_press) as listener:
listener.join()
I want to check if the Key that is pressed is the lower a. But till now it prints out "keyboard pressed" no matter if i press the 'a' key or any other key.
Thanks for any help
You need to check the key.char attribute and also use quotes to 'a'
from pynput.keyboard import Listener
counta = 0
def on_press(key):
if key.char == 'a':
print("a is pressed")
global counta
counta += 1
print(counta)
else:
print("keyboard pressed")
with Listener(on_press=on_press) as listener:
listener.join()
Try to use the debugger it will help you a lot.