2015-11-25 2 views
0

Salut J'utilise Visual C++ J'ai un programme de travail SAUF pour une chose que je veux pouvoir utiliser un programme de capture d'écran séparé. J'ai un programme de capture d'écran séparé installé le programme ont un raccourci enregistré pour prendre une capture d'écran, je veux que mon programme le fasse.Simuler la touche de raccourci globale

J'ai essayé keybd_event et SendInput avec FindWindow et cela fonctionne très bien avec le bloc-notes, mais le programme de capture d'écran s'exécute minimisé ou caché et je connais le problème avec SetForegroundWindow.

Existe-t-il un moyen de faire en sorte que le système Windows que je viens de presser sur le clavier pour toutes les fenêtres comme réel hotkey? Ce programme de capture d'écran fonctionne très bien avec un raccourci réel.

Répondre

1

Peut-être que le crochet du clavier résoudra le problème?

pas parfait, mais ils sont assez globale

+0

Ok Je peux en apprendre davantage sur le hook mais est-ce que cela peut mettre une touche enfoncée ou simplement écouter? C'est un autre programme qui a un raccourci clavier, je veux que mon programme le presse. – pomodo01

+0

Il semble que je l'ai eu. Vous voulez simuler une frappe, de sorte que le programme externe prend la capture d'écran, non? Eh bien, ce n'est pas le raccourci clavier avec lequel vous avez des problèmes. Par ailleurs, considérer les points suivants: * Prendre Sreenshot à partir de votre propre code est assez facile * Vous pouvez combiner votre propre raccourci clavier avec une sorte de tuyau appel au programme mentionné Mais de toute façon, la solution à ce que vous Voulez-vous maintenant est [SendInput()] (https://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput). Ouais, vous l'avez essayé, mais ça devrait marcher s'il est correctement implémenté. – MasterAler

0

enter image description here

Voici un code source pour un raccourci clavier global. Il a été configuré pour écouter une combinaison de touches CTRL + y. Une fois que CTRL + et est déclenchée, il saisit une capture d'écran.

Pour fermer le raccourci clavier global, appuyez simplement sur CTRL +q.

Pour masquer la fenêtre de console et gardez la touche de raccourci en cours d'exécution dans la presse d'arrière-plan 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); 
}