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
|
#include <Windows.h>
#include <string>
HANDLE hScreen, hKeyboard;
void clearScreen( HANDLE );
int main(){
hScreen = GetStdHandle( STD_OUTPUT_HANDLE );
hKeyboard = GetStdHandle( STD_INPUT_HANDLE );
const WORD width = 50, height = 40; // width and height of screen
const COORD winSize = { width, height };
CHAR_INFO bgBuff[ width * height ];
SetConsoleScreenBufferSize( hScreen, winSize );
MoveWindow( GetConsoleWindow(), 0,0, 430,550, TRUE );
COORD homeCoords = { width/2, height/2}; // starting home coordinates
DWORD dwWritten;
DWORD numEvents = 0; // How many events happened?
BOOL appRunning = TRUE; // Is the application running?
DWORD numEventsRead = 0; // How many events have we read from the console?
while ( appRunning ){
GetNumberOfConsoleInputEvents( hKeyboard, &numEvents ); // Get all input events that have happened
if ( numEvents > 0 ){ // check if something happened
INPUT_RECORD evBuff; // make an input eventbuffer to hold the events happening
ReadConsoleInput( hKeyboard, &evBuff, numEvents, &numEventsRead ); // Read events
if ( evBuff.EventType == KEY_EVENT ){ // was it a keyboard event?
switch ( evBuff.Event.KeyEvent.wVirtualKeyCode ){
case VK_ESCAPE:
appRunning = FALSE; // set appRunning to false and exit loop
break;
case VK_LEFT:
if ( homeCoords.X > 0 )
homeCoords.X -= 1;
break;
case VK_RIGHT:
if ( homeCoords.X < (width - 1) )
homeCoords.X += 1;
break;
case VK_UP:
if ( homeCoords.Y > 0 )
homeCoords.Y -= 1;
break;
case VK_DOWN:
if ( homeCoords.Y < (height - 1) )
homeCoords.Y += 1;
break;
default:
//CHAR charBuff = ; // get char value
FillConsoleOutputCharacter( hScreen, evBuff.Event.KeyEvent.wVirtualKeyCode, 1, homeCoords, &dwWritten ); // print char to screen
break;
}// END SWITCH
clearScreen( hScreen );
FillConsoleOutputCharacter( hScreen, 'A', 1, homeCoords, &dwWritten ); // print char to screen
//Sleep(5);
}// END IF
}// END IF
}// END WHILE LOOP
//Sleep( 1000 );
clearScreen( hScreen );
return 0;
}
|