Be a Supporter!

Best way to detect keypress in C

  • 1,745 Views
  • 5 Replies
New Topic Respond to this Topic
Entice
Entice
  • Member since: Jun. 30, 2008
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Best way to detect keypress in C 2010-01-08 21:11:12 Reply

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
Response to Best way to detect keypress in C 2010-01-09 02:13:47 Reply

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
Response to Best way to detect keypress in C 2010-01-09 11:00:43 Reply

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.

BBS Signature
Venks
Venks
  • Member since: Jun. 8, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Best way to detect keypress in C 2010-01-12 23:39:43 Reply

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

BBS Signature
henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Best way to detect keypress in C 2010-01-13 04:12:57 Reply

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
Response to Best way to detect keypress in C 2010-01-14 01:02:47 Reply

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.