Created
August 19, 2012 20:10
-
-
Save hirosof/3397361 to your computer and use it in GitHub Desktop.
指定されたキーが押されたら制御をかえす関数(タイムアウト値入力可能)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdarg.h> | |
#include <malloc.h> | |
#include <Windows.h> | |
#pragma comment(lib,"winmm.lib") | |
int WaitPressKey(int,int); | |
int WaitPressKeyEx(int,int,bool,...); | |
int main(void){ | |
int check = 0; | |
printf("何らかのキーを押してください\n"); | |
WaitPressKey(-1,-1); | |
printf("Dキーを押してください\n"); | |
WaitPressKey('D',-1); | |
printf("Alt+Wを押してください\n"); | |
WaitPressKeyEx(2,-1,true,VK_MENU,'W'); | |
printf("Enter(Return)キーかスペースキーを押してください\n"); | |
WaitPressKeyEx(2,-1,false,VK_RETURN,VK_SPACE); | |
printf("Ctrl + Shift + Nを押してください\n"); | |
WaitPressKeyEx(3,-1,true,VK_CONTROL , VK_SHIFT , 'N'); | |
printf("ESCキーかF4キーかバックスペースキーを押してください\n"); | |
WaitPressKeyEx(3,-1,false,VK_ESCAPE,VK_F4,VK_BACK); | |
printf("Ctrl + Shift + Alt + F を押してください\n"); | |
WaitPressKeyEx(4,-1,true,VK_CONTROL , VK_SHIFT , VK_MENU, 'F'); | |
printf("Alt + F2を押してください(タイムアウト:4秒)"); | |
check = WaitPressKeyEx(2,4000,true,VK_MENU,VK_F2); | |
if(check == -2)printf("[timeout]\n"); else printf("\n"); | |
printf("Ctrl + Shift + F5を押してください(タイムアウト:3秒)"); | |
check=WaitPressKeyEx(3,3000,true,VK_CONTROL , VK_SHIFT , VK_F5); | |
if(check == -2)printf("[timeout]\n"); else printf("\n"); | |
return 0; | |
} | |
int WaitPressKey(int keyid,int timeout){ | |
int byteKeyState[256]; | |
int flag = 0 , i , retkey = keyid; | |
int starttime; | |
if(keyid >= 256)return -1; | |
timeBeginPeriod(1); | |
starttime = timeGetTime(); | |
do{ | |
//キーボードの状態を取得 | |
for(i = 0 ; i < 256 ; i++)byteKeyState[i] = GetAsyncKeyState(i) & 0x8000; | |
//確認 | |
if(keyid == -1){ | |
//引数で確認すべきキーコードが-1(全て対象)と指定されてる時 | |
//全てのデータをチェックして押されてるものがあったらflagを1にして | |
//forループを抜ける(OEMのコードは除く) | |
for(i = 0 ; i < 256 ; i++){ | |
if(i>=0xE9 && i<=0xF5 || i==0xE1 || i==0xE3 || i==0xE4 || i==0xE6){ | |
continue; | |
}else if(byteKeyState[i]){ | |
flag = 1; | |
retkey = i; | |
break; | |
} | |
} | |
}else{ | |
//引数で確認すべきキーコードが指定されてる時 | |
if(byteKeyState[keyid]) flag = 1; | |
} | |
if((flag==0) && ((timeout == 0) || ((timeout>0)&&((int)(timeGetTime() - starttime) >=timeout)))){ | |
retkey = -2; | |
break; | |
} | |
}while(flag == 0); | |
timeEndPeriod(1); | |
return retkey; | |
} | |
int WaitPressKeyEx(int NumberOfKeys,int timeout,bool isAND,...){ | |
int byteKeyState, *keycodes , retkey=0 ,cnt ; | |
int andendflag , orendflag=0; | |
va_list dynamic_val; | |
int starttime; | |
if(NumberOfKeys == 0 || NumberOfKeys>256)return -1; | |
//動的メモリ確保 | |
keycodes = new int[NumberOfKeys]; | |
//キーリスト取得開始 | |
va_start(dynamic_val , isAND); | |
//キーリスト取得ループ | |
for(cnt = 0 ; cnt < NumberOfKeys ; cnt++) *(keycodes + cnt) = va_arg(dynamic_val,int); | |
//キーリスト取得完了 | |
va_end(dynamic_val); | |
timeBeginPeriod(1); | |
starttime = timeGetTime(); | |
do{ | |
//キーボードの状態確認ループ | |
for(cnt = 0 , andendflag = 1,orendflag = 0; cnt < NumberOfKeys ; cnt++){ | |
byteKeyState = (GetAsyncKeyState(*(keycodes + cnt)) & 0x8000) >> 15; | |
if(isAND){ | |
andendflag *= byteKeyState ; | |
if(andendflag == 0)break; | |
}else if(byteKeyState){ | |
orendflag = 1; | |
retkey = *(keycodes + cnt); | |
break; | |
} | |
} | |
if((!orendflag && !andendflag) && ((timeout == 0) || ((timeout>0)&&((int)(timeGetTime() - starttime) >=timeout)))){ | |
retkey = -2; | |
break; | |
} | |
}while((isAND && !andendflag) || (!isAND && !orendflag)); | |
timeEndPeriod(1); | |
//動的メモリ解放 | |
delete []keycodes; | |
return (isAND && retkey!=-2) ? 0 : retkey; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment