A key press funtion

This is a discussion on A key press funtion within the Game Programming forums, part of the General Programming Boards category; hi there, im making a small game which uses to getch() for key press the code is as follows Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    6

    A key press funtion

    hi there,

    im making a small game which uses to getch() for key press

    the code is as follows

    Code:
    if(getch() == KEY_DOWN)
    {
     move object down
    }
    if(getch() == KEY_UP)
    {
    move object up
    }
    the problem is when the code reaches getch() the program doesnt move onrward until u actually press something.

    is there a similar function that gets ur key press but doesn't cause the program to loop on the function until a key is pressed?

    ty.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    You used to use kbhit() to detect if a key had been pressed. Then if one had you would use getch to get the keycode of the key that was pressed.

    However inside of Windows this is different. It's also different inside of DirectInput which IMO is much closer to the old school interrupt 09h keyboard interrupt handlers than anything.

    Inside of DirectInput you can either choose polling mode or buffered mode. Polling mode is usually used for real-time games. What happens is when you init DirectInput you give it a 256 char array each representing a specific keypress. During a poll you first tell DirectInput to update which causes it to fill the keystate buffer with the correct data. If keystate[key_code] is non-zero then the key is down, else it is up. So checking keypresses becomes:

    Code:
    if (keystate[key_code])
    {
       ..Do something
    }
    And for extended keys or key combos

    Code:
    if (keystate[key_code] && keystate[DK_LCTRL))
    {
       ..Do something
    }
    This is how most keyboard handlers work which is why I showed you the basics of it.

    For your situation and provided you have the correct headers:

    Code:
    int hit = kbhit();
    
    if (hit)
    {
      int ch = getch();
    
      if (ch == key_code)
      {
         ..do something
       }
    }
    However this code is old, deprecated, not completely valid under XP or protected mode, and still won't work perfect since it relies on the OEM interrupt 09h handler which basically sucks.

    You can also grab data from port 60h however if you don't do some housekeeping you will soon encounter a keyboard buffer overflow which causes a very annoying beeping to occur. Please note that both port 60h and kbhit() or any combination therein will NEVER produce a robust keyboard handler. The ONLY way to have robust keyboard handler is to write a new interrupt 09h keyboard handler. The default BIOS implemented 09h keyboard handler is not designed for time critical keyboard functionality. It is also extremely cryptic when it comes to multiple keypresses and the like. It is essentially useless for games. Also under Windows XP it is possible that Windows has hooked this already and reimplemented their own. In short you cannot rely on kbhit(), getch(), or port 60h in Windows XP or in a Windows XP DOS session.

    Furthermore since interrupts are not allowed in Windows and since Windows would frown on you trying to reimplement the 09h handler your best bet is to use Windows built-in messaging scheme for keyboard handling or use DirectInput. DirectInput is far better than Win32 but with the caveat that you must know how to use it.
    Last edited by VirtualAce; 08-08-2008 at 11:04 PM.

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    You could use the kbhit function to test to see if a key has been hit and you could then use a switch on a single getche to do the job.

    Code:
    if (kbhit()) //see if key has been hit
    {
        switch (getche()) //check to see what key was hit
        {
            case 'a': //if the key was 'a'
                //...
                break;
            //etc...
        }
    }

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    A switch implies keys are mutually exclusive. That is it implies that only one key press at a time can be handled and no key combos are supported. This is a bad idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21