/* Arrow Key Handler for C/C++ Console Applications, Created by some_random_dude */
/* Include these pre-processor directives at the start of your main C++ file (main.cpp) */
#include <conio.h>
#define ARROW_UP 72
#define ARROW_DOWN 80
#define ARROW_LEFT 75
#define ARROW_RIGHT 77
/* This is my function. How does it work? Call it in an if statement like this: */
/* if (HandelArroeKeyBy_some_random_dude () == ARROW_UP) {Do stuff here for up arrow key} */
/* Use what ever key you want though */
int HandleArrowKeysBy_some_random_dude ()
{
int iInput;
char cInput;
cInput = getch ();
putchar (cInput);
iInput = cInput;
switch (iInput)
{
case ARROW_UP:return ARROW_UP;break;
case ARROW_DOWN:return ARROW_DOWN;break;
case ARROW_LEFT:return ARROW_LEFT;break;
case ARROW_RIGHT:return ARROW_RIGHT;break;
default:return 0;break;
}
return 0;
}
Rename the function something smaller if you like.
And you could simplify your code, because you just return the value stored in the variable if it is an arrow.
However, what will be the purpose of this function if when you use it, you practically copy-paste all the logic of the function?