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 Game Programming forums, part of the General Programming 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
    Registered User
    Join Date
    May 2002
    Posts
    132
    Its been a very long time since I've had to do this, but I believe you can do it by checking for first the up key, and then check for either the left or right key. Then check for the down key and check for the right or left key with the following code:

    int Choice;
    int x,y;
    do {
    Choice = getch();
    switch(Choice)
    {
    case 72:
    Choice = getch();
    // they want to go up and to the left
    if(Choice == <place number for left key here>) {
    y--;
    x--;
    }
    // they want to go up and to the right
    else if(Choice == <place number for right key here>) {
    y--;
    x++;
    }
    // they only want to go up
    else {
    y--;
    }
    break;
    case <number for down key here>:
    Choice = getch();
    // they want to go down and to the left
    if(Choice == <place number for left key here>) {
    y++;
    x--;
    }
    // they want to go down and to the right
    else if(Choice == <place number for right key here>) {
    y++;
    x++;
    }
    // they only want to go down
    else {
    y++;
    }
    break;
    }
    putpixel(x, y);
    }
    while(Choice != 27);

    And that should do it. Try it. If it doesn't work, then post a reply and I'll try another solution with you.

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, 03:51 PM
  2. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 08:14 PM
  3. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 03: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, 06:04 AM
  5. detecting two keys at once using getch,switch,case
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-15-2002, 12:54 AM

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