Poker-AI.org

Poker AI and Botting Discussion Forum
It is currently Mon Nov 13, 2023 1:26 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Mouse + Keylogger
PostPosted: Wed Sep 18, 2013 3:33 am 
Offline
Veteran Member

Joined: Wed Mar 20, 2013 1:43 am
Posts: 267
Since someone requested a mouseclick logger, I created a mouse and keyboard logger in C++

Code:
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <chrono>
#include <fstream>
#pragma comment(lib, "user32.lib")

std::string path = "C:\\test\\clicks.txt";

HHOOK kHook = NULL;
HHOOK mHook = NULL;

using namespace std;

void UpdateKeyState(BYTE *keystate, int keycode){
   keystate[keycode] = GetKeyState(keycode);
}

void MoveMouse(int x, int y){
   MOUSEINPUT m;
   m.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));//x being coord in pixels
   m.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));//y being coord in pixels
   m.dwExtraInfo = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
   INPUT inPut = {};
   inPut.type = 0;
   inPut.mi = m;
   std::this_thread::sleep_for(std::chrono::milliseconds(2));
   SetCursorPos(x, y);
   SendInput(1, &inPut, sizeof(inPut));
}

LRESULT CALLBACK LowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam){
   //WARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP
   //LPARAM is the key information
   cout << "key pressed";
   KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);
   wchar_t buffer[5];

   //get the keyboard state
   BYTE keyboard_state[256];
   GetKeyboardState(keyboard_state);
   UpdateKeyState(keyboard_state, VK_SHIFT);
   UpdateKeyState(keyboard_state, VK_CAPITAL);
   UpdateKeyState(keyboard_state, VK_CONTROL);
   UpdateKeyState(keyboard_state, VK_MENU);
   //Get keyboard layout
   HKL keyboard_layout = GetKeyboardLayout(0);
   
   //Get the name
   char lpszName [0x100] = {0};

   DWORD dwMsg = 1;
   dwMsg += cKey.scanCode << 16;
   dwMsg += cKey.flags << 24;

   int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName, 255);

   //convert Key Info
   int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer, 4, 0, keyboard_layout);
   buffer[4] = L'\0';

   //print
   char ch[260];
    char DefChar = ' ';
    WideCharToMultiByte(CP_ACP,0,buffer,-1, ch,260,&DefChar, NULL);
   string s = ch;
   cout << "key: " << cKey.vkCode << " " << ch << endl;


   return CallNextHookEx(kHook, nCode, wParam, lParam);
}

void saveClick(std::string path, int x, int y){
  ofstream file;
  file.open (path.c_str(), std::ios_base::app);
  if(file.is_open()){
  file << "X: " << x << " Y: " << y << std::endl;
  file.close();
  }
  else
     std::cout << "unable to open file" << std::endl;
}

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam){
   MSLLHOOKSTRUCT lP = *((MSLLHOOKSTRUCT*)lParam);
   if(wParam == WM_MOUSEMOVE){
      int x = lP.pt.x;
      int y = lP.pt.y;
      cout << "x: " << x << " y: " << y << endl; //comment this out, if you dont want to cout the mouse position every time it moves
   }
   if(wParam == WM_LBUTTONDOWN){
      int x = lP.pt.x;
      int y = lP.pt.y;
      saveClick(path, x, y);
   }
   return CallNextHookEx(mHook, nCode, wParam, lParam);
}





int main(){
   MSG msg;
   kHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyBoardProc, NULL, 0);
   mHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, NULL, 0);
   if(kHook == NULL){
      cout << "KeyBoard-Hook failed!";
   }
   if(mHook == NULL){
      cout << "Mouse-Hook failed!";
   }
   while(GetMessage(&msg, NULL, 0, 0) > 0){
   TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
   return 0;
}


It can print the mouse coordinates to the screen, whenever the mouse moves and it also prints pressed keys. It saves mouseclicks to a txt file. Saving keyboardstrokes to a file should be no problem with the code provided.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group