Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
curses library
#1
Hi there,
i am new in programming and i am using a rpi 3 b with python 2.7 to control a couple of servo motors but i have a couple of problems with my code.
I want to use keyboard inputs to activate my gpio's.
I have done some research and i found a library that can help me do that but i am getting all kind of bugs. Basically i want the keyboard controls i type to go in a variable (i use raw_input). I dont know though how to write the keyboard keys in the code. For example i know that up arrow is KEY_UP, down arrow is KEY_DOWN etc. Is there any way that i can see how to express each key to my code?
Or any different idea on how to make this work ?
Thanks in advance
Reply
#2
First, you should be using python 3.7. That's the latest version.
Python 2.7 dies in less that 10 months.
That being said, please:
  • Show code
  • Show errors complete and unmodified
  • Use BBcode tags
Reply
#3
Something I found on the web a long time ago. There has to be other key capture programs as well.

import curses

arrow_keys = {curses.KEY_UP:'up arrow',
              curses.KEY_DOWN:'down arrow',
              curses.KEY_LEFT:'left arrow',
              curses.KEY_RIGHT:'right arrow'}

## open a window/screen object
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(0,10,"Hit 'q' to quit ")
stdscr.refresh()

key = ''
while key != ord('q'):
    key = stdscr.getch()
    stdscr.addstr(20,25,"     ")
    stdscr.addch(20,25,key)
    stdscr.refresh()

    if key in arrow_keys:
       stdscr.addstr(18, 20, arrow_keys[key])
    else:
       stdscr.addstr(18, 20, "              ")

curses.endwin()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to be able to scroll up in curses to see previous text. Caiden 1 716 Jul-28-2023, 01:15 PM
Last Post: deanhystad
  curses issue otalado 2 2,682 Jun-29-2021, 02:07 PM
Last Post: tmz
  How to make curses.border() use A_BOLD atttribute? pjfarley3 0 4,776 Feb-03-2021, 11:22 PM
Last Post: pjfarley3
  Curses script doesn't work wavic 1 4,064 Jan-08-2021, 09:11 PM
Last Post: wavic
  Why aren't all curses panel functions supported in python curses.panel? pjfarley3 2 2,616 Jul-22-2020, 11:08 PM
Last Post: pjfarley3
  curses key codes not working jgrillout 0 2,975 Feb-11-2019, 01:46 AM
Last Post: jgrillout
  Pretty table and curses? MuntyScruntfundle 0 2,850 Oct-16-2018, 10:22 AM
Last Post: MuntyScruntfundle
  curses.initscr doesn't work zaphod424 3 9,691 Feb-28-2018, 12:36 PM
Last Post: Gribouillis
  Curses could not find terminal Solstice 13 16,864 Jan-06-2018, 07:56 AM
Last Post: Gribouillis
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,740 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020