Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing C++'s getch() in Python

126 views
Skip to first unread message

binoyth...@gmail.com

unread,
May 25, 2019, 7:06:19 AM5/25/19
to
I'm working on Python 3.7 under Windows. I need a way to input characters without echoing them on screen, something that getch() did effectively in C++. I read about the unicurses, ncurses and curses modules, which I was not able to install using pip.

Is there any way of getting this done?

Shakti Kumar

unread,
May 25, 2019, 7:27:46 AM5/25/19
to
On Sat, 25 May 2019 at 4:43 PM <binoyth...@gmail.com> wrote:

> I'm working on Python 3.7 under Windows. I need a way to input characters
> without echoing them on screen, something that getch() did effectively in
> C++.


try getpass module.
Typically this would be,

import getpass
variable = getpass.getpass('your prompt')


> https://mail.python.org/mailman/listinfo/python-list
>
--
Sent from Shakti’s iPhone

binoyth...@gmail.com

unread,
May 25, 2019, 10:01:08 AM5/25/19
to
Hi Shakti!

Thanks for your response. I have tried getpass() but got the following warning:

Warning (from warnings module):
File "C:\Users\Binoy\AppData\Local\Programs\Python\Python37-32\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.

And true enough, the input string is echoed. I saw a video where getpass() worked on Linux. So, probably, its a Windows thing.

Still looking for a solution to the same on Windows.

Paul Moore

unread,
May 25, 2019, 10:15:16 AM5/25/19
to
On Sat, 25 May 2019 at 12:12, <binoyth...@gmail.com> wrote:
>
> I'm working on Python 3.7 under Windows. I need a way to input characters without echoing them on screen, something that getch() did effectively in C++. I read about the unicurses, ncurses and curses modules, which I was not able to install using pip.
>
> Is there any way of getting this done?

On Windows, the msvcrt module exposes getch:
https://docs.python.org/3.7/library/msvcrt.html#msvcrt.getch

Random832

unread,
May 25, 2019, 3:37:36 PM5/25/19
to
On Sat, May 25, 2019, at 10:07, binoyth...@gmail.com wrote:
> Hi Shakti!
>
> Thanks for your response. I have tried getpass() but got the following warning:
>
> Warning (from warnings module):
> File
> "C:\Users\Binoy\AppData\Local\Programs\Python\Python37-32\lib\getpass.py", line 100
> return fallback_getpass(prompt, stream)
> GetPassWarning: Can not control echo on the terminal.
> Warning: Password input may be echoed.
>
> And true enough, the input string is echoed. I saw a video where
> getpass() worked on Linux. So, probably, its a Windows thing.

getpass works fine on the windows console. Are you running the script in an IDE such as IDLE, PyCharm, etc?

binoyth...@gmail.com

unread,
May 26, 2019, 10:47:29 AM5/26/19
to
I've run getpass() on IDLE, Spyder, PyCharm and Mu. All with negative results.



Shakti Kumar

unread,
May 26, 2019, 11:02:16 AM5/26/19
to
On Sun, 26 May 2019 at 20:23, <binoyth...@gmail.com> wrote:
>
> I've run getpass() on IDLE, Spyder, PyCharm and Mu. All with negative results.
>
>
As Random832 pointed out, these IDEs cannot handle the stdout/stdin with getpass
You should use a normal terminal (command prompt) on your windows for
running this program.

You can read more here, https://docs.python.org/3.1/library/getpass.html

eryk sun

unread,
May 26, 2019, 11:18:05 AM5/26/19
to
On 5/25/19, Paul Moore <p.f....@gmail.com> wrote:
>
> On Windows, the msvcrt module exposes getch:
> https://docs.python.org/3.7/library/msvcrt.html#msvcrt.getch

I suggest using msvcrt.getwch instead of msvcrt.getch. Both functions
are limited to the basic multilingual plane (BMP, i.e. U+0000 --
U+FFFF), but getch is additionally limited to the console input
codepage. In Windows, getpass.getpass is based on msvcrt.getwch and
msvcrt.putwch.

If you use getch and need the full BMP range, you can temporarily
change the console input codepage to UTF-8 (65001). It's a multibyte
encoding (i.e. 1-3 bytes per BMP code), so the initial getch call has
to be followed by a loop that calls it again while the sequence can't
be decoded and kbhit() is true.
0 new messages