Introduction Hello everyone. This little code snippet shows you how to read in scan codes from the keyboard. It is slightly different than reading in a regular character. When you use the getch() function, it is normally returning an ASCII value. Some keyboards have extra keys though. These include the…
Ancient Dragon
Achieved Level 70
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
The only way I know how is to use either termcap library or curses. Unlike MS-Windows, *nix supports a huge variety of monitory, keyboards, and terminals.
Ancient Dragon
Achieved Level 70
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
That code didn't work for me so I modified it. This code will work for any situation that someone needs to use the up arrow, down arrow, left arrow, and right arrow.
The problem with your program is that there is no difference between F1 and a semicolon because they both have the same ascii value. Same with all the other special keys you defined. That's why I added 255 to the ascii value for special keys so that the program would be able to distinguish them from normal keys. Some programmers just make the value of special keys negative, e.g. #defind F1 (-59)
Ancient Dragon
Achieved Level 70
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
That code didn't work for me so I modified it. This code will work for any situation that someone needs to use the up arrow, down arrow, left arrow, and right arrow.
It will? Try it on any Linux Compiler. Try it on any non-Borland/Microsoft compiler. It won't work in most of these situations which means it doesn't work.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
It might, assuming you don't plan to port the program to another operating system and use a compiler that supports functions in conio.h. conio.h and its associated library are non-standard, which means not all compilers will support it. Too bad because conio.h contains several really useful functions, such as kbhit(). But you can replicate that by using win32 api console functions.
Ancient Dragon
Achieved Level 70
32,124 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69