db_bridge/console.cpp
changeset 21 403742321c65
parent 20 5fec678f931b
child 23 7acfc5eda7ed
--- a/db_bridge/console.cpp
+++ b/db_bridge/console.cpp
@@ -2,16 +2,29 @@
 #include <cstdio>
 
 #ifdef WIN32
+#include <windows.h>
 #include <conio.h>
+
 int Console::getch ()
 {
-    return::getch ();
+    int pressed = 0;
+    int ch = -1;
+
+    while (!pressed) {
+        if (kbhit ()) {
+            ch =::getch ();
+            pressed = 1;
+        }
+        Sleep (10);
+    }
+    return ch;
 }
 #else
 #include <termios.h>
+
 int Console::getch ()
 {
-    static int ch = -1, fd = 0;
+    int ch = -1, fd = 0;
     struct termios neu, alt;
 
     fd = fileno (stdin);
@@ -31,12 +44,17 @@
     int c;
     char buffer[17];
 
-    while ((c = Console::getch ()) != '\n' && i < 17) {
-        buffer[i++] = c;
-        std::cout << "\b*";
+    while (((c = Console::getch ()) != '\n') && i < 17) {
+        if (c != BS) {
+            buffer[i++] = c;
+            std::cout << "*";
+        } else if (i > 0) {
+            i--;
+            std::cout << "\b \b";
+        }
     }
     buffer[i] = 0;
+    std::cout << std::endl;
 
     password = std::string (buffer);
-    std::cout << std::endl;
 }