Pelles C forum

C language => Beginner questions => Topic started by: nima on March 28, 2011, 08:16:33 PM

Title: Getting key strokes in c
Post by: nima on March 28, 2011, 08:16:33 PM
hi,
I'm trying to make a game and i need to get keystrokes. I cannot use getchar() because it needs an enter after the character is typed.
Title: Re: Getting key strokes in c
Post by: Vortex on March 28, 2011, 09:07:39 PM
You need to process the WM_CHAR message to read keyboard input.
Title: Re: Getting key strokes in c
Post by: TimoVJL on March 28, 2011, 09:11:19 PM
if you use console, try _getch(), include conio.h
Title: Re: Getting key strokes in c
Post by: nima on March 28, 2011, 10:09:41 PM
Thanks timovjl, _getch() works as I wanted,
but it returns 224 for all of the arrow keys. I can use other keys but is there a way to use arrows.
Title: Re: Getting key strokes in c
Post by: CommonTater on March 29, 2011, 05:08:40 AM
Quote from: nima on March 28, 2011, 10:09:41 PM
Thanks timovjl, _getch() works as I wanted,
but it returns 224 for all of the arrow keys. I can use other keys but is there a way to use arrows.

Some keys return two characters... so you need to check the first to see if it's an "arrow key" then deal with the second character to find out which one it is...

Something to the effect of ... if (first key == 224 ) get second key...
Title: Re: Getting key strokes in c
Post by: nima on March 29, 2011, 08:26:49 AM
Thanks a lot CommonTater.
Problem solved  :)
Title: Re: Getting key strokes in c
Post by: abhas on April 09, 2011, 05:00:24 AM
i am just curious- how to get the second key?
Title: Re: Getting key strokes in c
Post by: CommonTater on April 09, 2011, 05:30:45 AM
Quote from: abhas on April 09, 2011, 05:00:24 AM
i am just curious- how to get the second key?

Call getchar() again.


if (key == 224)
  key = GetChar();


It might be a bit more complex than that, I don't recall offhand if the second character value overlapps the regular key values... but a bit of experimentation will give you the answer.

Title: Re: Getting key strokes in c
Post by: abhas on April 09, 2011, 06:35:19 AM
 8) ;D o got it.