robik / ConsoleD

Because colors are awesome.
63 stars 11 forks source link

Windows: readPassword #17

Open bheads opened 9 years ago

bheads commented 9 years ago

readPassword is returning a string of keys pressed not characters.

entering youHooHoo as a password returns YOU►HOO►HOO

We might want to use _getch for the password input (https://msdn.microsoft.com/en-us/library/078sfkak.aspx)

Unix is working as expected.

bheads commented 9 years ago

Nim's password input ran into this as well https://github.com/Araq/Nim/pull/2137

Will see if I can get a PR today

bheads commented 9 years ago

Testing with this:

int getch(bool echo = false)
    {
        import std.algorithm : among;

        INPUT_RECORD ir;
        DWORD count;
        int result;
        auto m = mode;

        mode = ConsoleInputMode.None;
        scope(exit) mode = m;

        while(ReadConsoleInputW(hInput, &ir, 1, &count)) {
            if(ir.EventType == KEY_EVENT &&
                ir.KeyEvent.bKeyDown &&
                ir.KeyEvent.wVirtualKeyCode.among(VK_SHIFT, VK_MENU, VK_CONTROL) == 0) {
                return ir.KeyEvent.UnicodeChar;
            }
        }

        return '\0';
    }
robik commented 9 years ago

Sorry for this delay and thanks for reporting. Any chance this will be a PR? :)

AntonOks commented 7 years ago

The below code (based on https://github.com/Abscissa/scriptlike/issues/24) works a lot better (NOTE: I use the "original" terminal.d from Adam! and flush the STDOUT to get things printed ;) ):

string readPassword (char mask = '*') { // import terminal; import arsd.terminal; auto term = Terminal(ConsoleOutputType.linear); auto input = RealTimeConsoleInput(&term, ConsoleInputFlags.raw); string pass; dchar ch; ch = input.getch(); while(ch != '\r' && ch != '\n') { import std.range; if(ch == '\b' && !pass.empty) { pass = pass[0..$-1]; write('\b'); stdout.flush; } else { pass ~= ch; write(mask); stdout.flush(); } ch = input.getch(); } return pass; }

adamdruppe commented 7 years ago

On Sun, Mar 05, 2017 at 01:20:28PM -0800, notna123 wrote:

flush the STDOUT to get things printed ;) ):

The getch function should flush for you btw.

AntonOks commented 7 years ago

nope, it doesn't... just tried again and still need the flush ;)

AntonOks commented 7 years ago

fixed in #36

adamdruppe commented 7 years ago

On Sun, Mar 05, 2017 at 01:33:50PM -0800, notna123 wrote:

nope, it doesn't... just tried again and still need the flush ;)

Weird. I see it in the source, it should flush before any events :S

whelp, even if it is a bug I fix later, no big deal to flush twice, if the buffer is empty anyway it will just noop.