Code:
/* contut071x1.c focus on buffer resize event */
#include <windows.h>
#include <stdio.h>
void PAUSE(void) { while(getc(stdin) != '\n'); }
int stdin_proc(void);
void key_event_proc(KEY_EVENT_RECORD keyrec);
void mouse_event_proc(MOUSE_EVENT_RECORD mrec);
int resize_event_proc(WINDOW_BUFFER_SIZE_RECORD wbuffsizerec );
int main(void)
{
HANDLE hOut1; /* declare a screen buffer pointer */
COORD Outbuff, Pos1; /* struct to hold x and y int values */
SMALL_RECT srDisplay = {0, 0, 100, 42} ;
/*means {srWindow.Left, srWindow.Top, srWindow.Right, srWindow.Bottom } */
FreeConsole(); /* program leaves initial calling console */
AllocConsole(); /* program starts a new console for itself */
SetConsoleTitle("The contutxx.c series of experiments: 07x Reading Keyboard and Mouse input ");
printf(" Testing contut072.c ConsoleMode experiment: Console Input Events \n");
/*** assign to a the main stdout buffer */
hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);
/*** Set the console Screen Buffer Size */
Outbuff.Y = 200; Outbuff.X = 110;
if(! SetConsoleScreenBufferSize(hOut1, Outbuff) )
printf("SetConsoleScreenBufferSize poop \n");
/* move the cursor and do some reporting */
Pos1.Y = 2; Pos1.X = 0;
SetConsoleCursorPosition(hOut1, Pos1);
printf("hOut1 Buffer set to: Y=%d X=%d , ", Outbuff.Y, Outbuff.X);
Sleep(130); /* need this much Sleep time to for screen buffer to set up */
/*** Set the console WINDOW size (srDisplay initialized above) */
if(! SetConsoleWindowInfo(hOut1, TRUE, &srDisplay) )
printf("SetConsoleWindowInfo poop \n");;
printf("'Window' set to: Y=%d X=%d \n",
(srDisplay.Bottom - srDisplay.Top), (srDisplay.Right - srDisplay.Left) );
stdin_proc(); /* the call to read the input */
PAUSE();
printf(" ... THE END ... \n");
PAUSE();
return 0;
}
/********************************************************************/
int stdin_proc(void)
{
HANDLE hStdin;
DWORD fdwMode, fdwOldMode, numread, i;
INPUT_RECORD inputrec[128];
unsigned int coniomode, eventct, tevents;
/* assign this HANDLE to the stdin buffer */
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if(hStdin == INVALID_HANDLE_VALUE)
printf("GetStdHandle poop");
else {
GetConsoleMode(hStdin, &fdwOldMode);
printf("GetConsoleMode(hStdin, &fdwOldMode) fdwOldMode = %lx \n", fdwOldMode );
}
/* Ask user for num of tests and mode */
printf("\nEnter Number of Events to read (say like 20) :");
scanf("%u", &tevents);
PAUSE();
printf("\nEnter 1 to run console in 'RAW' MODE or just ENTER for 'cooked' mode (default): ");
coniomode = getchar();
if(coniomode == 0x31) {
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT ;
if(! SetConsoleMode(hStdin, fdwMode) )
printf("SetConsoleMode poop\n");
else {
printf("\nSet Console input to 'RAW' mode (The program has to handle events.) \n");
printf("fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT ;\n");
printf("SetConsoleMode(hStdin, fdwMode) fdwMode = %lx \n", fdwMode );
}
}
else {
coniomode = 0;
printf("\nLeaving Console input set to 'cooked' mode... events handled by system.\n");
}
printf("\n ok Now we should be ready to try some input reading. \n\
The program is set to report arrow keys and ctrl-x if pressed, \n\
else just boring Input_Record key and mouse member values... \n\n");
if(coniomode == 0x31)
printf("Note: Since console input mode is set to ENABLE_WINDOW_INPUT \n\
ctrl-r should produce a WINDOW_BUFFER_SIZE_EVENT (yeah, right) \n\n");
/* get input events */
eventct = 0;
while(eventct++ < tevents) {
if(! ReadConsoleInput(hStdin, inputrec, 128, &numread) )
printf("ReadConsoleInput poop \n");
else {
printf("----------\n");
for(i = 0; i < numread; i++) {
switch(inputrec[i].EventType) {
case KEY_EVENT:
printf("Event: %2d of %2d is a ", eventct, tevents );
key_event_proc(inputrec[i].Event.KeyEvent);
break;
case MOUSE_EVENT:
printf("Event: %2d of %2d is a ", eventct, tevents );
mouse_event_proc(inputrec[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT:
/* this is for a new buffer size, not window size... */
printf("Event: %2d of %2d is a WINDOW_BUFFER_SIZE_EVENT \n", eventct, tevents );
resize_event_proc(inputrec[i].Event.WindowBufferSizeEvent);
break;
case FOCUS_EVENT:
printf("Event: %2d of %2d is a FOCUS_EVENT \n", eventct, tevents );
break;
case MENU_EVENT:
printf("Event: %2d of %2d is a MENU_EVENT ", eventct, tevents );
break;
default:
printf(" !!! Unknown Event Type !!! \n");
} /* end switch */
} /* end for */
} /* end else */
} /* end while */
SetConsoleMode(hStdin, fdwOldMode);
return 1;
}
/********************************************************************/
void key_event_proc(KEY_EVENT_RECORD keyrec)
{
HANDLE hOut;
COORD Pos;
printf(" KEY_EVENT_RECORD : \n");
printf(" KDown Repeat KeyCode ScanCode Unicode Ascii Ctrl-State \n");
printf(" %3x %3x %3x %3x %3x %3x %3lx \n",
keyrec.bKeyDown, keyrec.wRepeatCount,
keyrec.wVirtualKeyCode, keyrec.wVirtualScanCode,
keyrec.uChar.UnicodeChar, keyrec.uChar.AsciiChar,
keyrec.dwControlKeyState );
if(keyrec.dwControlKeyState == 0x8) {
switch(keyrec.uChar.AsciiChar) {
case 0x18:
printf("ctrl-x found by key_event_proc1()! \n");
break;
case 0x12:
printf("ctrl-r found by key_event_proc1()! DO YOU SEE A WINDOW_BUFFER_SIZE_EVENT ??? \n");
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Pos.Y = 100; Pos.X = 105;
if(! SetConsoleScreenBufferSize(hOut, Pos) )
printf("SetConsoleScreenBufferSize poop \n");
break;
default :
break;
} /* end switch */
} /* end if */
if(keyrec.dwControlKeyState == 0x100) { /* an enhanced key, like arrows */
switch(keyrec.wVirtualScanCode) { /* check scancode for which arrow */
case 0x48:
printf("ScanCode 0x48: up-arrow! \n");
break;
case 0x4b:
printf("ScanCode 0x4b: left-arrow! \n");
break;
case 0x4d:
printf("ScanCode 0x4d: right-arrow! \n");
break;
case 0x50:
printf("ScanCode 0x45: down-arrow! \n");
break;
default :
break;
} /* end switch1 */
switch(keyrec.wVirtualKeyCode) { /* check keycode for which arrow */
case 0x25:
printf("KeyCode 0x25: left-arrow! \n");
break;
case 0x26:
printf("KeyCode 0x26: up-arrow! \n");
break;
case 0x27:
printf("KeyCode 0x27: right-arrow! \n");
break;
case 0x28:
printf("KeyCode 0x28: down-arrow! \n");
break;
default :
break;
} /* end switch2 */
} /* end if */
}
/********************************************************************/
void mouse_event_proc(MOUSE_EVENT_RECORD mrec)
{
printf(" MOUSE_EVENT_RECORD : \n");
printf(" PosY PosX Button-state Event-Flags Ctrl-State \n");
printf(" %3x %3x %3lx %3lx %3lx \n",
mrec.dwMousePosition.Y, mrec.dwMousePosition.X, mrec.dwButtonState,
mrec.dwEventFlags, mrec.dwControlKeyState );
}
/********************************************************************/
int resize_event_proc(WINDOW_BUFFER_SIZE_RECORD wbuffsizerec )
{
printf(" WINDOW_BUFFER_SIZE_RECORD : \n");
printf(" PosY PosX \n");
printf(" %d %d \n", wbuffsizerec.dwSize.Y, wbuffsizerec.dwSize.X);
return 1;
}
What would cause a buffer resize (outside of my program) anyhow?