tags:

views:

63

answers:

1

Hi! 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!

+2  A: 

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().

Dingo