Is there a way to do key listeners in python without a huge bloated module such as pygame?
An example would be, when I pressed the a key it would print to the console
The a key was pressed!
It should also listen for the arrow keys/spacebar/shift key.
To detect keypress, we will use the is_pressed() function defined in the keyboard module. The is_pressed() takes a character as input and returns True if the key with the same character is pressed on the keyboard.
A keyboard listener is a threading. Thread , and all callbacks will be invoked from the thread. Call pynput. keyboard.
I was searching for a simple solution without window focus. Jayk's answer, pynput, works perfect for me. Here is the example how I use it.
from pynput import keyboard  def on_press(key):     if key == keyboard.Key.esc:         return False  # stop listener     try:         k = key.char  # single-char keys     except:         k = key.name  # other keys     if k in ['1', '2', 'left', 'right']:  # keys of interest         # self.keys.append(k)  # store it in global-like variable         print('Key pressed: ' + k)         return False  # stop listener; remove this if want more keys  listener = keyboard.Listener(on_press=on_press) listener.start()  # start to listen on a separate thread listener.join()  # remove if main thread is polling self.keys 
                        It's unfortunately not so easy to do that. If you're trying to make some sort of text user interface, you may want to look into curses. If you want to display things like you normally would in a terminal, but want input like that, then you'll have to work with termios, which unfortunately appears to be poorly documented in Python. Neither of these options are that simple, though, unfortunately. Additionally, they do not work under Windows; if you need them to work under Windows, you'll have to use PDCurses as a replacement for curses or pywin32 rather than termios.
I was able to get this working decently. It prints out the hexadecimal representation of keys you type. As I said in the comments of your question, arrows are tricky; I think you'll agree.
#!/usr/bin/env python
import sys
import termios
import contextlib
@contextlib.contextmanager
def raw_mode(file):
    old_attrs = termios.tcgetattr(file.fileno())
    new_attrs = old_attrs[:]
    new_attrs[3] = new_attrs[3] & ~(termios.ECHO | termios.ICANON)
    try:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, new_attrs)
        yield
    finally:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, old_attrs)
def main():
    print 'exit with ^C or ^D'
    with raw_mode(sys.stdin):
        try:
            while True:
                ch = sys.stdin.read(1)
                if not ch or ch == chr(4):
                    break
                print '%02x' % ord(ch),
        except (KeyboardInterrupt, EOFError):
            pass
if __name__ == '__main__':
    main()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With