How to detect key enters?

Hi guys!
I want to make my program clear the screen once I pressed a specified key.
How can I do that?

The following is my idea of code but I don't know whether this is right.
If this is ok, what should be put in the bracket?
Thank you~
1
2
3
4
if ();
{
system("cls");
}


Last edited on
Don't use system ("anything");. It is non-portable, OS-specific code that if you give it to someone else, will cause a compiler error at best, and a computer crash at worst.

There are a couple of other options that are better.

1: Output a whole bunch of newlines:
 
std::cout << std::string ( 100, '\n' );

This will create a bunch of new lines and "hide" your previous outputs.

2: If you have the Curses library, you can use that.
e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ncurses.h>

int main ()
{
	initscr ();

	addstr ("Press enter to quit!");

	refresh ();
	getch ();
	endwin ();

	return 0;
}

You can use that to test and see if you have it. You will need to link to the curses library when compiling: -lncurses.

I don't advise using system() because it is non-portable and IMO shouldn't even be used.

The command inside the parentheses calls directly on the operating system. So, say if you have a Windows computer, and you write code that has a system() command in it, and you give that code to someone who has a Mac, then it will not work on their computer.

If you're just dinking around on your own, system() is fine, but for any code you're going to give to other people, it is bad.

Good luck!
max
I want to make my program clear the screen once I pressed a specified key.

Why? What does your program do.

At the risk of answering an XY Problem, it looks like you're on Windows. Therefore you may call the CRT function _getch, declared in conio.h. The function is platform-specific and obsolete, but it isn't going anywhere.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-16

If you opt for a libcurses derivative like @agent max suggests you'll specifically need to compile PDCurses from source code
https://pdcurses.org/

You can also use the Windows Console API (harder) or virtual terminal control codes (easier)
https://docs.microsoft.com/en-us/windows/console/classic-vs-vt
Last edited on
For Windows, Virtual terminal codes are only supported by Windows 10 starting at build 1511 - not by Windows XP/7/8/8.1

If you're using a supported version of windows then these are the easier way. Also these terminal codes provide much additional console functionality. See https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
I haven't tried it but theoretically you could run another thread that just takes input and applies it to a global variable, then use that variable as an argument in the main program.
Not saying it would be efficient in any way but it would work, I'm new too.
visual studio has several windows tools .. getch, kbhit, events, direct input, and more.
the threading trick would work but its a bit heavy handed. Its portable, though. The problem with trying to thread is a layer of complexity ... the other thread can't ask for user input directly, now it has to parse and handle what the thread reads...
system is a security risk on top of portability. Its ok for school or home toy projects; I use it routinely to make enhanced 'batch' files. Its a tool, a crude one, but sometimes all you need is a rock.
cout << "\033[2J\033[1;1H";
is supposed to clear it too, but its not on all win versions, try it?
Last edited on
seeplus wrote:
For Windows, Virtual terminal codes are only supported by Windows 10 starting at build 1511 - not by Windows XP/7/8/8.1
Topic archived. No new replies allowed.