Created
September 3, 2012 12:41
-
-
Save hirosof/3609082 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 "WaitPressKey.h" | |
| 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; | |
| } | |
| Sleep(1); | |
| }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){ | |
| andendflag = 0; | |
| orendflag = 1; | |
| retkey = *(keycodes + cnt); | |
| break; | |
| }else if(isAND == false && andendflag){ | |
| andendflag = 0; | |
| } | |
| } | |
| if((!orendflag && !andendflag) && ((timeout == 0) || ((timeout>0)&&((int)(timeGetTime() - starttime) >=timeout)))){ | |
| retkey = -2; | |
| break; | |
| } | |
| Sleep(1); | |
| // }while((isAND && !andendflag) || (!isAND && !orendflag)); | |
| }while(!andendflag && !orendflag); | |
| timeEndPeriod(1); | |
| //動的メモリ解放 | |
| delete []keycodes; | |
| return (isAND && retkey!=-2) ? 0 : retkey; | |
| } |
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
| #pragma once | |
| #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,...); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment