怎样使用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 << "pressed/n"; PlaceCursor(36, 7); SetColor(RED, BLACK); cout << "0"; } else { PlaceCursor(0, 0); SetColor(WHITE, BLACK); cout << "UP ARROW - "; SetColor(GREEN, BLACK); cout << "pressed/n"; 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 << "pressed/n"; PlaceCursor(39, 7); SetColor(RED, BLACK); cout << "0"; } else { PlaceCursor(0, 1); SetColor(WHITE, BLACK); cout << "DOWN ARROW - "; SetColor(GREEN, BLACK); cout << "pressed/n"; 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 << "pressed/n"; PlaceCursor(42, 7); SetColor(RED, BLACK); cout << "0"; } else { PlaceCursor(0, 2); SetColor(WHITE, BLACK); cout << "LEFT ARROW - "; SetColor(GREEN, BLACK); cout << "pressed/n"; 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 << "pressed/n"; PlaceCursor(45, 7); SetColor(RED, BLACK); cout << "0"; } else { PlaceCursor(0, 3); SetColor(WHITE, BLACK); cout << "RIGHT ARROW - "; SetColor(GREEN, BLACK); cout << "pressed/n"; PlaceCursor(45, 7); SetColor(GREEN, BLACK); cout << "1"; } Sleep(50); if (GetAsyncKeyState(VK_ESCAPE)) { exit(0); } } return 0; }

你可能感兴趣的:(UP,output,colors)