Skip to content

Instantly share code, notes, and snippets.

@soachishti
Created February 4, 2015 19:08
Show Gist options
  • Save soachishti/229a9f344932cad8f540 to your computer and use it in GitHub Desktop.
Save soachishti/229a9f344932cad8f540 to your computer and use it in GitHub Desktop.
Move in console by pressing right and left
/*
PRESS RIGHT LEFT BUTTON AND SEE RESULT
*/
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
/*
SET CURSOR POSITION FROM CURRENT POSITION TO FORWARD OR BACKWARD
*/
void setCurPosFromCurrPos(int x = 0,int y = 0){
//GET CURRENT POSITION
CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleinfo);
int cursorx = consoleinfo.dwCursorPosition.X;
int cursory = consoleinfo.dwCursorPosition.Y;
//SET POSITION
COORD p = {cursorx + x,cursory + y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
}
void main() {
int ch;
cout << "Syed Owais Ali Chishti";
while (ch = _getch()) {
if (ch == 8){
cout << "\b";
}
else if (ch == 224) {
ch = _getch();
if (ch == 75) { //LEFT
setCurPosFromCurrPos(-1);
}
if (ch == 77) { //RIGHT
setCurPosFromCurrPos(1);
}
cout << "";
}
else {
cout << (char)ch;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment