Hello,
I found this little code which basicly writes any keystroke to the app
window, however, it only works for the app window. I wanted to modify
it to work when any window is focused, just like key logger.
I'm too new to C++ to figure out this by myself.
Any help would be much appreciated.
Thanks
--- CODE ---
// These make the EXE small (Note: May only work in MSVC).... check out
the exe in the zip
// Start Optimization
#pragma comment(linker, "/MERGE:.rdata=.d ata")
#pragma comment(linker, "/MERGE:.reloc=.d ata")
#pragma comment(linker, "/MERGE:.text=.da ta")
#pragma comment(linker, "/IGNORE:4078")
#pragma comment(linker, "/FILEALIGN:16")
#pragma comment(linker, "/ALIGN:16")
#pragma optimize("gsy", on)
// End Optimization
#include <windows.h>
HINSTANCE hInst;
HHOOK hHook = NULL;
HWND hWndMain = NULL;
HWND hWndEdit = NULL;
char szKey[32];
LRESULT CALLBACK KeyboardProc(in t Code, WPARAM wParam, LPARAM lParam)
{
if(lParam & 0x40000000)
{
GetKeyNameText( lParam, szKey, 32);
SendMessage(hWn dMain, WM_USER, 0, 0);
}
return CallNextHookEx( hHook, Code, wParam, lParam);
}
BOOL CALLBACK DlgProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM
lParam)
{
switch(uMessage )
{
case WM_CREATE:
{
memset(szKey, 0, 32);
hHook = SetWindowsHookE x(WH_KEYBOARD, KeyboardProc, hInst,
GetCurrentThrea dId());
if(hHook == NULL){
MessageBox(hWnd , "Cannot load keyboard hook", "Error", MB_OK |
MB_ICONERROR);
return FALSE;
}
RECT rcWindow; GetClientRect(h WndMain, &rcWindow);
hWndEdit = CreateWindow("E dit", "", WS_CHILD | WS_VISIBLE |
ES_MULTILINE | WS_VSCROLL | ES_READONLY,
0, 0, rcWindow.right, rcWindow.bottom , hWndMain, (HMENU)1000,
hInst, 0);
}
return TRUE;
case WM_USER:
SendMessage(hWn dEdit, EM_REPLACESEL, FALSE, (LPARAM)"{");
SendMessage(hWn dEdit, EM_REPLACESEL, FALSE, (LPARAM)szKey);
SendMessage(hWn dEdit, EM_REPLACESEL, FALSE, (LPARAM)"}");
return TRUE;
case WM_CLOSE:
EndDialog(hWnd, 0);
PostQuitMessage (0);
break;
case WM_DESTROY:
UnhookWindowsHo okEx(hHook);
break;
}
return DefDlgProc(hWnd , uMessage, wParam, lParam);
}
int WINAPI WinMain(HINSTAN CE hInstance, HINSTANCE, LPTSTR lpstrCmdLine,
int nCmdShow)
{
MSG Msg;
hInst = hInstance;
hWndMain = CreateWindowEx( WS_EX_APPWINDOW , "#32770", "Press Any Key",
WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE,
((GetSystemMetr ics(SM_CXSCREEN )-400)/2),
((GetSystemMetr ics(SM_CYSCREEN )-300)/2), 400, 300, NULL, NULL, hInst,
0);
if(hWndMain == NULL) return FALSE;
SetWindowLong(h WndMain, GWL_WNDPROC, (LONG)DlgProc);
SendMessage(hWn dMain, WM_CREATE, 0, 0);
while(GetMessag e(&Msg, NULL, 0, 0)) DispatchMessage (&Msg);
return 0;
}
--- END CODE ---