[SOLVED] Python - how to process console input without newline?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Python - how to process console input without newline?
I need to process console input but it does not include any end-of-line indication.
The application is a remote control for an entertainment system using a Raspberry Pi/Arduino combination to receive commands and drive the IR LEDs, and a Bluetooth keyboard.
I need to be able to receive a single character as it is keyed. I don't want to have to press Enter.
Edit:
I tried this, it gives a number for each key press, doesn't need anything extra
Code:
#! /usr/bin/python
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)
I initially tried the code in Emacs' shell and immediately got 1758 spaces and a blinking cursor. Characters entered were echoed, but no numbers. I thought before I say anything I should try it in konsole. It worked well there.
You might want to slow down that infinite loop so that it doesn't hog
all of your cpu, it was just an example.
Code:
import curses, time
def main(stdscr):
# do not wait for input when calling getch
stdscr.nodelay(1)
while True:
time.sleep(.1)
# get keyboard input, returns -1 if none available
c = stdscr.getch()
........
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.