Best way to detect keypress in C
- Entice
-
Entice
- Member since: Jun. 30, 2008
- Offline.
-
- Send Private Message
- Browse All Posts (16,716)
- Block
-
- Forum Stats
- Member
- Level 13
- Blank Slate
I need to be able to detect key presses/combinations in C, more specifically: Ctrl-C, Ctrl-V, arrow keys, etc.
I read about ncurses, but I want my code to be as "standard"/platform independent as possible. Is there a good way to detect key presses using just the functions in the standard library?
I also read about getchar() but I'm not sure if that's what I'm looking for.
- kiwi-kiwi
-
kiwi-kiwi
- Member since: Mar. 6, 2009
- Offline.
-
- Forum Stats
- Member
- Level 09
- Programmer
Don't really know if there is something like that, what crosses my mind right now is make a standard function that returns a code for key combinations and put the linux implementation in a file, windows implementation in a file etc. then use the preprocessor to define #L for linux #W for windows, something like that. Then when you need to read keyboard combinations you just include the appropriate file.
- Dragonslayerk
-
Dragonslayerk
- Member since: Dec. 4, 2009
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
maybe do a
if(keypress==(key you want){
if(keypress==(other key){
blah,blahblah}
}
That might work.
Gordon Freeman is better than Master Chief. I'm sorry, but he just is.
- Venks
-
Venks
- Member since: Jun. 8, 2008
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
I sure do with there was a preview button{
Cus I'm really just trying to see how this code option works
}
//Sorry for the spam - henke37
-
henke37
- Member since: Sep. 10, 2004
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 1/9/10 11:00 AM, Dragonslayerk wrote: maybe do a
if(keypress==(key you want){
if(keypress==(other key){
blah,blahblah}
}
That might work.
You forgot two things that are kinda important: how keypress is set and what value to use for the comparisons.
Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
There are two ways I have found this setup.
One, is that in my current c # program, Keys are detected by constantly retrieving them from something that detects the keys which are pressed. Which I don't get to see.
The other, is that you can ask the operating system directly to call a method with input from the keyboard.
But you want to be platform independant, so you'll still need a library.
Go figure.



