1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#if 0
Program displays mouse position (x,y) in terms of character cells as one moves the mouse. In
addition, it displays whether the left or right button has been clicked. This output is on
line #2 in upper left corner of console window. In terms of character output, a keypress is
output on 1st line right above mouse movement displays.
#endif
// cl Test3.cpp /O1 /Os /MT kernel32.lib // 124,416 bytes
// g++ test3.cpp -lkernel32 -oTest3_gcc.exe -mconsole -m64 -s -Os // 43,008 bytes
// cl Test3.cpp /O1 /Os /GS- TCLib.lib kernel32.lib // 4,608 bytes
//#define TCLib
#include <windows.h>
#ifdef TCLib
#include "stdio.h"
#else
#include <stdio.h>
#endif
void cls(HANDLE hStdOut)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD coordScreen = {0,0};
DWORD cCharsWritten;
DWORD dwConSize;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hStdOut,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
GetConsoleScreenBufferInfo(hStdOut,&csbi);
FillConsoleOutputAttribute(hStdOut,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten);
SetConsoleCursorPosition(hStdOut,coordScreen);
return;
}
int main(void)
{
INPUT_RECORD ir[128];
HANDLE hStdInput = NULL;
HANDLE hStdOutput = NULL;
HANDLE hEvent = NULL;
DWORD nRead;
COORD xy;
hStdInput=GetStdHandle(STD_INPUT_HANDLE);
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
cls(hStdOutput);
SetConsoleMode(hStdInput,ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS);
FlushConsoleInputBuffer(hStdInput);
hEvent=CreateEvent(NULL,FALSE,FALSE,NULL); //Event is created non-signaled (3rd param).
HANDLE handles[2] = {hEvent, hStdInput}; //Program loops monitoring two handles. The
while(WaitForMultipleObjects(2,handles,FALSE,INFINITE)) //1st handle ( handles(0) ) is an event which
{ //is initially set to non-signaled. The 2nd
ReadConsoleInput(hStdInput,ir,128,&nRead); //handle monitored by WaitForMultipleObjects()
for(size_t i=0;i<nRead;i++) //is the standard input handle set up to
{ //allow access to mouse/keyboard input. As
switch(ir[i].EventType) //long as neither handle is in a signaled
{ //state, WaitForMultipleObjects() will block
case KEY_EVENT: //in an efficient wait state. If any keypress
if(ir[i].Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE) //or mouse movement occurs, WaitForMultiple
SetEvent(hEvent); //Objects will return TRUE and the input will
else //be read by ReadConsolInput(). If the [ESCAPE]
{ //key is pressed the event object represented
xy.X=0;xy.Y=0; //by hEvent will be set to a signaled state by
SetConsoleCursorPosition(hStdOutput,xy); //the SetEvent() Api function. This will be
printf //picked up by the next WaitForMultipleObjects()
( //call, and the function will return FALSE and
"AsciiCode = %d: symbol = %c\n", //execution will drop out of the while loop
ir[i].Event.KeyEvent.uChar.AsciiChar, //and program termination will occur.
ir[i].Event.KeyEvent.uChar.AsciiChar //It is important to note that if the 3rd
); //parameter to WaitForMultipleObjects() is
} //set to FALSE, the function will return if
break; //either of the handles in the HANDLE array
case MOUSE_EVENT: //represented by handles is signaled.
xy.X=0, xy.Y=1;
SetConsoleCursorPosition(hStdOutput,xy);
printf
(
"%.3d\t%.3d\t%.3d",
ir[i].Event.MouseEvent.dwMousePosition.X,
ir[i].Event.MouseEvent.dwMousePosition.Y,
(int)ir[i].Event.MouseEvent.dwButtonState & 0x07 //mask out scroll wheel, which screws up//output
);
break;
}
}
};
return 0;
}
| |