ConsoleKeyInfo ReadKeyInternal(out bool fresh) { if (!inited) { Init(); } InitKeys(); object o; if ((o = GetKeyFromBuffer(true)) == null) { do { if (ConsoleDriver.InternalKeyAvailable(150) > 0) { do { AddToBuffer(stdin.Read()); } while (ConsoleDriver.InternalKeyAvailable(0) > 0); } else if (stdin.Peek() != -1) { do { AddToBuffer(stdin.Read()); } while (stdin.Peek() != -1); } else { if ((o = GetKeyFromBuffer(false)) != null) { break; } AddToBuffer(stdin.Read()); } o = GetKeyFromBuffer(true); } while (o == null); // freshly read character fresh = true; } else { // this char was pre-buffered (e.g. not fresh) fresh = false; } return((ConsoleKeyInfo)o); }
void GetCursorPosition() { int row = 0, col = 0; WriteConsole("\x1b[6n"); if (ConsoleDriver.InternalKeyAvailable(1000) <= 0) { noGetPosition = true; return; } int b = stdin.Read(); while (b != '\x1b') { AddToBuffer(b); if (ConsoleDriver.InternalKeyAvailable(100) <= 0) { return; } b = stdin.Read(); } b = stdin.Read(); if (b != '[') { AddToBuffer('\x1b'); AddToBuffer(b); return; } b = stdin.Read(); if (b != ';') { row = b - '0'; b = stdin.Read(); while ((b >= '0') && (b <= '9')) { row = row * 10 + b - '0'; b = stdin.Read(); } // Row/col is 0 based row--; } b = stdin.Read(); if (b != 'R') { col = b - '0'; b = stdin.Read(); while ((b >= '0') && (b <= '9')) { col = col * 10 + b - '0'; b = stdin.Read(); } // Row/col is 0 based col--; } #if DEBUG logger.WriteLine("GetCursorPosition: {0}, {1}", col, row); logger.Flush(); #endif cursorLeft = col; cursorTop = row; }