Skip to content

Instantly share code, notes, and snippets.

@Alynva
Created February 10, 2017 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alynva/8421083534af67c6e7e080d286add568 to your computer and use it in GitHub Desktop.
Save Alynva/8421083534af67c6e7e080d286add568 to your computer and use it in GitHub Desktop.
Exemplifies the usage of a mouse in console environment. Purely coded in C using gcc compiler (MinGW, to be more precise)
#include <windows.h>
#define MAXCONSOLEINPUTS 20
//This is the input module. It can handle not only Mouse but keyboard also.
HANDLE rHnd, wHnd; //Handles for reading and writing to the console.
//The Mouse Handle function. Or to be more precise, the pointer to the function
int (*handler) (int x, int y, int button,int state);
int initializecontrols();
int addmousehandler(int (*func) (int x, int y, int button,int state));
int domouse();
int initializecontrols() {
//This module initializes the reading and writing handles.
//It must be called before calling any of other functions that make use of
//console input and output handles
rHnd = GetStdHandle(STD_INPUT_HANDLE);
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
if (rHnd == NULL || wHnd == NULL) return FALSE;
return TRUE;
}
int addmousehandler(int (*func) (int x, int y, int button,int state)) {
if (func) //If a valid function is passed
handler = func;
else return FALSE;
return TRUE;
}
int domouse() {
//This function should be called to receive the events present in the console buffer
//returns nothing but calles the active handler with the event data as parameters
long int numEvents =0,i;
INPUT_RECORD ConsoleInputs[MAXCONSOLEINPUTS];
int mouseX=0, mouseY=0,mouseButton=0,controlState=0;
GetNumberOfConsoleInputEvents (rHnd, &numEvents);
//return if nothing good has happened (i.e. no events)
if (numEvents == 0) return FALSE;
ReadConsoleInput (rHnd,ConsoleInputs,numEvents,&numEvents);
//Send each event to its respective handler
for (i =0; i<numEvents; i++) {
switch (ConsoleInputs[i].EventType) {
case MOUSE_EVENT:
mouseX =ConsoleInputs[i].Event.MouseEvent.dwMousePosition.X;
mouseY =ConsoleInputs[i].Event.MouseEvent.dwMousePosition.Y;
controlState = ConsoleInputs[i].Event.MouseEvent.dwControlKeyState;
mouseButton = ConsoleInputs[i].Event.MouseEvent.dwButtonState;
(*handler) (mouseX,mouseY,mouseButton,controlState);
break;
case KEY_EVENT:
//You can add your own handler here
break;
}
}
return TRUE;
}
#include <stdio.h>
#include <windows.h>
#include "INPUT.c"
extern HANDLE rHnd,wHnd; //Declared in Input.c
//This function is our mouse handler.
int mousehandler (int x, int y, int button, int state);
void main() {
if (!initializecontrols()) { //must call before doing anything
printf ("ERROR WHILE INITIALIZING\n"); //if couldn't initialize
exit(0);
}
addmousehandler(mousehandler); //adds our handler. Only one handler at a time
while (!kbhit()) //Run until any key is pressed
domouse();
}
void locate(int x, int y) {
COORD pos = {x,y};
SetConsoleCursorPosition (wHnd,pos);
}
void clearconsole () {
int num;
//Basically fill the console with a space ' '
FillConsoleOutputCharacter (wHnd,' ',80*80, (COORD) {0,0},&num);
locate(0,0);
}
int mousehandler (int x, int y, int button, int state) {
clearconsole(); //Clears the console by filling with spaces.
locate (0,0); //Locates the cursor at specified coordinate;
printf ("Move around and note things change. Press any key to exit\n");
printf ("X= %2d \t Y= %2d\n",x,y);
/*Notes Start
Button = 0 No keys pressed
Button = 1 Leftmost key pressed
Button = 2 Rightmost key pressed
Button = 4 From left first key pressed
*/
printf ("Button = %d\n", button);
/* The space parameter specifies which toggle keys were active and which not*/
if (state & NUMLOCK_ON)
printf ("Num Lock Pressed\n");
if (state & CAPSLOCK_ON)
printf ("Caps Lock Pressed\n");
if (state & SCROLLLOCK_ON)
printf ("Scroll Lock Pressed\n");
return 1; //Can also be written as return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment