I am controlling a remote toy car using python code. As of now, the code is as below:
def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
def car():
while True:
key = getkey()
if key == 's': #Down arrow
print "Down"
Backward()
elif key == 'w': #Up arrow
print "Up"
forward()
elif key == 'a':
print "left"
Left()
elif key == 'd':
print "Right"
Right()
elif key == 'q': #Quit
print "That's It"
break
def forward():
GPIO.output(11,True) #Move forward
When I press 'w' forward() method is called and the car moves forward but wont stop until I quit the program or call GPIO.output(11, False) from some other method.
Is there any key Listener which detects the key release of any particular key?
For example, if 'w' pressed called this method and if released call some other method
Sudo code:
if w_isPressed()
forward()
else if w_isReleased()
stop()
I've seen Pygame game development library being successfully used in similar scenarios before, handling realtime systems and machinery in production, not just toy examples. I think it's a suitable candidate here too. Check out pygame.key module for what is possible to do with the keyboard input.
In short, if you are not familiar with game development, you basically continuously poll for events such as input state changes inside an 'infinite' game loop and react accordingly. Usually update the parameters of the system using deltas per time elapsed. There's plenty of tutorials on that and Pygame available around and Pygame docs are pretty solid.
A simple example of how to go about it:
import pygame
pygame.init()
# to spam the pygame.KEYDOWN event every 100ms while key being pressed
pygame.key.set_repeat(100, 100)
while 1:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print 'go forward'
if event.key == pygame.K_s:
print 'go backward'
if event.type == pygame.KEYUP:
print 'stop'
You'll need to play with pygame.KEYDOWN, pygame.KEYUP and pygame.key.set_repeat depending on how your car movement is implemented.
Faced a similar problem (I am no Python expert) but this worked for me
import pynput
from pynput import keyboard
def on_press(key):
try:
print('Key {0} pressed'.format(key.char))
#Add your code to drive motor
except AttributeError:
print('Key {0} pressed'.format(key))
#Add Code
def on_release(key):
print('{0} released'.format(key))
#Add your code to stop motor
if key == keyboard.Key.esc:
# Stop listener
# Stop the Robot Code
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Related
I am trying to make a program that will read the user's inputs and if it detects a certain key combination, it will type a special character. Currently I am testing and I want the script to just output a '+' after each character that the user types. I am using the pynput event listener to determine if a key was pressed and using a 'Pause' variable so that the output of the '+' doesn't cause the event listener to make an infinite loop. The problem is that the event listener is going into an infinite loop anyway and I am not sure why.
Current typed output after each key press: +++++++++++++++++++...
Desired typed output after each key press: +
Current console output after each key press:
pause is False
getting pressed
getting released
['f']
paused
send outputs
unpaused
pause is False
getting pressed
getting released
['+']
paused
send outputs
unpaused
pause is False
getting pressed
getting released
['+']
paused
send outputs
unpaused
pause is False
getting pressed
getting released
...
Desired console output after each key press:
pause is False
getting pressed
getting released
['f']
paused
send outputs
pause is True
unpaused
import pynput
import time
from pynput.keyboard import Key, Listener, Controller
keyboardOut = Controller()
PressedKeys = []
ReleasedKeys = []
Pause = False
def on_press(key):
global PressedKeys, ReleasedKeys, Pause
print("pause is "+str(Pause))
if Pause == False:
print("getting pressed")
PressedKeys.append(key)
else:
return
def on_release(key):
global PressedKeys, ReleasedKeys, Pause
if Pause == False and len(PressedKeys) > 0:
print("getting released")
ReleasedKeys.append(key)
test_frame()
else:
return
def test_frame():
global Pause
print(PressedKeys)
Pause = True
print("paused")
print("send outputs")
keyboardOut.press('+')
keyboardOut.release('+')
PressedKeys.clear()
ReleasedKeys.clear()
Pause = False
print("unpaused")
time.sleep(1)
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
How can I fix my code to *actually* pause the event listener. I am new to python, so I might just not understand how to update global variables correctly.
You have to stop the listener before using the keyboard controller.
I hope this code helps out, I tried to get as close to what I thought you were looking for.
code:
from pynput import keyboard
from time import sleep
KeyboardOut = keyboard.Controller()
pressed_keys = []
released_keys = []
def on_press(key):
try:
pressed_keys.append(format(key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
if len(pressed_keys) > 0:
released_keys.append(format(key))
test_frame()
return False
if key == keyboard.Key.esc:
# Stop listener
return False
def test_frame():
print('\n\n'+str(pressed_keys))
print('paused')
print('send outputs')
KeyboardOut.press('+')
KeyboardOut.release('+')
pressed_keys.clear()
released_keys.clear()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.run()
print('unpaused')
sleep(1)
return True
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.run()
I am a student learning Python, it would be great if you guys can help me out with this.
This is a temporary minimum code segment, window1 is a premade variable that creates a rectangle outside the function.
def key_pressed(event):
if event.key == "a":
add(window1)
else:
if event.key == "n":
name = str(input("Task Name: "))
tname = Text(name)
tname.set_position(get_width(), get_height())
add(tname)
elif event.key == "t":
time = int(input("Due Time: "))
ttime = Text(str(time))
add(tname)
if event.key == "Escape":
remove(window1)
remove(tname)
remove(ttime)
add_key_down_handler(key_pressed)
etc.etc.
I'm using Python 3 Graphics (Brython) on the website CodeHS, and I want my program to allow a key event only after another one has been pressed. I've tried nesting the if statements and other stuff but I can't seem to get it to work. Plus, the event.key == "Escape" does not remove the tname and ttime graphics.
How can I make it so that 'n' and 't' can only be pressed after 'a' is pressed and not be pressed after 'esc' when it removes the window?
Thanks
I'm trying to use pynput to get realtime keyboard input, but the on_press function gets called on autorepeat.
example code:
#!/usr/bin/env python3
import sys
import numpy as np
import sounddevice as sd
from pynput import keyboard
frequency = []
def on_press(key):
global frequency
try:
print(key.vk)
if key.char == "q":
frequency.append(440)
elif key.char == "w":
frequency.append(880)
except AttributeError:
pass
def on_release(key):
global frequency
if key == keyboard.Key.esc:
# Stop listener
print("Press Enter")
return False
elif key.char == "q":
frequency.remove(440)
elif key.char == "w":
frequency.remove(880)
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release,
suppress = True)
listener.start()
start_idx = 0
def callback(outdata, frames, time, status):
if status:
print(status, file=sys.stderr)
print(frames)
global start_idx
t = (start_idx + np.arange(frames)) / 48000
t = t.reshape(-1, 1)
outdata[:] = 0 * t
if len(frequency) > 0:
print("Playing")
for freq in frequency:
outdata[:] = outdata[:] + 0.2 * np.sin(2 * np.pi * freq * t)
start_idx += frames
try:
with sd.OutputStream(channels=1, callback=callback,
samplerate=48000):
input()
listener.stop()
except Exception as e:
print("Exception")
listener.stop()
exit()
If you run the code and press and hold the Q key, the keyboard autorepeat kicks in and ruins the whole listener. Is there a python input module which handles raw keyboard input properly?
The second thing is that the code tends to crash my Xorg quite regularly. I just run the script a few times and the Xorg goes down. And I can't figure out why. Linux 5.5.2-zen1-1-zen x86_64 GNU/Linux, X.Org 1.20.7.
The third thing is that the sound synthesis seems to lag quite a bit. The number of frames to the callback function seems to hang around 400, which at the rate of 48000 samples per second is less then 10 milliseconds, but the actual audio feedback feels like on hundreds of milliseconds delay.
pygame has a good keylistener and is easy to build a GUI window that displays output. It also works well in Linux:
import pygame
def main_loop():
#code
loopExit = False
while not loopExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loopExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
#code
if event.type == pygame.KEYUP:
if event.key == pygame.K_q:
#code
https://www.pygame.org/docs/ref/key.html
I am using a Raspberry Pi 3 to control a robotic vehicle. I have successfully linked my PS4 controller to the RPi using ds4drv. I have the following code working and outputting "Button Pressed"/"Button Released" when a button is pressed/released on the PS4 controller using pygame. I am wondering how to identify which button is exactly being pressed.
ps4_controller.py
import pygame
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
try:
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")
except KeyboardInterrupt:
print("EXITING NOW")
j.quit()
Figured out a hack.
The PS4 buttons are numbered as the following:
0 = SQUARE
1 = X
2 = CIRCLE
3 = TRIANGLE
4 = L1
5 = R1
6 = L2
7 = R2
8 = SHARE
9 = OPTIONS
10 = LEFT ANALOG PRESS
11 = RIGHT ANALOG PRESS
12 = PS4 ON BUTTON
13 = TOUCHPAD PRESS
To figure out which button is being pressed I used j.get_button(int), passing in the matching button integer.
Example:
import pygame
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
try:
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
if j.get_button(6):
# Control Left Motor using L2
elif j.get_button(7):
# Control Right Motor using R2
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")
except KeyboardInterrupt:
print("EXITING NOW")
j.quit()
You are really close! With a few tweaks, you code becomes this instead:
import pygame
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
try:
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYAXISMOTION:
print(event.dict, event.joy, event.axis, event.value)
elif event.type == pygame.JOYBALLMOTION:
print(event.dict, event.joy, event.ball, event.rel)
elif event.type == pygame.JOYBUTTONDOWN:
print(event.dict, event.joy, event.button, 'pressed')
elif event.type == pygame.JOYBUTTONUP:
print(event.dict, event.joy, event.button, 'released')
elif event.type == pygame.JOYHATMOTION:
print(event.dict, event.joy, event.hat, event.value)
except KeyboardInterrupt:
print("EXITING NOW")
j.quit()
Some resources that I found helpful in writing the up included pygame's event documentation, the use of python's dir function to see what properties a python object has, and the documentation for pygame's parent C library, SDL if you wanted a deeper explanation of what the property actually means. I included both the dictionary access version (using event.dict) as well as the property-access version (using just event.whatever_the_property_name_is). Note that event.button only gives you a number; it is up to you to manually create a mapping of what each button number means on your controller. Hope this clears it up!
A bit late, but if there are people looking for a solution on this still, I have created a module: pyPS4Controller that has all the button events on the controller already mapped and they can be overwritten like so:
from pyPS4Controller.controller import Controller
class MyController(Controller):
def __init__(self, **kwargs):
Controller.__init__(self, **kwargs)
def on_x_press(self):
print("Hello world")
def on_x_release(self):
print("Goodbye world")
controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
# you can start listening before controller is paired, as long as you pair it within the timeout window
controller.listen(timeout=60)
This is just an example for 1 event. There are more events that can be overwritten such as:
on_x_press
on_x_release
on_triangle_press
on_triangle_release
on_circle_press
on_circle_release
on_square_press
on_square_release
on_L1_press
on_L1_release
on_L2_press
on_L2_release
on_R1_press
on_R1_release
on_R2_press
on_R2_release
on_up_arrow_press
on_up_down_arrow_release
on_down_arrow_press
on_left_arrow_press
on_left_right_arrow_release
on_right_arrow_press
on_L3_up
on_L3_down
on_L3_left
on_L3_right
on_L3_at_rest # L3 joystick is at rest after the joystick was moved and let go off
on_L3_press # L3 joystick is clicked. This event is only detected when connecting without ds4drv
on_L3_release # L3 joystick is released after the click. This event is only detected when connecting without ds4drv
on_R3_up
on_R3_down
on_R3_left
on_R3_right
on_R3_at_rest # R3 joystick is at rest after the joystick was moved and let go off
on_R3_press # R3 joystick is clicked. This event is only detected when connecting without ds4drv
on_R3_release # R3 joystick is released after the click. This event is only detected when connecting without ds4drv
on_options_press
on_options_release
on_share_press # this event is only detected when connecting without ds4drv
on_share_release # this event is only detected when connecting without ds4drv
on_playstation_button_press # this event is only detected when connecting without ds4drv
on_playstation_button_release # this event is only detected when connecting without ds4drv
Full documentation is available # https://github.com/ArturSpirin/pyPS4Controller
if event.type == pygame.JOYBUTTONDOWN:
if j.get_button(0):
PXV = -0.1
if j.get_button(2):
PXV = 0.1
if event.type == pygame.JOYBUTTONUP:
if j.get_button(0) or j.get_button(2):
PXV = 0
joy button down works but PXV(player x velocity)
does not get reset back to zero when i release my controller
I'm currently working on project where I ssh to a raspberry pi from my laptop to control some motors. I have written some code in Python that allows you to enter a letter and depending on the letter it moves forwards or backwards. However you have to press enter after every letter for the code to execute.
Is there a way that the interface detects letters without the need to press enter.
I know you can bind key presses in tkinter but I can not do that through ssh.
Thanks in advance
You could use the curses library for that.
You can grab the key that was pressed using the screen.getch() function. It will return the decimal code of the key (see ASCII Table).
An example:
import curses
screen = curses.initscr()
curses.cbreak()
screen.keypad(1)
key = ''
while key != ord('q'): # press <Q> to exit the program
key = screen.getch() # get the key
screen.addch(0, 0, key) # display it on the screen
screen.refresh()
# the same, but for <Up> and <Down> keys:
if key == curses.KEY_UP:
screen.addstr(0, 0, "Up")
elif key == curses.KEY_DOWN:
screen.addstr(0, 0, "Down")
curses.endwin()
Another option is sshkeyboard library. Simply pip install sshkeyboard, and then use the following code to detect key presses over SSH:
from sshkeyboard import listen_keyboard
def press(key):
print(f"'{key}' pressed")
def release(key):
print(f"'{key}' released")
listen_keyboard(
on_press=press,
on_release=release,
)
Inside def press you could have some logic to react to specific keys:
def press(key):
if key == "up":
print("up pressed")
elif key == "down":
print("down pressed")
elif key == "left":
print("left pressed")
elif key == "right":
print("right pressed")