马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我用getch函数写的,但是不能失去焦点。#include <stdio.h>
#include <conio.h>
void main()
{
int anjian_d = 0;
int anjian_f = 0;
int anjian_j = 0;
int anjian_k = 0;
int anjian_sum = 0;
while(1)
{
if (_kbhit())
{
switch (_getch())
{
case 'd':
case 'D':
anjian_d++;
break;
case 'f':
case 'F':
anjian_f++;
break;
case 'j':
case 'J':
anjian_j++;
break;
case 'k':
case 'K':
anjian_k++;
break;
}
system("cls");
anjian_sum = anjian_d + anjian_f + anjian_j + anjian_k;
printf("d:%d\nf:%d\nj:%d\nk:%d\nsum:%d\n", anjian_d, anjian_f, anjian_j, anjian_k,anjian_sum);
}
}
}
这是我用getkeystate写的,但是如果持续按键他会一直检测到,然后变量会一直在增加。#include <stdio.h>
#include <conio.h>
#include <Windows.h>
void main()
{
int ajd,ajD, ajf, ajj, ajk,ajF,ajJ,ajK;
int anjian_d = 0;
int anjian_f = 0;
int anjian_j = 0;
int anjian_k = 0;
int anjian_sum = 0;
while (1)
{
if (_kbhit())
{
ajd = GetKeyState(100);
ajf = GetKeyState(102);
ajj = GetKeyState(106);
ajk = GetKeyState(107);
ajD = GetKeyState(68);
ajF = GetKeyState(70);
ajJ = GetKeyState(74);
ajK = GetKeyState(75);
if (ajd < 0 || ajD < 0)
{
anjian_d++;
goto end;
}
if (ajf < 0 || ajF < 0)
{
anjian_f++;
goto end;
}
if (ajJ < 0 || ajj < 0)
{
anjian_j++;
goto end;
}
if (ajK < 0 || ajk < 0)
{
anjian_k++;
}
end:
system("cls");
anjian_sum = anjian_d + anjian_f + anjian_j + anjian_k;
printf("d:%d\nf:%d\nj:%d\nk:%d\nsum:%d\n", anjian_d, anjian_f, anjian_j, anjian_k, anjian_sum);
}
}
}
想问问怎么用getkeystate实现和getch一样的效果。 |