LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2018, 08:36 AM   #1
CollieJim
Member
 
Registered: Mar 2005
Distribution: Gentoo, Kubuntu
Posts: 582

Rep: Reputation: 28
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.

How can it be done? Is a C module needed?

Thanks
 
Old 05-20-2018, 10:34 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,135
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
https://docs.python.org/2/library/ms...ml#console-i-o
https://docs.python.org/2/library/select.html

https://stackoverflow.com/questions/...ading-keypress
https://stackoverflow.com/questions/...ress-in-python

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)

Last edited by teckk; 05-20-2018 at 10:46 AM.
 
1 members found this post helpful.
Old 05-20-2018, 11:05 AM   #3
CollieJim
Member
 
Registered: Mar 2005
Distribution: Gentoo, Kubuntu
Posts: 582

Original Poster
Rep: Reputation: 28
Thanks. Just what I need.

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.
 
Old 05-20-2018, 11:10 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,135
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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()
        ........
 
Old 05-20-2018, 11:44 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by CollieJim View Post
I initially tried the code in Emacs' shell and immediately got 1758 spaces and a blinking cursor.
It should work in M-x term, but M-x shell doesn't give you a fully functional terminal for handling curses programs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
match newline, space, newline vincix Programming 13 03-09-2017 03:14 PM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
How do i ask for input in python? raskol Programming 3 03-29-2008 10:36 PM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM
console input in perl! farhanali Programming 7 06-12-2003 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration