Any way to get keyboard input in c++?

I need keyboard input for my game and have tried conio.h and windows.h, but they don’t work on replit.

Replit runs on linux, so keep that in mind. I’ll see if I can find any packages.

1 Like

This is what I have so far (based off Check keypress in C++ on Linux - Stack Overflow)

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>

int main() {
  struct termios oldSettings, newSettings;

  tcgetattr( fileno( stdin ), &oldSettings );
  newSettings = oldSettings;
  newSettings.c_lflag &= (~ICANON & ~ECHO);
  tcsetattr( fileno( stdin ), TCSANOW, &newSettings );    

  while (true) {
    fd_set set;
    struct timeval tv;

    tv.tv_sec = 10;
    tv.tv_usec = 0;

    FD_ZERO( &set );
    FD_SET( fileno( stdin ), &set );

    int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );
    if( res > 0 ) {
      char c;
      auto no = read( fileno( stdin ), &c, 1 );
      (void) no;
      std::cout << c << std::endl;

  tcsetattr( fileno( stdin ), TCSANOW, &oldSettings );
  return 0;

Note that I am terrible at C++ and you should probably change the code to make it better.

1 Like

Idk what type of input you want to capture, but if you planning on capturing special keys (like the arrow keys for example) you can try use ncurses library.


A BIG warning about ncurse, it used to run super slow in C and python on replit. At least this was few months back when I left (I am now back and will check again).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.