Is the only way to read single characters from the keyboard to use >curses.cbreak() or curses.raw()? If so how do I then read characters,
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
Curses is not portable IIRC. A more portable means would
be to use tkinter with the "bind" function to bind keys.
Is the only way to read single characters from the keyboard to use curses.cbreak() or curses.raw()? If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.
Chris Green <cl@isbd.net> writes:
Is the only way to read single characters from the keyboard to use >curses.cbreak() or curses.raw()? If so how do I then read characters,
It seems that you want to detect keypresses and not read
characters from a line-buffered console with editing
features.
Curses is not portable IIRC. A more portable means would
be to use tkinter with the "bind" function to bind keys.
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
answer = input( 'Format drive C: (Y/N)?' )
import tkinter
ram@zedat.fu-berlin.de (Stefan Ram) writes:
import tkinter
This two-liner allows to answer with just one keypress ([Y]/[N]) here.
import tkinter.messagebox
tkinter.messagebox.askyesno( "Question", "Format harddisk?" )
--
https://mail.python.org/mailman/listinfo/python-list
Is the only way to read single characters from the keyboard to use curses.cbreak() or curses.raw()? If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Curses is not portable IIRC. A more portable means wouldimport tkinter
be to use tkinter with the "bind" function to bind keys.
text = tkinter.Text()
text.pack()
text.bind\
( "<y>",
lambda event:
text.insert
( tkinter.END, "Y\nFormatting drive C:\n...\n" )or "break" )
text.insert( tkinter.END, "Format drive C:?\n" )
text.focus()
tkinter.mainloop()
Not allowing users to edit their keypresses before confirming
them with [Return]. What could possibly go wrong?
import sys, termios, tty
Is the only way to read single characters from the keyboard to use curses.cbreak() or curses.raw()? If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.
Chris Green <cl@isbd.net> writes:
import sys, termios, tty
There might be some versions of Python and the Microsoft®
Windows operating system where "termios" is not available.
You would need to have a loop that collected all the utf-8 bytes of a single code point.
You can to look at the first byte of know if the utf-8 is 1, 2, 3 or 4 bytes for a code point.
# Get a single character, setcbreak rather than setraw meands
CTRL/C
etc. still work
#
def getch():
sys.stdout.flush()
tty.setcbreak(fdInput)
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)
Will not work for uncode code points above 255.
On 11 Dec 2022, at 18:50, Chris Green <cl@isbd.net> wrote:
My solution in the end was copied from one I found that was much
simpler and straightforward than most. I meant to post this earlier
but it got lost somewhere:-
import sys, termios, tty
#
#
# Read a single character from teminal, specifically for 'Y/N'
#
fdInput = sys.stdin.fileno()
termAttr = termios.tcgetattr(0)
#
#
# Get a single character, setcbreak rather than setraw meands CTRL/C
etc. still work
#
def getch():
sys.stdout.flush()
tty.setcbreak(fdInput)
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)
Traceback (most recent call last):a.getch()
termios.tcsetattr(fdInput, termios.TCSAFLUSH, termAttr)
sys.stdout.write(ch)
return ch
#
#
# Get a y or n answer, ignore other characters
#
def getyn():
ch = 'x'
while ch != 'y' and ch != 'n':
ch = getch().lower()
return ch
So getyn() reads a y or an n, ignores anything else and doesn't wait
for a return key. Keyboard input operation is restored to normal
after doing this. Using tty.setcbreak() rather than tty.setraw() means
that CTRL/C etc. still work if things go really wrong.
--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
On 11 Dec 2022, at 18:50, Chris Green <cl@isbd.net> wrote:
My solution in the end was copied from one I found that was much
simpler and straightforward than most. I meant to post this earlier
but it got lost somewhere:-
import sys, termios, tty
#
#
# Read a single character from teminal, specifically for 'Y/N'
#
fdInput = sys.stdin.fileno()
termAttr = termios.tcgetattr(0)
#
#
# Get a single character, setcbreak rather than setraw meands CTRL/C
etc. still work
#
def getch():
sys.stdout.flush()
tty.setcbreak(fdInput)
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)
Will not work for uncode code points above 255.
This is what happened when I typed € key:
Traceback (most recent call last):a.getch()
File "<stdin>", line 1, in <module>
File "/private/var/folders/ll/08dwwqkx6v9bcd15sh06x14w0000gn/T/a.py", line 15, in getch
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 0: unexpected end of data
On 11/12/2022 23.09, Chris Green wrote:
Is the only way to read single characters from the keyboard to use
curses.cbreak() or curses.raw()? If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.
All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.
Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.
You may like to re-ask this question over on the Python-Tutor list. The ListAdmin over there (literally) wrote the book on Python+curses...
Did such research include the keyboard module? https://pypi.org/project/keyboard/
This project includes an (Enter-free) "hot-key" feature which firstly
detects the specific key or key-combination, and then calls an action
if/when True.
(amongst other functionality)
Quick read tutorial: https://www.thepythoncode.com/article/control-keyboard-python
Disclaimer: have had it on my 'radar', but never actually employed.
(if you have considered, will be interested to hear conclusions...)
Is the only way to read single characters from the keyboard to use curses.cbreak() or curses.raw()?
If so how do I then read characters,
On 11/12/2022 21:07, dn wrote:
On 11/12/2022 23.09, Chris Green wrote:
Is the only way to read single characters from the keyboard to use
curses.cbreak() or curses.raw()?
You may like to re-ask this question over on the Python-Tutor list. The
ListAdmin over there (literally) wrote the book on Python+curses...
Did such research include the keyboard module?
https://pypi.org/project/keyboard/
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 75:06:03 |
Calls: | 6,716 |
Calls today: | 4 |
Files: | 12,246 |
Messages: | 5,357,385 |