detecting two keys at once using getch,switch,case

This is a discussion on detecting two keys at once using getch,switch,case within the A Brief History of Cprogramming.com forums, part of the Community Boards category; i am making a game and i would appreciate it if anyone could tell me how to detect two keys ...

  1. #1
    Unregistered
    Guest

    detecting two keys at once using getch,switch,case

    i am making a game and i would appreciate it if anyone could tell me how to detect two keys being pressed at once impleminting the following code:

    int Choice;
    do
    {
    Choice=getch()
    switch(Choice)
    {
    case 72: //up
    Y--; //coords move up
    }
    putpixel(X,Y);
    }while(Choice!=27); //27 is ascii for <Esc>

    so how would i detect if the user inputs up and left at the same time, so that i could move the pixel up and left?
    thanks for any help

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    133
    I dont believe its possible using getch, gonna have to hack some keyboard buffer **** up in assembly, or you could download allegro and use that for everything you could ever need.

    btw magos, he obviously didnt get an answer with that other post so he tried again, the dumbass who answered it didnt know what he was talking about (or maybey I dont).

  4. #4
    Unregistered
    Guest
    Hey muttski!!

    To do multiple key presses you will have to write an interrupt handler for interrupt 09h. The default interrupt handler installed by the BIOS does not allow for multiple keystrokes. It places them in a circular buffer and then reads from the buffer. If the buffer overflows, it will beep. This is fine for normal use but absolutely horrible for games.

    Code:
    interrupt (*OldInt9Handler)(...)      //use ... for C++ programs
    
    interrupt MyInt9Handler(...)
    {
      int keyhit=inportb(0x60);
      if(keyhit & 0x80)
      {
         keyhit-=128;
      }
      switch Keys[keyhit]
      {
        case 1:  Keys[keyhit]=0;break;
        case 0:  Keys[keyhit]=1;break;
      }
     
    }
    To track them now:
    Code:
    if (Keys[scancode])
    {
      //key with scancode has been pressed
    }
    You cant use a switch statement because switch is for mutually exclusive items. In other words, after you find one item to be true, you do a break which then exits the switch altogether. If you don't break, then all of the next statements will execute until a break is encountered.

    To install it:
    Code:
    OldInt9Handler=getvect(0x09);        //save current vector
    setvect(0x09,MyInt9Handler);          //install new one
    To restore the IVT:
    Code:
    setvect(0x09,OldInt9Handler);         //restore vector to old one

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    ...that Unregistered moron was Bubba...

    Note to self: To increase post counts, one must first login


  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    133
    This guy (bubba) knows what hes doing.

    Im kinda confused on this part:
    Code:
    if(keyhit & 0x80)
    {
       keyhit-=128;
    }
    so your testing to see if that last bit is set, but why then clear that last bit?
    flashdaddee.com rocks!!!

  7. #7
    Unregistered
    Guest
    im kinda confused by the whole thing seeing that i dont know how to use interrup, 0x09(or other #'s), and setvector

    now im not asking for you to explain those, but could you post the url of a free tutorial that explains them?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    133
    Hmmmm, ok heres what you do, go download djgpp and allegro.
    flashdaddee.com rocks!!!

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    133
    Can someone answer my question, why clear that bit?
    flashdaddee.com rocks!!!

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You clear the bit because when a key is held down the port returns the key+128 or 0x80. So to make it just toggle a flag on and off, you subtract 128 from the held down value. Otherwise you will never toggle the correct key flag, you will only be placing ones at key+128 and never clearing the flag at the actual key scan code in the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ToUnicodeEx and dead keys inside system wide hooks
    By RevengerPT in forum Windows Programming
    Replies: 1
    Last Post: 08-13-2009, 02:51 PM
  2. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 07:14 PM
  3. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM
  4. Pausing for input, with kbhit() still detecting keys
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 05:04 AM
  5. detecting two keys at once using getch,switch,case
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 05-11-2002, 12:03 PM

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