Polling the keyboard (detect a keypress) in python
Asked Answered
B

12

79

How can I poll the keyboard from a console python app? Specifically, I would like to do something akin to this in the midst of a lot of other I/O activities (socket selects, serial port access, etc.):

while True:
    # doing amazing pythonic embedded stuff
    # ...

    # periodically do a non-blocking check to see if
    # we are being told to do something else
    x = keyboard.read(1000, timeout = 0)

    if len(x):
        # ok, some key got pressed
        # do something

What is the correct pythonic way to do this on Windows? Also, portability to Linux wouldn't be bad, though it's not required.

Bespatter answered 15/11, 2008 at 3:29 Comment(2)
Just to let other people know, I found that most solutions involving select or thread libraries did not work correctly from IDLE. However, they all worked fine on the CLI i.e. python /home/pi/poll_keyboard.pyCacophony
In general I think reacting to key presses instead of polling them periodically is more robust solution as you are not potentially missing the key presses. See my answer below.Sixteenmo
B
39

The standard approach is to use the select module.

However, this doesn't work on Windows. For that, you can use the msvcrt module's keyboard polling.

Often, this is done with multiple threads -- one per device being "watched" plus the background processes that might need to be interrupted by the device.

Burr answered 15/11, 2008 at 17:9 Comment(4)
Correct me if i’m wrong, but in my experience msvcrt only works when you run the program in a command window, ie. not in IDLE and not with a GUI...Modest
@digitalHamster0: Anything that replaces sys.stdin with a custom object (e.g. IDLE, most GUIs) would have that effect. When sys.stdin isn't a true file, you can't use select; when it's not connected to a "real" console, you can't use the msvcrt keyboard polling functions (that implicitly rely on a "real" console).Anaemia
In general I think reacting to key presses instead of polling them periodically is more robust solution as you are not potentially missing the key presses. See my answer below.Sixteenmo
If you could a minimal working example, that will be great. The link does not provide enough information.Congener
G
23

A solution using the curses module. Printing a numeric value corresponding to each key pressed:

import curses

def main(stdscr):
    # do not wait for input when calling getch
    stdscr.nodelay(1)
    while True:
        # get keyboard input, returns -1 if none available
        c = stdscr.getch()
        if c != -1:
            # print numeric value
            stdscr.addstr(str(c) + ' ')
            stdscr.refresh()
            # return curser to start position
            stdscr.move(0, 0)

if __name__ == '__main__':
    curses.wrapper(main)
Ghastly answered 19/8, 2010 at 17:12 Comment(2)
OZ123: It can. See #32417879Tautology
Had issues using curses via SSH term on headless host. Issues were badly messing up the terminal - requiring it to be reset between each run. It did work, i.e. detect keypress. There has to be a smarter solution.Subtropics
B
18

Ok, since my attempt to post my solution in a comment failed, here's what I was trying to say. I could do exactly what I wanted from native Python (on Windows, not anywhere else though) with the following code:

import msvcrt 

def kbfunc(): 
   x = msvcrt.kbhit()
   if x: 
      ret = ord(msvcrt.getch()) 
   else: 
      ret = 0 
   return ret
Bespatter answered 20/11, 2008 at 0:32 Comment(0)
J
18

None of these answers worked well for me. This package, pynput, does exactly what I need.

https://pypi.python.org/pypi/pynput

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()
Joly answered 11/12, 2016 at 6:31 Comment(3)
This worked for me, except the key pressed was echoed to the screen immediately after being pressed, and there was no way to disable it. github.com/moses-palmer/pynput/issues/47 Plus, the characters are buffered and additionally appear on the command line when the program exits too.This appears to be a limitation of the Linux implementation, but it works fine on Windows.Enrage
This solution doesn't work when the script runs via ssh. It bombs out with the error: 'Xlib.error.DisplayNameError: Bad display name "".'Smothers
As mentioned above by David - this is not a good solution for headless instances as it has a dependency on Xserver. import Xlib.displaySubtropics
I
16
import sys
import select

