下面是回避API的方法,響應稍慢:
[ 本帖最后由 TonyDeng 于 2015-3-3 01:51 编辑 ]
程序代码:
#include <stdio.h> #include <stdlib.h> #include <conio.h> const int K_ENTER = 0x0d; const int K_BACKSPACE = 0x08; const int K_SPACE = 0x20; int myGets(char* str, int maxLength); int main(void) { char str[11]; myGets(str, sizeof(str) - 1); puts(str); printf_s("\nPress any key to continue..."); _getch(); return EXIT_SUCCESS; } int myGets(char* str, int maxLength) { const char cursor = '%'; int ch; int index = 0; do { _putch(cursor); _putch(K_BACKSPACE); ch = _getch(); switch (ch) { case K_BACKSPACE: if (index > 0) { _putch(ch); _putch(K_SPACE); _putch(K_SPACE); _putch(K_BACKSPACE); _putch(K_BACKSPACE); --index; } break; case K_ENTER: break; default: _putch(ch); str[index++] = ch; break; } } while ((ch != K_ENTER) && (index < maxLength)); str[index] = '\0'; _putch('\n'); return index; }
[ 本帖最后由 TonyDeng 于 2015-3-3 01:51 编辑 ]
授人以渔,不授人以鱼。