Page 1 of 1

How do I read keyboard input without waiting for the user to press Enter?

Posted: Mon May 03, 2021 8:28 pm
by nayanagg
Hi,

I want to print character in i2c LCD display from keyboard through pi pico. But the issue is in input() and sys.stdin.read(1) character is not getting visible until ENTER button is pressed.

Kindly help...

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 6:20 am
by Roberthh
It may as well be that the character is not sent from the PC until you push enter. Which terminal program do you use to connect to the Pi Pico, and how is it configured, I use sys.stdin.read() in my on-board editor for exactly reading character key by key. (see https://github.com/robert-hh/Micropytho ... pye_gen.py, line 24, https://github.com/robert-hh/Micropytho ... ter/pye.py, line 274 ff).
input() waits indeed on the Pi Pico side until an enter key arrives.

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 9:31 am
by nayanagg
I'm using Thonny IDE connected to Pi Pico

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 9:44 am
by Roberthh
I do not know how thonny handle it's REPL window. You could try to use a simple terminal emulator for REPL, like putty or TeraTerm (windows) or picocom (linux, raspbian) or screen (mac). These are known to send single keystrokes.

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 12:47 pm
by hippy
Thonny only sends when 'return' is pressed. As suggested, use a terminal program which sends each key as pressed.

A more advanced option is to build your own MicroPython firmware with an additional USB Virtual Serial Port so you can read from that using a terminal program while still having access to the full REPL.

I build my MicroPython with three USB Virtual Serial Ports; one for the REPL, one for raw data comms, the other for diagnostics, logging, whatever.

https://www.raspberrypi.org/forums/view ... p?t=305834

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 1:43 pm
by Roberthh
@hippy Good hint.

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Tue May 04, 2021 5:30 pm
by russ_h
@hippy Excellent idea.

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Thu May 06, 2021 12:25 pm
by nayanagg
Thanks for the suggestion.

Now I tried to connect a USB keyboard to pi pico and pressed a character to get it displayed on the i2c LCD screen using sys.stdin.read(1) but shows nothing.

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Thu May 06, 2021 3:02 pm
by Roberthh
You have to use a terminal emulation program like Putty for the test. I would not expect a directly attached keyboard to work. It would not be powered, and it will definitely not send ASCII or UTF-8 character codes itself. And the rp2 must operate in host mode. Did you manage to set it as such?

Re: How do I read keyboard input without waiting for the user to press Enter?

Posted: Fri May 07, 2021 1:07 am
by nayanagg
Hi,

I've attached a 5v supply to VSYS so the USB keyboard is getting power the only issue is how to use Pi Pico in USB host mode.