Ok, no one had an answer to the last question, so I had to research it m'self. Here's the code to get the job done:
Code:
#include <iostream>
#include <conio.h>
#include <windows.h>
void process_mouse( INPUT_RECORD *ir_ptr );
void process_keyboard( INPUT_RECORD *ir_ptr );
void main()
{
HANDLE hStdin;
INPUT_RECORD ir;
ULONG num;
int kbc=0;
textbackground(0);
textcolor(15);
gotoxy(1, 25);
cputs("Press ESC to exit");
hStdin = GetStdHandle( STD_INPUT_HANDLE );
while( ReadConsoleInput( hStdin, &ir, 1, &num ) ){
switch( ir.EventType ){
case MOUSE_EVENT:
process_mouse( &ir );
break;
case KEY_EVENT:
kbc++;
if (kbc == 2){
process_keyboard( &ir );
kbc = 0;
}
break;
}
}
}
void process_mouse( INPUT_RECORD *ir_ptr )
{
int x = ir_ptr->Event.MouseEvent.dwMousePosition.X;
int y = ir_ptr->Event.MouseEvent.dwMousePosition.Y;
textbackground(0);
textcolor(15);
gotoxy(1, 22);
switch( ir_ptr->Event.MouseEvent.dwButtonState ){
case FROM_LEFT_1ST_BUTTON_PRESSED :
cprintf( "Button1 clicked" );
break;
case RIGHTMOST_BUTTON_PRESSED :
cprintf( "Button2 clicked" );
break;
case FROM_LEFT_2ND_BUTTON_PRESSED :
cprintf( "Middle button clicked" );
}
switch( ir_ptr->Event.MouseEvent.dwEventFlags ){
case MOUSE_MOVED :
gotoxy(1, 23);
cprintf( "Mouse X position: %d ", x);
gotoxy(1, 24);
cprintf( "Mouse Y position: %d ", y);
}
}
void process_keyboard( INPUT_RECORD *ir_ptr )
{
WORD kb;
textbackground(0);
textcolor(15);
kb = ir_ptr->Event.KeyEvent.wVirtualKeyCode;
if (kb != VK_ESCAPE){
gotoxy(1, 21);
cprintf("%c was pressed", ir_ptr->Event.KeyEvent.uChar);
}
else
exit(1);
}
The way in which the program is structured, you can input to the console window using either mouse or keyboard input simultaneously. This is perfect for GUIs written using the mouse pointer. A list of Virtual Key Codes should be available in your compiler's documentation that you can use to trap keyboard input.