2 Replies - 2031 Views - Last Post: 10 February 2012 - 11:52 AM

#1 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

How to use GetAsyncKeyState()

Posted 24 March 2009 - 02:46 PM

Description: Compiled on Windows XP SP2 using Dev-C++ 4.9.9.2A demonstration of using GetAsyncKeyState()
#include <iostream>
#include <windows.h>
using namespace std;

/* Colors defined for SetColor(int) */
enum {
    BLACK = 0,
    DARK_BLUE = 1,
    DARK_GREEN = 2,
    TEAL = 3,
    DARK_RED = 4,
    DARK_PURPLE = 5,
    GOLD = 6,
    GREY = 7,
    DARK_WHITE = 8,
    BLUE = 9,
    GREEN = 10,
    CYAN = 11,
    RED = 12,
    PURPLE = 13,
    YELLOW = 14,
    WHITE = 15
};

void SetColor(const int foreground, const int background) {
    
    int Color = foreground + (background * 16);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);

    return;
}

void PlaceCursor(const int x, const int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD PlaceCursorHere;
    PlaceCursorHere.X = x;
    PlaceCursorHere.Y = y;

    SetConsoleCursorPosition(hConsole, PlaceCursorHere);
    return;
}

int main() {

    int UpArrow = 0;
    int DownArrow = 0;
    int LeftArrow = 0;
    int RightArrow = 0;

    PlaceCursor(15, 5);
    cout << "UP, DOWN, LEFT, RIGHT";

    PlaceCursor(15, 7);
    SetColor(YELLOW, BLACK);
    cout << "Last key(s) pressed: 0, 0, 0, 0";

    while (true) {

        /* Draw the box around: Last key(s) pressed */
        PlaceCursor(14, 6);
        SetColor(WHITE, BLACK);
        cout << "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
        PlaceCursor(14, 7);
        cout << "º";
        PlaceCursor(46, 7);
        cout << "º";
        PlaceCursor(14, 8);
        cout << "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";

        UpArrow = GetAsyncKeyState(VK_UP);
        DownArrow = GetAsyncKeyState(VK_DOWN);
        LeftArrow = GetAsyncKeyState(VK_LEFT);
        RightArrow = GetAsyncKeyState(VK_RIGHT);

        //if (UpArrow == 0 || DownArrow != 0 || LeftArrow != 0 || RightArrow != 0) {
        if (UpArrow == 0) {
                    PlaceCursor(0, 0);
                    SetColor(WHITE, BLACK);
                    cout << "UP ARROW    - ";

                    SetColor(RED, BLACK);
                    cout << "pressedn";

                    PlaceCursor(36, 7);
                    SetColor(RED, BLACK);
                    cout << "0";
        }
        else {
                    PlaceCursor(0, 0);
                    SetColor(WHITE, BLACK);
                    cout << "UP ARROW    - ";

                    SetColor(GREEN, BLACK);
                    cout << "pressedn";

                    PlaceCursor(36, 7);
                    SetColor(GREEN, BLACK);
                    cout << "1";
             }

        //if (DownArrow == 0 || UpArrow != 0 || LeftArrow != 0 || RightArrow != 0) {
        if (DownArrow == 0) {
                    PlaceCursor(0, 1);
                    SetColor(WHITE, BLACK);
                    cout << "DOWN ARROW  - ";

                    SetColor(RED, BLACK);
                    cout << "pressedn";

                    PlaceCursor(39, 7);
                    SetColor(RED, BLACK);
                    cout << "0";
        }
        else {
                    PlaceCursor(0, 1);
                    SetColor(WHITE, BLACK);
                    cout << "DOWN ARROW  - ";

                    SetColor(GREEN, BLACK);
                    cout << "pressedn";

                    PlaceCursor(39, 7);
                    SetColor(GREEN, BLACK);
                    cout << "1";
             }

        //if (LeftArrow == 0 || UpArrow != 0 || DownArrow != 0 || RightArrow != 0) {
        if (LeftArrow == 0) {
                    PlaceCursor(0, 2);
                    SetColor(WHITE, BLACK);
                    cout << "LEFT ARROW  - ";
                      
                    SetColor(RED, BLACK);
                    cout << "pressedn";

                    PlaceCursor(42, 7);
                    SetColor(RED, BLACK);
                    cout << "0";
        }
        else {
                    PlaceCursor(0, 2);
                    SetColor(WHITE, BLACK);
                    cout << "LEFT ARROW  - ";
                      
                    SetColor(GREEN, BLACK);
                    cout << "pressedn";

                    PlaceCursor(42, 7);
                    SetColor(GREEN, BLACK);
                    cout << "1";
             }

        //if (RightArrow == 0 || UpArrow != 0 || DownArrow != 0 || LeftArrow != 0) {
        if (RightArrow == 0) {
                    PlaceCursor(0, 3);
                    SetColor(WHITE, BLACK);
                    cout << "RIGHT ARROW - ";
                         
                    SetColor(RED, BLACK);
                    cout << "pressedn";

                    PlaceCursor(45, 7);
                    SetColor(RED, BLACK);
                    cout << "0";
        }
        else {
                    PlaceCursor(0, 3);
                    SetColor(WHITE, BLACK);
                    cout << "RIGHT ARROW - ";
                         
                    SetColor(GREEN, BLACK);
                    cout << "pressedn";

                    PlaceCursor(45, 7);
                    SetColor(GREEN, BLACK);
                    cout << "1";
             }
               
          
        Sleep(50);
        if (GetAsyncKeyState(VK_ESCAPE)) { exit(0); }
        }

    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to use GetAsyncKeyState()

#2 stev3   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: How to use GetAsyncKeyState()

Posted 09 June 2009 - 12:36 AM

very nice :)
Was This Post Helpful? 0
  • +
  • -

#3 skrtbhtngr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 10-February 12

Re: How to use GetAsyncKeyState()

Posted 10 February 2012 - 11:52 AM

NICE WORK!! AMAZING
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1