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

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
nayanagg
Posts: 8
Joined: Mon May 03, 2021 8:26 pm

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

Post by nayanagg » Mon May 03, 2021 8:28 pm

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...

User avatar
Roberthh
Posts: 2683
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Tue May 04, 2021 6:20 am

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.

nayanagg
Posts: 8
Joined: Mon May 03, 2021 8:26 pm

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

Post by nayanagg » Tue May 04, 2021 9:31 am

I'm using Thonny IDE connected to Pi Pico

User avatar
Roberthh
Posts: 2683
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Tue May 04, 2021 9:44 am

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.

hippy
Posts: 29
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

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

Post by hippy » Tue May 04, 2021 12:47 pm

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

User avatar
Roberthh
Posts: 2683
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Tue May 04, 2021 1:43 pm

@hippy Good hint.

User avatar
russ_h
Posts: 54
Joined: Thu Oct 03, 2019 2:26 am

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

Post by russ_h » Tue May 04, 2021 5:30 pm

@hippy Excellent idea.

nayanagg
Posts: 8
Joined: Mon May 03, 2021 8:26 pm

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

Post by nayanagg » Thu May 06, 2021 12:25 pm

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.

User avatar
Roberthh
Posts: 2683
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Thu May 06, 2021 3:02 pm

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?

nayanagg
Posts: 8
Joined: Mon May 03, 2021 8:26 pm

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

Post by nayanagg » Fri May 07, 2021 1:07 am

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.

Post Reply