c语言怎么每隔1秒执行一段程序,并保持检测键盘有什么键被按下
最好附上容易移植的伪代码,谢谢
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;
}
热门标签: