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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <windows.h>
const char ERR_NOT_A_CONSOLE[] =
"You must run this program interactively in a console window.\n";
//-----------------------------------------------------------------------------
HANDLE hstdin;
HANDLE hstdout;
BOOL done = FALSE;
//-----------------------------------------------------------------------------
void write( const char* message )
{
DWORD foo;
WriteConsole(
hstdout,
(const void*)message,
lstrlen( message ),
&foo,
NULL
);
}
//-----------------------------------------------------------------------------
COORD get_cursor_pos()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( hstdout, &csbi );
return csbi.dwCursorPosition;
}
//-----------------------------------------------------------------------------
void set_cursor_pos( COORD pos )
{
SetConsoleCursorPosition( hstdout, pos );
}
//-----------------------------------------------------------------------------
void key_event_handler( KEY_EVENT_RECORD event )
{
if (event.wVirtualKeyCode == VK_ESCAPE) done = TRUE;
}
//-----------------------------------------------------------------------------
void mouse_event_handler( MOUSE_EVENT_RECORD event )
{
char message[ 100 ];
COORD pos;
pos = get_cursor_pos();
wsprintf(
message,
"%3d %3d [%c][%c][%c]",
event.dwMousePosition.X,
event.dwMousePosition.Y,
(event.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED) ? 'x' : ' ',
(event.dwButtonState & FROM_LEFT_2ND_BUTTON_PRESSED) ? 'x' : ' ',
(event.dwButtonState & RIGHTMOST_BUTTON_PRESSED) ? 'x' : ' '
);
write( message );
set_cursor_pos( pos );
}
//-----------------------------------------------------------------------------
int main()
{
DWORD mode;
DWORD count;
INPUT_RECORD console_event;
BOOL blink = TRUE;
// Validate the console
hstdin = GetStdHandle( STD_INPUT_HANDLE );
hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
if (!GetConsoleMode( hstdin, &mode ))
{
WriteFile(
GetStdHandle( STD_ERROR_HANDLE ),
(const void*)ERR_NOT_A_CONSOLE,
lstrlen( ERR_NOT_A_CONSOLE ),
&count,
NULL
);
return 1;
}
// Select raw input and mouse events
SetConsoleMode( hstdin, ENABLE_MOUSE_INPUT );
write( "Press Esc to quit.\n." );
// Main loop
while (!done)
{
if (WaitForSingleObject( hstdin, 350 ) == WAIT_OBJECT_0)
{
if (!ReadConsoleInput( hstdin, &console_event, 1, &count )) continue;
switch (console_event.EventType)
{
case KEY_EVENT:
key_event_handler( console_event.Event.KeyEvent );
break;
case MOUSE_EVENT:
mouse_event_handler( console_event.Event.MouseEvent );
break;
}
}
blink = !blink;
if (blink) write( "\b." );
else write( "\b " );
}
// All done
SetConsoleMode( hstdin, mode );
return 0;
}
|