Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Arrow Keys In C++
JWZ6 (648)



Source: this.

This is an Arrow Keys C++ Tutorial! I shall follow the same concept I did in my previous tutorial: JavaScript Arrow Keys. Let's go!

@27whuang Here's the C++ Arrow Key Tutorial You Wanted!

Table Of Contents

  • Important C++
    • Termios
    • If, Else If, Else
    • Cin
  • Key Codes
  • How To Use The Arrow Keys
  • Summary And Example

Important C++


  • Termios is an h file. For example, you can use cc_t for terminal special characters. If you don't know what Termios is, I suggest you read this article about Termios.h.

If, Else If, Else

  • We use if, else if, else to detect which key is pressed. These loops are more easier to understand. C++ if, else if, else loops are the same as the JavaScript if, else if, else loops. For example, you might have:
if (some_var < 2){
  std::cout << "Yes";
else if (some_var = 2){
  std::cout << "Yay!";
else {
  std::cout << "Yee";


Cin is asking for user input. Our use of Cin is important, as it detects which key you press. For example , you might have:

using namespace std;
cout << "MIMIC!";
string one;
cin >> one;
cout << one;

Key Codes

Up - 65
Down - 66
Right - 67
Left - 68

How To Use The Arrow Keys

We first define our key. We then use termios like addEventListener in JavaScript. Next, we use char. We then use (k==27)&&(e=91) to detect the keys. Next, we apply the key codes. We might have:

#include <iostream>
#include <termios.h>
#define key 1
using namespace std;
int main()
  struct termios term;
  tcgetattr(key, &term);
  term.c_lflag &= ~ICANON;
  tcsetattr(key, TCSANOW, &term);
  cout << "Press The Right Arrow Key: ";
  char k,e,y;
  cin >> k;
  cin >> e;
  cin >> y;
  if ((k==27)&&(e=91)) {
    if (y==65) { 
      cout << "\nYay, you did it!\n";
  else {
    cout << "\nNot The Right Arrow Key\n";

Summary And Example


I hope that you enjoyed this tutorial. If you didn't quite get it, reread and check out the helpful links. If you like and understand this, make sure to updoot!


Here is my example: my example.

That's It For This Tutorial


27whuang (2)

Thx now I can work on my first person shooter :D

Bunnytoes (142)

@JWZ6 This is really good but how to do in c# :/ ?

JWZ6 (648)

@Bunnytoes i dont rly know c# so sorry... C++ is cool and easy, though. I recommend it

27whuang (2)

@JWZ6 lmao im only 10 and im using c++

Bunnytoes (142)

@JWZ6 I was learning it but I decided against it

Bunnytoes (142)

@JWZ6 I know a method but it only works in unity

JWZ6 (648)

@27whuang XD but that means u shouldn't be here...

JWZ6 (648)

@27whuang u have to be 13 or older to be in repltalk so u is nut allowed... 😬

27whuang (2)

@JWZ6 ...uhhhhhhh what is repltalk?

27whuang (2)

@JWZ6 and my teacher told me to use repl...

JWZ6 (648)

@27whuang this... u can be here just don't like comment or something read the rules.

JWZ6 (648)

@27whuang OH that's sad just don't expose urself im gonna try to help you

27whuang (2)

@JWZ6 what is reple talk tho