I have a simple python script, that has some functions that run in a loop (I'm taking sensor readings).
while True:
print "Doing a function"
If the keyboard is pressed I'd like to print "key pressed".
What's the simplest way of doing this in Python? I've searched high and low. I've found out how to do it with pygame, but I'd rather do it without. If I do have to use pygame is it possible to not have a separate window for the application?:
import pygame, time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame Keyboard Test')
pygame.mouse.set_visible(0)
while True:
print "doing a function"
for event in pygame.event.get():
if (event.type == KEYUP) or (event.type == KEYDOWN):
print "key pressed"
time.sleep(0.1)
Best Answer
Edit:
I've thought about this problem a lot, and there are a few different behaviors one could want. I've been implementing most of them for Unix and Windows, and will post them here once they are done.
Synchronous/Blocking key capture:
input
orraw_input
, a blocking function which returns text typed by a user once they press a newline.Asynchronous key capture:
Polling:
The user simply wants to be able to do something when a key is pressed, without having to wait for that key (so this should be non-blocking). Thus they call a poll() function and that either returns a key, or returns None. This can either be lossy (if they take too long to between poll they can miss a key) or non-lossy (the poller will store the history of all keys pressed, so when the poll() function requests them they will always be returned in the order pressed).
The same as 1, except that poll only returns something once the user presses a newline.
Robots:
These are something that can be called to programmatically fire keyboard events. This can be used alongside key captures to echo them back out to the user
Implementations
Synchronous/Blocking key capture:
A simple
input
orraw_input
, a blocking function which returns text typed by a user once they press a newline.A simple blocking function that waits for the user to press a single key, then returns that key
Asynchronous key capture:
A callback that is called with the pressed key whenever the user types a key into the command prompt, even when typing things into an interpreter (a keylogger)
A callback that is called with the typed text after the user presses enter (a less realtime keylogger)
Windows:
This uses the windows Robot given below, naming the script keyPress.py
A callback that is called with the keys pressed when a program is running (say, in a for loop or while loop)
Windows:
Polling:
The user simply wants to be able to do something when a key is pressed, without having to wait for that key (so this should be non-blocking). Thus they call a poll() function and that either returns a key, or returns None. This can either be lossy (if they take too long to between poll they can miss a key) or non-lossy (the poller will store the history of all keys pressed, so when the poll() function requests them they will always be returned in the order pressed).
Windows and OS X (and maybe Linux):
Simple use case:
The same as above, except that poll only returns something once the user presses a newline.
Robots:
These are something that can be called to programmatically fire keyboard events. This can be used alongside key captures to echo them back out to the user
Windows:
OS X: