Keyboard, game, ASCII code issues

Source: Internet
Author: User
Tags ack

Beginners love to develop small games, such as snakes and Tetris. To develop such games, they must be controlled by keys. Generally, we use the getch () function to receive keys directly, then convert it to the corresponding ASCII code, and compare it with the ASCII code to determine what key the user is pressing, and then perform the corresponding operations based on the key, such as moving and rotating.

However, I recently discovered that this is not that simple. For example, when a user presses the "upward direction key" for a snake, the user must control the snake to go up. To illustrate the problem, to simplify it, if you press the up arrow key, the output is "up". Other arrow keys are similar. Generally, we will check the ASCII code table (below ),

, The direction key is 38, and then we will write such a small program:


# Include <stdio. h>
# Include <conio. h>
Int main ()
Char ch;
While (true)
Ch = getch ();
If (CH = 38) // The up key
Printf ("up ");
If (CH = 27) // ESC key
Printf ("ESC ");


If you are interested, you can try to compile and run it, and you will find the problem: If you press the "ESC" key, "ESC" will be output, but no matter how you press the upward direction key, never output "up "! What is this? I don't know. Of course, by checking the ASCII code table, we will find that ascii38 corresponds to '&'. If '&' is input, it will output 'up '! Dizzy ...... which of the following gods knows why? I am very grateful to you for replying.

But the problem still needs to be solved. How do I receive the up arrow keys on the keyboard? Later I checked a lot of information and read a lot of examples. I found that if I changed 38 to 72, I could output 'up '! It's amazing. When we look at the characters corresponding to the ASCII code 72, we will find that it is actually 'H'. That is to say, if we input 'h', we can also output 'up ', this is true! That's strange! The 'H' on the keyboard is different from the up arrow key. Isn't it troublesome to press the up arrow key during normal typing, joke. This is a small problem. If there is nothing in a small game like a snake, users generally don't want to press 'h', but what if they are in a large software or game? Maybe this small bug will crash the entire system! Maybe hackers intrude into the system through this bug!

After a problem is solved, another problem arises. How can we avoid the problem above? Meditation for a long time, restless, confused, not thinking about tea, poor spirit, dizzy and swollen ...... finally, today, a burst of inspiration, think of a solution! Haha, Let me go slowly:

I think so. Since it is so troublesome to query the ASCII code table that it cannot be used, why not develop a small program? Press a key and then output the corresponding ASCII code, the code is very simple. If you have learned C, you will write it. I will post the Code:

# Include <stdio. h>
# Include <conio. h>
Int main ()
Char C;
While (1)
C = getch ();
Printf ("% d % x % C \ n", C );


The Code means receiving a keyboard key, outputting the corresponding decimal ASCII code, and then outputting the corresponding hexadecimal ASCII code, finally, output the character corresponding to the button (if displayed, it will be displayed on the screen ). After you run it, you will find the problem, a shocking problem, and an incredible problem. When you press the up arrow key, you will see this)

What's going on? Are you clear? Let's take a closer look. That's right. That's it. The line below is just 72. We have already done the experiment. But what happened to the line above? If we press a key, how can we display two rows? Let's look at the source code again. We should only output one line. What we expect is to output only the one line later. What is the above line? What does output-32 correspond? Why is there a line above? I am so dizzy, I still want to ask you, orz .........

I think so, and I don't know if it's right. Maybe, when you press the up arrow key, you actually press two keys, ah !? I don't understand either. I just guess that the first key is the-32 shown above, and the second key is the real ASCII code. I don't know, right? Anyway, if I press my idea, the problem with the same button can be solved. Check the Code:

# Include <stdio. h>
# Include <conio. h>
Int main ()
Char ch;
While (true)
Ch = getch ();
If (CH =-32)
Ch = getch ();
If (CH = 72) // The up key
Printf ("up ");
If (CH = 27) // ESC key
Printf ("ESC ");


In this case, the 'up' input will not be 'up' output only after the upward direction key is pressed. Of course, we can do this in development, at the very least, there will be no problems, but I still don't understand why. Please give me some advice.





In addition, I think it is quite useful to convert keys into ASCII codes. At least I can quickly get the ASCII values of keys or characters you want. I don't need to check the ASCII code list all day, I have made a small software, in my network disk, if necessary can go to my Network Disk download:




In addition, the ASCII code table is attached. It won't be used even though it is checked. Check it and maybe it can be used.