def heardEnter():
    i,o,e = select.select([sys.stdin],[],[],0.0001)
    for s in i:
        if s == sys.stdin:
            input = sys.stdin.readline()
            return True
    return False
Indraft answered 20/9, 2009 at 1:47 Comment(11)
no worky. got error: select.error: (10093, 'Either the application has not called WSAStartup, or WSAStartup failed')Cohune
I've heard, more than a couple times, that the select system call on MS Windows doesn't support regular file descriptors and only works on sockets. (I don't know if the Python implementation of select() has ever worked around that under the hood).June
what is this weird timeout for? It works for me with timeout=0 but not with 0.0001 as shown..Metry
Windows 7 + python 2.7 returns error: i,o,e = select.select([sys.stdin],[],[],0) select.error: (10038, 'An operation was attempted on something that is not a socket')Rewarding
For me, this only detects keypresses after I press Enter.Murrah
@frans: Presumably the timeout is there for when the input pipe has just been drained, but whatever is feeding the pipe has more data to send that wouldn't fit in the pipe buffer; you allow a small amount of time for the input process to refill the buffer before giving up.Anaemia
Works on Mac, but only indicates pressing enter (or returning a typed line if changing the return statement). Would it be possible to return keypresses immediately? Combining with e.g. code.activestate.com/recipes/134892 (which returns individual keypresses but blocks)?Angeloangelology
There's actually a non-blocking version of the getch method in comments, but it misses keypresses which occur whenever the program is outside of the select timeout. code.activestate.com/recipes/134892/#c12Angeloangelology
On Ubuntu 18.10, this does not detect anything until <enter> is pressed. :-\Covariance
@MarkSmith: That's because the program hasn't received the input until either enter or control-D (*) is pressed, it's still in the kernel's "line-editing buffer". (If you press control-D with no characters in the buffer it will close the terminal.) // For this to work on unix-like systems, the terminal must be set to 'raw' or 'cbreak' mode instead of 'cooked'. I think this is done through some ioctl on stdin.Telegu
@JonathanHartley: (See my previous comment.)Telegu
B
6

From the comments:

import msvcrt # built-in module

def kbfunc():
    return ord(msvcrt.getch()) if msvcrt.kbhit() else 0

Thanks for the help. I ended up writing a C DLL called PyKeyboardAccess.dll and accessing the crt conio functions, exporting this routine:

#include <conio.h>

int kb_inkey () {
   int rc;
   int key;

   key = _kbhit();

   if (key == 0) {
      rc = 0;
   } else {
      rc = _getch();
   }

   return rc;
}

And I access it in python using the ctypes module (built into python 2.5):

import ctypes
import time

# first, load the DLL
try:
    kblib = ctypes.CDLL("PyKeyboardAccess.dll")
except:
    raise ("Error Loading PyKeyboardAccess.dll")

# now, find our function
try:
    kbfunc = kblib.kb_inkey
except:
    raise ("Could not find the kb_inkey function in the dll!")

# Ok, now let's demo the capability  
while True:
    x = kbfunc()

    if x != 0:
        print "Got key: %d" % x
    else:
        time.sleep(.01)
Bespatter answered 15/11, 2008 at 23:53 Comment(6)
How is this better than the built-in msvcrt.kbhit()? What advantage does it have?Burr
You are absolutely right! I misread your post; I didn't realize there is a python module called msvcrt! I just thought you meant "use the ms crt," and then I got drawn into thinking about threads and didn't connect the dots. You are absolutely right.Bespatter
I did the same thing with: import msvcrt def kbfunc(): x = msvcrt.kbhit() if x: ret = ord(msvcrt.getch()) else: ret = 0 return retBespatter
Please, do not use a lambda like that. "x = lambda" is supposed to be spelled "def x():" Saving a lambda confuses the n00bz and drives the experienced crazy trying to explain it.Burr
LOL! That's not a lambda. that's how the "comments" field reformatted my attempt to drop code into a comment. BTW saving a lambda confuses me too, and I am not a python n00b :-)Bespatter
This solution really intrigues me. I find the msvcrt approach to be slow, even in "optimized" micro loops as many solutions here provide. About this refer to link. Though this solution is not very portable.Jephum
B
5

