首页 > 电脑

c语言怎么每隔1秒执行一段程序,并保持检测键盘有什么键被按下

更新时间2019-09-01 07:59:17

最好附上容易移植的伪代码,谢谢

c语言的定时与键盘与系统或平台有关,不存在容易移值(如单片机的定时与键盘程序肯定不能用于windows的)

以下是Windows的一个程序 ,每秒定时,并判按键,ESC退出程序

#include <conio.h>
#include <windows.h>
#include <stdio.h>
static BOOL bExitApp = FALSE;
const  UINT uiTimerID = 10;
int ct=0;
VOID CALLBACK FooTimerFun( HWND h, UINT u , UINT u1, Dword  d)
{
char c;
printf("%d second ",ct++);
if (kbhit()) {
 c=getch();
 if (c==27)
  bExitApp=TRUE;
 printf("%d key get ",c);

}
}
int main()
{
MSG msgFoo;
SetTimer( NULL , uiTimerID , 1000 , FooTimerFun );
while( !bExitApp && GetMessage( &msgFoo , NULL , 0 , 0 ) ) {
 TranslateMessage( &msgFoo );
 DispatchMessage( &msgFoo );
}
KillTimer( NULL , uiTimerID );
return 0;
}


相关标签:c语言

上一篇:在String对象中,如何得到一个指定字符的下标

下一篇:MasterCAMx5程序如何转化为几何图形