0
# import curses
import curses

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho() 
curses.cbreak()
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                print("up")
            elif char == curses.KEY_DOWN:
                print ("down")
            elif char == curses.KEY_RIGHT:
                print ("right")
            elif char == curses.KEY_LEFT:
                print ("left")
            elif char == 10:
                print ("stop")   

finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()

I am trying to run the above code to handle my Bluetooth keyboard.

Getting the below Issue while clicking on "run the module".

Python 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516] on linux Type "copyright", "credits" or "license()" for more information.

=============== RESTART: /home/pi/Documents/KeyBoardHandle2.py =============== Traceback (most recent call last): File "/home/pi/Documents/KeyBoardHandle2.py", line 15, in wrapper(main) File "/usr/lib/python3.5/curses/init.py", line 73, in wrapper stdscr = initscr() File "/usr/lib/python3.5/curses/init.py", line 30, in initscr fd=_sys.stdout.fileno()) _curses.error: setupterm: could not find terminal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.