About the Getch () function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.