From Baidu learned:
This function is a non-echo function, when the user presses a character, the function automatically read, do not have to press ENTER, some C command-line program will use this function to do the game, but this function is not a standard function, pay attention to the portability!
So there is such an interface, it is very good, at least can do a game to play, combined with ASCII code, it is easy to write a key control of the 2048 or snake and so interesting game out.
Here's a simple example:
You will find that when you press the corresponding button, the corresponding statement will be printed.
#include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <conio.h> #define ESC 0x1b# Define Enter0x0d#define SPACE 0x20#define key_up 72//On # define Key_down 80//down #define KEY _left 75//Left #define Key_right 77//Right int key_exit_statu = 0; int key_enter_statu = 0; int key_space_statu = 0; int key_up_statu = 0; int key_down_statu = 0; int key_left_statu = 0; int key_right_statu = 0; Char ch; int get_value (); int main (void) {int i = 0;while (1) {get_value ();} return 0; }int get_value () {ch = getch (); System ("CLS"); switch (CH) {Case Esc:key_exit_statu = 1; printf ("Exit \ n"); break; Case Enter:key_enter_statu = 1; printf ("Enter \ n"); break; Case Space:key_space_statu = 1; printf ("Space \ n"); break; Case Key_up:case ' W ': Key_up_statu = 1;p rintf ("Upper \ n"); Break, Case Key_down:case ' s ': Key_down_statu = 1; printf ("Down \ n"); Break, Case Key_left:case ' a ': Key_left_statu = 1;p rintf ("left \ n") ; Break, Case Key_right:case ' d ': Key_right_statu = 1;p rintf ("Right \ n"); Break;}}
About the Getch () function