开发者

Why doesn't scanf work when using the Win32 ConsoleInput function?

This is the example code, I'm using these functions for a program, but scanf doesn't work well: It doesn't display my input and accepts the input only after the enter key is pressed twice.

this is the code:开发者_开发问答

#include <stdio.h>
#include <windows.h>
char c[25];
void KeyEventProc(KEY_EVENT_RECORD);

void KeyEventProc(KEY_EVENT_RECORD ker){
    if(ker.bKeyDown) ; // do stuff
}

main(){
 HANDLE hStdin;
 DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
 INPUT_RECORD irInBuf[128];
 hStdin = GetStdHandle(STD_INPUT_HANDLE);
 GetConsoleMode(hStdin, &fdwSaveOldMode);
 fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
 SetConsoleMode(hStdin, fdwMode);
 ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead);
 for (i = 0; i < cNumRead; i++){
  switch(irInBuf[i].EventType){
   case KEY_EVENT:
    KeyEventProc(irInBuf[i].Event.KeyEvent);
    // do stuff
    break;
   default:
    // do stuff
    break;
  }
 }
 scanf("%s", &c);
 printf("%s", c);
}

thanks for help!


It's not because you're using in the ReadConsoleInput, it's because you changed the console mode. Restore the console mode, by putting SetConsoleMode(hStdin, fdwSaveOldMode); before the call to scanf().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