[英]Simulate global hotkey
嗨,我正在使用Visual C ++,我有一個可以正常使用的程序,但我希望它能夠使用單獨的屏幕截圖程序。 我已經安裝了單獨的屏幕截圖程序,該程序具有注冊的熱鍵來進行屏幕截圖,我希望我的程序能夠執行此操作。
我用FindWindow嘗試過keybd_event和SendInput,它在記事本上工作正常,但是它的屏幕截圖程序最小化或隱藏運行,我知道SetForegroundWindow的問題。
有沒有辦法讓它像我在所有真正的熱鍵之類的所有窗口上按下鍵盤的Windows系統一樣? 它的截圖程序可以與真實的熱鍵配合使用。
這是全局熱鍵的源代碼。 它已設置為偵聽CTRL + y組合鍵。 觸發CTRL + y后 ,它將抓取屏幕截圖。
要關閉全局熱鍵,只需按CTRL + q即可 。
要隱藏控制台窗口並使熱鍵在后台運行,請按CTRL + w 。
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <iostream>
#include <windows.h>
#include <stdio.h>
HHOOK hKeyboardHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam);
void MessageLoop();
DWORD WINAPI my_HotKey(LPVOID lpParm);
int toggleVisibility = 1;
int screenResolutionX = GetSystemMetrics(SM_CXSCREEN);
int screenResolutionY = GetSystemMetrics(SM_CYSCREEN);
POINT startCoord,endCoord;
void grabScreenshot(POINT a, POINT b);
/*********************************************
*** ***
*** ***
**********************************************/
int main(int argc, char** argv){
/* uncomment to hide console window */
//ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
HANDLE hThread;
DWORD dwThread;
printf("\n s c r e e n s h o t H O T K E Y \n\n");
printf("press CTRL-y for screenshot \n");
printf("press CTRL-w to hide or make console window visible \n");
printf("press CTRL-q to quit \n");
printf("\n\n");
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) my_HotKey, (LPVOID) argv[0], NULL, &dwThread);
if (hThread) return WaitForSingleObject(hThread,INFINITE);
else return 1;
}
/*********************************************
*** ***
*** ***
**********************************************/
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam){
DWORD SHIFT_key=0;
DWORD CTRL_key=0;
DWORD ALT_key=0;
if ( (nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN) ) ) {
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
char lpszKeyName[1024] = {0};
int i = GetKeyNameText(dwMsg, (lpszKeyName+1),0xFF) + 1;
int key = hooked_key.vkCode;
SHIFT_key = GetAsyncKeyState(VK_SHIFT);
CTRL_key = GetAsyncKeyState(VK_CONTROL);
ALT_key = GetAsyncKeyState(VK_MENU);
//printf("%c",key);
if ( (key >= 'A') && (key <= 'Z') || (key >= 'a') && (key <= 'z') || (key >= '0') && (key <= '9') ) {
if (GetAsyncKeyState(VK_SHIFT)>= 0) key +=32;
/*********************************************
*** Hotkey scope ***
*** do stuff here ***
**********************************************/
if ( (CTRL_key !=0) && (key == 'y') || (key == 'Y') ) {
CTRL_key=0;
// grab a screenshot
startCoord.x=0;
startCoord.y=0;
endCoord.x=screenResolutionX;
endCoord.y=screenResolutionY;
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
Sleep(1000);
grabScreenshot(startCoord,endCoord);
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), true);
printf("\nThe Screenshot is in the Clipboard \n\n");
}
//******************************************************
if ( (CTRL_key !=0) && (key == 'q') || (key == 'Q') ) {
MessageBox(NULL, "\n\n\n\nShutting down\n\nPress OK to close\n\n", " H O T K E Y ", MB_OK);
PostQuitMessage(0);
}
//******************************************************
if ( (CTRL_key !=0) && (key == 'w') || (key == 'W') ) {
toggleVisibility = - toggleVisibility;
if (toggleVisibility >0 ) {
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), true);
}
else{
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
}
}
SHIFT_key = 0;CTRL_key = 0; ALT_key = 0;
}
}
return CallNextHookEx(hKeyboardHook, nCode,wParam,lParam);
}
/*********************************************
*** ***
*** ***
**********************************************/
void MessageLoop(){
MSG message;
while (GetMessage(&message,NULL,0,0)){
TranslateMessage( &message );
DispatchMessage( &message );
}
}
/*********************************************
*** ***
*** ***
**********************************************/
DWORD WINAPI my_HotKey(LPVOID lpParm){
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hKeyboardHook = SetWindowsHookEx ( WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent, hInstance, NULL );
MessageLoop();
UnhookWindowsHookEx(hKeyboardHook);
return 0;
}
/*********************************************
*** ***
*** ***
**********************************************/
void grabScreenshot(POINT a, POINT b){
// copy screen to bitmap
HDC hScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, abs(b.x-a.x), abs(b.y-a.y));
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BOOL bRet = BitBlt(hDC, 0, 0, abs(b.x-a.x), abs(b.y-a.y), hScreen, a.x, a.y, SRCCOPY);
// save bitmap to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
// clean up
SelectObject(hDC, old_obj);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.