I've come across a cross-platform implementation of kbhit at http://home.wlu.edu/~levys/software/kbhit.py (made edits to remove irrelevant code):

import os
if os.name == 'nt':
    import msvcrt
else:
    import sys, select

def kbhit():
    ''' Returns True if a keypress is waiting to be read in stdin, False otherwise.
    '''
    if os.name == 'nt':
        return msvcrt.kbhit()
    else:
        dr,dw,de = select.select([sys.stdin], [], [], 0)
        return dr != []

Make sure to read() the waiting character(s) -- the function will keep returning True until you do!

Blondy answered 15/4, 2019 at 15:13 Comment(4)
Is this still up to date? When I call the select version, I always get content back in dr. If it still works, can you put it in context? I have a "while true" loop I'd like to bail out from if a key is pressed.Aspect
@Aspect maybe you don't read() the waiting characters after detecting them as advised.Blondy
@Blondy where do you read() the character from?Apfel
@ThomasBrowne StdinBlondy
J
4

You might look at how pygame handles this to steal some ideas.

Jenifferjenilee answered 15/11, 2008 at 3:49 Comment(1)
PyGame event handling only works for GUI, not the console as the OP asked.Vauntcourier
A
4

I am using this for checking for key presses, can't get much simpler:

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import curses, time

def main(stdscr):
    """checking for keypress"""
    stdscr.nodelay(True)  # do not wait for input when calling getch
    return stdscr.getch()

while True:
    print("key:", curses.wrapper(main)) # prints: 'key: 97' for 'a' pressed
                                        # '-1' on no presses
    time.sleep(1)

While curses is not working on windows, there is a 'unicurses' version, supposedly working on Linux, Windows, Mac but I could not get this to work

Agonic answered 19/3, 2018 at 15:34 Comment(1)
There is also windows-curses on PyPI.Telegu
S
2

One more option would be to use sshkeyboard library to enable reacting to key presses instead of polling them periodically, and potentially missing the key press:

from sshkeyboard import listen_keyboard, stop_listening

def press(key):
    print(f"'{key}' pressed")
    if key == "z":
        stop_listening()

listen_keyboard(on_press=press)

Simply pip install sshkeyboard to use it.

Sixteenmo answered 27/10, 2021 at 14:22 Comment(0)
T
1

This can be done using 'pynput' module in python, You press a key and it gets printed It's that easy!

  1. PIP Install the module in command prompt, write following text and press enter

    pip install pynput

  2. Run the following code:

    from pynput.keyboard import Key, Listener
    
    def pressed(key):
        print('Pressed:',key)
    
    def released(key):
        print('Released:',key)
        if key == Key.enter:
            # Stop detecting when enter key is pressed
            return False
    
    # Below loop for Detcting keys runs until enter key is pressed
    with Listener(on_press=pressed, on_release=released) as detector:
        detector.join()
    
  3. You can end the loop with any key you want by changing Key.enter to some other key in the 8th line of the code.

Truitt answered 28/8, 2021 at 7:14 Comment(0)
G
-1

If you combine time.sleep, threading.Thread, and sys.stdin.read you can easily wait for a specified amount of time for input and then continue, also this should be cross-platform compatible.

t = threading.Thread(target=sys.stdin.read(1) args=(1,))
t.start()
time.sleep(5)
t.join()

You could also place this into a function like so

def timed_getch(self, bytes=1, timeout=1):
    t = threading.Thread(target=sys.stdin.read, args=(bytes,))
    t.start()
    time.sleep(timeout)
    t.join()
    del t

Although this will not return anything so instead you should use the multiprocessing pool module you can find that here: how to get the return value from a thread in python?

Granophyre answered 31/7, 2015 at 20:18 Comment(2)
Shouldn't that first line be: t = threading.Thread(target=sys.stdin.read, args=(1,))Bespatter
Won't this solution always sleep for 5 seconds, even if the user presses a key before that?Bespatter

© 2022 - 2024 — McMap. All rights reserved.