Thread: multiple keypresses

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    multiple keypresses

    I was wondering....Lets say your making a game and the character can jump up in the air by pressing the Up arrow.

    Then lets say the character can fly by holding down the "J" key...but to move forward, backward, etc. while flying, the user must press the arrow keys while holding down the "J" key.

    How do you handle input of the user holding down more than one key at once? It is easy to check for a certain key being pressed in the main game loop, but how do you check for multiple keys being held down at one time?

    Anybody have any experience with it? Please share....
    My Website

    "Circular logic is good because it is."

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    I'm not sure if you've looked into DirectX or not, but DirectInput can easily handle multiple keypresses.

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    You didn't mention what compiler you're using. If you have access to <windows.h>, look into the GetAsyncKeyState() and other useful keyboard functions in MSDN:

    Code:
    while(GetAsyncKeyState(VK_J)) //if this is true
    {
          if(GetAsyncKeyState(VK_J) && GetAsyncKeyState(VK_UP))
          {
               flyForward(); //or up if its a side-scroller I guess
          }
    
    //and so on...
    
    }
    Hope this helps
    "The mind, like a parachute, only functions when open."

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    this wont actually help you according to your question, but you could also do like -

    player presses J and the fly-mode gets activated, if he then presses the arrow keys he flies to that direction. the next time he presses J the fly-mode gets deactivated and gets back to the ground and then if he uses the arrow keys he walks instead of flies.

    i have used this once so thought about sharing it with you.
    -

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    that's a great question! how do you do that, in DOS? [seriously...]
    hasafraggin shizigishin oppashigger...

  6. #6
    Unregistered
    Guest
    Code:
    interrupt NewInt9Handler(...)
    {
      int keydown=inp(&h60);
      if (keydown & 0x80)
      {
        keys[keydown-128]=0;
      } else keys[keydown]=1;
    
      //Send EOI to the PIC
      outp(0x20,0x20);
    }
    
    
    if (keys[keyscancode])
    {
      //key is down
    } else //key is released
    This is for DOS only and does not check the current state of the keyboard. Different states will need to be masked out of the codes such as NumLock. This will not work when NumLock is on.

    For Windows and MSVC, use DirectX.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That was Bubba up there by the way.



    Cleared my cookies and forgot to sign in. Speaking of cookies...think I'll go and eat one.


  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    interrupt NewInt9Handler(...)


    need any headerfile???

  9. #9
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    yeah bubba, could you post a sample program on how to use multiple keypresses?

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    wait a minute....I think I just figured it out...

    By the way, I am doing this in SDL and using VC++...

    But anyways, when checking for key presses, SDL lets you check for whether a certain key is either KEYDOWN or KEYUP. On KEYDOWN of the J key, I could activate fly mode....so fly mode would be activated while they hold down J, and on KEYUP, I deactivate it....
    My Website

    "Circular logic is good because it is."

  11. #11
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    yeah i know allegro has similar constructs and i'm hoping that there are similar possibilities for this [i guess through interrupts] in DOS...
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  3. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 10:07 PM
  4. Keyboard INT 9 handler (Handles multiple keypresses!) Problem
    By zhopon in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-16-2001, 10:39 AM
  5. Replies: 0
    Last Post: 12-16-2001, 10:36 AM