| | |
How do you check if arrow keys are pressed?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
What is the top decimal value that kbhit() catches when you hit a key? I didn't know about kbhit. So I coded the entire thing from scratch. It works. But the UP arrow key for example is " H", that is: NULL, capital h. It is actually 2 characters. Does kbhit() catch 2 characters? And could you provide a sample program that prints on the screen that an arrow key was hit, like I coded my sample program? I am curious about this apparent quick way that you provided.
kbhit() doesn't catch characters. It returns TRUE if a key has been pressed, FALSE if no key is waiting. That way your program can do whatever processing it needs and not wait for a key. In effect it makes the keyboard interrupt driven. Leave out the kbhit() call if you don't need to test for keyboard readyness.As for a sample program, just output the value of the
integers ch1 and ch2. 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
diode, I ran your code in Dev-C++, and I found one error,
Also, when I ran it, if I press an arrow key, it displays a lowercase alpha, then the letter you defined. It doesn't detect that an arrow key was pressed (it doesn't say, "You pressed the [insert key here] key!"). I don't know, maybe Dev-C++ does something different. I haven't attempted to look through all of the code yet; it's a little late (early?) and I am not that great at C++ yet.
[EDIT] Wait! you used a lot of c libraries, no wonder, I haven't dealt with those at all... [/EDIT]
Anyways, I was hoping maybe you could explain this error I encountered.
last: didn't have anything happen, so I put in a nonsense integer declaration.Also, when I ran it, if I press an arrow key, it displays a lowercase alpha, then the letter you defined. It doesn't detect that an arrow key was pressed (it doesn't say, "You pressed the [insert key here] key!"). I don't know, maybe Dev-C++ does something different. I haven't attempted to look through all of the code yet; it's a little late (early?) and I am not that great at C++ yet.
[EDIT] Wait! you used a lot of c libraries, no wonder, I haven't dealt with those at all... [/EDIT]
Anyways, I was hoping maybe you could explain this error I encountered.
Damn computer! It ate everything!
•
•
•
•
diode, I ran your code in Dev-C++, and I found one error,last:didn't have anything happen, so I put in a nonsense integer declaration.
Also, when I ran it, if I press an arrow key, it displays a lowercase alpha, then the letter you defined. It doesn't detect that an arrow key was pressed (it doesn't say, "You pressed the [insert key here] key!"). I don't know, maybe Dev-C++ does something different. I haven't attempted to look through all of the code yet; it's a little late (early?) and I am not that great at C++ yet.
[EDIT] Wait! you used a lot of c libraries, no wonder, I haven't dealt with those at all... [/EDIT]
Anyways, I was hoping maybe you could explain this error I encountered.
BTW, about your sig, I didn't know Ozzy Osbourne said that, but that is also on a Magic: the Gathering card, though I forget which one.
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
I usually do it with this:
You can use this to discover what the key values are.C Syntax (Toggle Plain Text)
if (kbhit()) { ch1 = getch(); if (ch1 != '\0') { // process a normal keystroke } else { ch2 = getch(); // get the arrow or function key switch(ch2) { // process the arrow or function key } } }
This only works for compilers that havegetch()andkbhit()defined, of course.
getch() returns 0 when one of the special keys is pressed, such as one of the Function or arrow keys. When that happens the program has to call getch() again which will return the key code of the key that was pressed. The reason for this behavior is that special keys have the same key codes as other normal keys, and the program needs some way to distinguish them. When I write such programs I normally make those key codes negative values so that the rest of the program can easily distintuish between normal and special keys. Other programmers have added 255 to the value, which works ok too.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Please help me in libpng1.2.33
- Next Thread: Split / explode-like function
Views: 10667 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






