User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Detect keypress in Python in a simple way?

Mon Sep 30, 2019 8:11 pm

Hey guys,
I have been online for such a long time now and I really can't seem to find a working option. My plan is to run a program on BrickPi3 using Python which is very simple - move the robot forward when pressing 'w', stop it when 's' is pressed. I'm new to BrickPi3 and this is just to test. I know this isn't the BrickPi forum, but the problem is about Python and actually doesn't have anything to do with Python. So this is my program:

Code: Select all

import brickpi3
from time import sleep
import keyboard

BP = brickpi3.BrickPi3()

speed = 50

while True:
    if keyboard.is_pressed('w'):
        BP.set_motor_power(BP.PORT_B + BP.PORT_C, speed)
        if keyboard.is_pressed('s'):
            BP.set_motor_power(BP.PORT_B + BP.PORT_C, 0)
My problem is that the keyboard module isn't working. I've installed it using

Code: Select all

sudo pip3 install keyboard
, but when I run the program, it tells me I need root. So I try running the program over the terminal, and when I move into the directory and type

Code: Select all

sudo python motor_test.py
, it tells me something about 'no module named keyboard'. Is there a way to detect keypress using the keyboard module or another simple way as I would not prefer to use something like curses as it does make everything much more complicated.
Thank you very much!
Raspberry Pi's are cool and so are Polar Bears!

User avatar
joelostinspace
Posts: 76
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 8:25 pm

are you running it in python2.7???
try:

Code: Select all

sudo python3 motor_test.py
show complete error output

Tell me and I forget. Teach me and I remember. Involve me and I learn.


--- Benjamin Franklin

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 8:41 pm

Why are you using sudo - shouldn't need it.

I would probably use the pygame library to detect key presses.

User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 9:34 pm

joelostinspace wrote:
Mon Sep 30, 2019 8:25 pm
are you running it in python2.7???
try:

Code: Select all

sudo python3 motor_test.py
show complete error output
No, I'm using Python 3.
Raspberry Pi's are cool and so are Polar Bears!

User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 9:36 pm

rpiMike wrote:
Mon Sep 30, 2019 8:41 pm
Why are you using sudo - shouldn't need it.

I would probably use the Pygame library to detect key presses.
Ok. How would that translate into the original code I posted above?
Thank you.
Raspberry Pi's are cool and so are Polar Bears!

User avatar
rpiMike
Posts: 1340
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 9:52 pm

Something like the following:

Code: Select all

import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print("pressed w")
            if event.key == pygame.K_s:
                print("pressed s")
            
The downside is you need a pygame window - but i'm usually displaying something.

User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Re: Detect keypress in Python in a simple way?

Mon Sep 30, 2019 10:14 pm

Ok. Thank you very much!
Raspberry Pi's are cool and so are Polar Bears!

Return to “Python”