| | |
how to detect arrow keys??
![]() |
Try this simplest program, look at these codes on your screen:
Special keys are presented by two codes, as usually the 1st is 0 (zero).
So you need two getch call to accept these keys.
Take a pen and a piece of paper, start the program then press your favorite keys...
This program works not only with unfading Turbo C...
Good luck!
C Syntax (Toggle Plain Text)
#include <stdio.h" #include <conio.h> /* Press Esc to quit */ #define ESC 27 int main() { int ch; while ((ch=getch()) != ESC) { printf("%d",ch); while (kbhit()) { printf(" %d",getch()); } printf("\n"); } printf("ESC %d\n",ch); return 0; }
So you need two getch call to accept these keys.
Take a pen and a piece of paper, start the program then press your favorite keys...
This program works not only with unfading Turbo C...
Good luck!
•
•
Join Date: Sep 2006
Posts: 327
Reputation:
Solved Threads: 22
This program only works in Turbo C.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> int main(void) { unsigned char key, key2; while( (key=getch()) != '\r' ) { if (key == 0) { key2 = getch(); switch(key2) { case 72: printf("up "); break; case 75: printf("left "); break; case 77: printf("right "); break; case 80: printf("down "); break; default: break; } } } return 0; }
Last edited by Colin Mac; Aug 5th, 2008 at 10:03 am.
Look at this wonderful table:
http://www.jimprice.com/jim-asc.shtml#keycodes
Google is your friend
...
http://www.jimprice.com/jim-asc.shtml#keycodes
Google is your friend
... Actually, if you need to simply accept input, you don't need
kbhit() at all: c Syntax (Toggle Plain Text)
c2 = 0; // reset c2 to 'nothing' c1 = getch(); // wait for a key if (c1 == 0) // check if it's a function key { c2 = getch(); // it is, get the function value } // if c2 is not 0, a function key was pressed
kbhit() is useful when you want the program to continuously run and when a key is pressed do something different. For example: c Syntax (Toggle Plain Text)
if (kbhit()) // check to see if a key was pressed { // a key was pressed so we interrupt the standard // programming cycle c2 = 0; // reset c2 to 'nothing' c1 = getch(); if (c1 == 0) { c2 = getch(); } // if c2 is not 0, a function key was pressed ... }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
I did, and don't see why you need a
Simple without the
kbhit() for that. A simple test for 0 would work.•
•
•
•
... then try to print the second byte of a special key without kbhit() at the same line...
kbhit() . Look at my code and make the simple change... The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- How do you check if arrow keys are pressed? (C)
- How do you detect if arrow keys and the escape ke has been pressed in Dev-C++? (C++)
Other Threads in the C Forum
- Previous Thread: shell programming using C
- Next Thread: Help with memory block usage - C
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks bash binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






