【发布时间】:2013-01-02 12:16:25
【问题描述】:
我正在使用 Visual Studio 2010,当用户按下键盘上的右数组键时,我正在尝试移动光标:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h,c);
}
int main()
{
int Keys;
int poz_x = 1;
int poz_y = 1;
gotoxy(poz_x,poz_y);
while(true)
{
fflush(stdin);
Keys = getch();
if (Keys == 77)
gotoxy(poz_x+1,poz_y);
}
cin.get();
return 0;
}
它工作但只有一次 - 第二,第三等按下不工作。
【问题讨论】:
-
fflush(stdin);- 不要那样做。我不想忍受因为你的代码中的一行而自燃的可能性。
标签: c++ winapi console-application keyboard-events