NO

Author Topic: Getting key strokes in c  (Read 5723 times)

nima

  • Guest
Getting key strokes in c
« 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.

Offline Vortex

  • Member
  • *
  • Posts: 867
    • http://www.vortex.masmcode.com
Re: Getting key strokes in c
« Reply #1 on: March 28, 2011, 09:07:39 PM »
You need to process the WM_CHAR message to read keyboard input.
Code it... That's all...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Getting key strokes in c
« Reply #2 on: March 28, 2011, 09:11:19 PM »
if you use console, try _getch(), include conio.h
May the source be with you

nima

  • Guest
Re: Getting key strokes in c
« Reply #3 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.

CommonTater

  • Guest
Re: Getting key strokes in c
« Reply #4 on: March 29, 2011, 05:08:40 AM »
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...

nima

  • Guest
Re: Getting key strokes in c
« Reply #5 on: March 29, 2011, 08:26:49 AM »
Thanks a lot CommonTater.
Problem solved  :)

abhas

  • Guest
Re: Getting key strokes in c
« Reply #6 on: April 09, 2011, 05:00:24 AM »
i am just curious- how to get the second key?

CommonTater

  • Guest
Re: Getting key strokes in c
« Reply #7 on: April 09, 2011, 05:30:45 AM »
i am just curious- how to get the second key?

Call getchar() again.

Code: [Select]
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.

« Last Edit: April 09, 2011, 05:32:57 AM by CommonTater »

abhas

  • Guest
Re: Getting key strokes in c
« Reply #8 on: April 09, 2011, 06:35:19 AM »
 8) ;D o got it.