求个条件循环命令
例子:当我按住鼠标的中键不松时,循环输入键盘ABCABCABC,弹起鼠标中键时,结束ABC的循环
#include <cstdio> #include <windows.h> int main( void ) { HANDLE hInput = GetStdHandle( STD_INPUT_HANDLE ); // 允许控制台输入鼠标信息 DWORD mode_save; GetConsoleMode( hInput, &mode_save ); DWORD mode_new = (mode_save & ~ENABLE_QUICK_EDIT_MODE) | ENABLE_MOUSE_INPUT; SetConsoleMode( hInput, mode_new ); // 检测 鼠标中键 是否按下 DWORD n; for( INPUT_RECORD record; ReadConsoleInput(hInput,&record,1,&n) && n==1; ) { if( record.EventType==MOUSE_EVENT && record.Event.MouseEvent.dwEventFlags==0 && (record.Event.MouseEvent.dwButtonState&FROM_LEFT_2ND_BUTTON_PRESSED)!=0 ) break; } // 检测 鼠标中键 是否释放 size_t loop=0; for( INPUT_RECORD record; PeekConsoleInput(hInput,&record,1,&n); ) { putchar( "ABC"[loop++%3] ); if( n==1 && record.EventType==MOUSE_EVENT && record.Event.MouseEvent.dwEventFlags==0 && (record.Event.MouseEvent.dwButtonState&FROM_LEFT_2ND_BUTTON_PRESSED)==0 ) break; } // 恢复控制台原先的设置 SetConsoleMode( hInput, mode_save ); }
#include <stdio.h> #include <windows.h> typedef char hotkey[256]; int gethotkey(char 组合键, hotkey 输出) { int i, j = 0, state; char key[256] = { 0 }; while (1) { state = GetAsyncKeyState(组合键); if (state == 0 && j == 0) continue; if (state == 0) break; for (i = 1; i < 256; i++) { if (i == 组合键) continue; state = GetAsyncKeyState(i); // 按键第一次按下第0位和第15位值都为1, 按键持续按下第0位值为1第15位值为0, 按键弹上第0位和15位值都为0 if (((state >> 0) & 1) == 1 && ((state >> 15) & 1) == 1) key[j++] = (char)i; if (j >= 256) break; } } memcpy(输出, key, sizeof(hotkey)); return 1; } int main() { printf("%s", "先按下鼠标中键不放再按下其它键再弹上鼠标中键\n"); hotkey key = { 0 }; gethotkey(VK_MBUTTON, key); // 可将鼠标中键设置为任意组合键 printf("热键组合: %s\n", key); return 0; }