我想监视给予另一个应用程序的关键事件,每当按下ctrl d时,发送ctrl c,抓取(新)剪贴板内容,并根据它执行其他操作.
我的代码:
void PressKeyboardKey(char key){ INPUT ip; ip.type = INPUT_KEYBOARD; ip.ki.wVk = key; ip.ki.wScan = 0; ip.ki.dwFlags = 0; ip.ki.time = 0; ip.ki.dwExtraInfo = 0; SendInput(1,&ip,sizeof(INPUT)); ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1,sizeof(INPUT)); } LRESULT CALLBACK LowLevelKeyboardProc( int nCode,WPARAM wParam,LPARAM lParam ){ char pressedKey; // Declare a pointer to the KBDLLHOOKSTRUCTdsad KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam; switch( wParam ) { case WM_KEYUP: { pressedKey = (char)pKeyBoard->vkCode; //get the key code if ((pressedKey == -94) || (pressedKey == -93)){ //Ctrl CtrlPressed = FALSE; }else if (pressedKey == 68){ //d if (CtrlPressed && IsTargetApplicationActive()){ PressKeyboardKey(67); //c //Read from Clipboard Sleep(CLIPBOARD_SLEEP_TIME); HANDLE h; if (!OpenClipboard(NULL)){ break; //abort } h = GetClipboardData(CF_TEXT); const char* output = evaluateItem((char*)h); // For now prints the content of the clipboard Sleep(ADDITIONAL_SLEEP_TIME); //Ususally not there,added just for testing CloseClipboard(); ..... }
如果CLIPPBOARD_SLEEP_TIME> = 350(最小值),一切都按预期工作,对于良好的工作流程来说,时间太长了.否则,我只获得旧的剪贴板内容.
我的第一个猜测是,目标应用程序填充剪贴板非常慢,但是以下ahk脚本:
^d:: Send,^c MsgBox,%clipboard%
任何人都知道如何在C中做到这一点 – 我做错了什么?
解决方法
你必须使用SetClipboardViewer(),不要使用Sleep()