Version 10 of Echo-free password entry

Updated 2004-05-17 07:18:05 by ecs

"What can I do so the characters can't be seen when a user types a password?" is a frequent question. Tk applications have a complete and simple answer: use entry's -show option. For a usage example, see A little login dialog.

A pure-Tcl solution is slightly subtler. In a Unix context, the formula is

        exec stty -echo / echo

[elaborate, including signal hygiene ...--which Expect handles by itself]

"stty -echo ..." is an error-prone subject. Don Libes and marsd describe, for example, a long-standing problem that continues to plague Linux 2.4.12 [L1 ].

The Unix Terminal Extension provides the ability to disable echoing, as well as the ability to read one character at a time from stdin. See terminal:password:get for an example.

If you don't mind using the Tcl Windows API extension (which only works on Win NT platforms, not Win 98!), the following should do the trick

   package require twapi
   set console_handle [twapi::GetStdHandle -10]
   set oldmode [twapi::GetConsoleMode $console_handle]
   set newmode [expr {$oldmode & ~4}] ;# Turn off the echo bit
   twapi::SetConsoleMode $console_handle $newmode
   gets stdin password ;#...or do whatever...
   twapi::SetConsoleMode $console_handle $oldmode ;# Restore echo mode

The TWAPI extension is available from http://twapi.sf.net