C language uses getch () to read direction keys

Time:2021-10-22

Original intention:

When solving the n queen problem, you need to use the direction keys to switch the layout, so you find information on the Internet, feel that you have gained a lot, and record some knowledge points you have summarized.

Summary:

1. Getch() needs to read characters once.

2. Getch() needs to read the direction key or function key twice.

(the first return value is 0 or 224 (direction key)
When the getch function reads a function key or arrow (direction) keyboard, the function will return twice. The first call returns 0 or 0xe0, and the second call returns the actual key value.

The return values of 72, 80, 75 and 77 are ↑↓←→, respectively.

Code demonstration:

While (1) {// infinite loop
        //Use getch () to read the direction key, which needs to be read twice
          if (ch1=getch()){
             ch2=getch();// The first call to getch() returns a value of 224
             Switch (CH2) {// call getch() for the second time
             Case 75: ascll of {//←
                 if(current-1>=0){
                    current--;
                 }else{
                     current = tol-1;
                 }
                    system("cls\n");
                    Cout < < "please enter the number of Queens:" < < n < < endl;
                    Cout < < total < < tol < < species layout < < endl < < endl;
                    show(current);
                break;
             }
             case 77: {
                 if(current+1<=tol-1){
                    current++;
                 }else{
                     current = 0;
                 }
                    system("cls\n");
                    Cout < < "please enter the number of Queens:" < < n < < endl;
                    Cout < < total < < tol < < species layout < < endl < < endl;
                    show(current);

                 break;
             }
             Default: cout < < input error! "< < endl; break;

             }
          }

This is the end of this article about reading the direction keys in C language using getch(). For more information about reading the direction keys in C language, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!