ASCII Value Control characters ASCII Value Control characters ASCII Value Control characters ASCII Value Control characters
0 Nut 32 (Space) 64 @ 96 ,
1 Soh 33 ! 65 A 97 A
2 STX 34 " 66 B 98 B
3 Etx 35 # 67 C 99 C
4 EOT 36 $ 68 D 100 D
5 Enq 37 % 69 E 101 E
6 ACK 38 & 70 F 102 F
7 Bel 39 , 71 G 103 G
8 BS 40 ( 72 H 104 H
9 HT 41 ) 73 I 105 I
10 Lf 42 * 74 J 106 J
11 Vt 43 + 75 K 107 K
12 FF 44 , 76 L 108 L
13 Cr 45 - 77 M 109 M
14 So 46 . 78 N 110 N
15 Si 47 / 79 O 111 O
16 DLE 48 0 80 P 112 P
17 DCI 49 1 81 Q 113 Q
18 DC2 50 2 82 R 114 R
19 DC3 51 3 83 X 115 S
20 Dc4 52 4 84 T 116 T
21 Nak 53 5 85 U 117 U
22 SYN 54 6 86 V 118 V
23 TB 55 7 87 W 119 W
24 Can 56 8 88 X 120 X
25 Em 57 9 89 Y 121 Y
26 Sub 58 : 90 Z 122 Z
27 ESC 59 ; 91 [ 123 {
28 FS 60 < 92 \ 124 |
29 GS 61 = 93 ] 125 }
30 RS 62 > 94 ^ 126 ~
31 Us 63 ? 95 - 127 Del
Nul VT vertical tabulation SYN idling Synchronization
Soh title starts FF Paper Control Etb Information Group Transfer ended
Start of STX text Cr press ENTER Can void
Etx text ended So shift output Em paper
Eoy transmission ends Si shift Input Sub placement
Enq query characters Dle Space ESC code change
Ack acknowledge DC1 Device Control 1 FS text Separator
Bel alarm DC2 Device Control 2 GS group Separator
BS returns to one grid DC3 Device Control 3 RS record delimiter
HT horizontal list Dc4 Device Control 4 US unit Separator
Lf line feed Nak no Del Delete

Common ASCII codes for keyboards
ESC key vk_escape (27)
Enter the key: vk_return (13)
Tab key: vk_tab (9)
Caps Lock key: vk_capital (20)
Shift key: vk_shift ($10)
CTRL: vk_control (17)
Alt key: vk_menu (18)
Space key: vk_space ($20/32)
Backspace key: vk_back (8)
Left logo key: vk_lwin (91)
Right logo key: vk_lwin (92)
Right-click the shortcut key: vk_apps (93)

Insert key: vk_insert (45)
Home Key: vk_home (36)
Page up: vk_prior (33)
Pagedown: vk_next (34)
End key: vk_end (35)
Delete key: vk_delete (46)

Direction key (Direction): vk_left (37)
Direction key (Direction): vk_up (38)
Direction key (→): vk_right (39)
Direction key (Direction): vk_down (40)

F1 key: vk_f1 (112)
F2 key: vk_f2 (113)
F3 key: vk_f3 (114)
F4 key: vk_f4 (115)
F5 key: vk_f5 (116)
F6 key: vk_f6 (117)
F7 key: vk_f7 (118)
F8: vk_f8 (119)
F9 key: vk_f9 (120)
F10 key: vk_f10 (121)
F11 key: vk_f11 (122)
F12 key: vk_f12 (123)

Num Lock key: vk_numlock (144)
Keypad 0: vk_numpad0 (96)
Keypad 1: vk_numpad0 (97)
Keypad 2: vk_numpad0 (98)
Keypad 3: vk_numpad0 (99)
Keypad 4: vk_numpad0 (100)
Keypad 5: vk_numpad0 (101)
Keypad 6: vk_numpad0 (102)
Keypad 7: vk_numpad0 (103)
Keypad 8: vk_numpad0 (104)
Keypad 9: vk_numpad0 (105)
Keypad.: vk_decimal (110)
Keypad *: vk_multiply (106)
Keypad +: vk_multiply (107)
Keypad-: vk_subtract (109)
Keypad/: vk_divide (111)

Pause Break Key: vk_pause (19)
Scroll Lock key: vk_scroll (145)


Keyboard, game, ASCII code issues

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: 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.