Skip to content

Instantly share code, notes, and snippets.

@L4ZZA
Last active December 10, 2019 17:43
Show Gist options
  • Save L4ZZA/f860e7316e595b3f51a914a66906c0d3 to your computer and use it in GitHub Desktop.
Save L4ZZA/f860e7316e595b3f51a914a66906c0d3 to your computer and use it in GitHub Desktop.
Simple header to quickly get rid of echoing when printing to console. If specified, it prints the int and the int and the char value of the key pressed.
//==========================================//
// Author: Pietro Lazzarotto //
// Credits 2019 //
//==========================================//
#pragma once
#include <cstdio>
#include <conio.h>
#define RELEASED 0
// WARNING: Update the prefix with the first value shown in the output (console),
// if when pressing arrow keys it prints 2 lines.
// [Remember to pass true to the method]
#define ARROW_PREFIX -32
/*
* Reads single key pressed with optional echoing.
* Param: pass true if you want to see the value of the key pressed
* Returns: the char equivalent of the key pressed
*/
static inline char get_char(bool show = false)
{
char ch = _getch();
// overridig arrow keys prefix if found
if (ch == ARROW_PREFIX)
{
ch = _getch();
}
// prints the int value of the key and the char itself
if (show && ch != RELEASED)
{
printf("%d - %c\n", ch, ch);
}
return ch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment