Need some help with this capturing keys

T

tmarkovski

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=.data")
#pragma comment(linker, "/MERGE:.reloc=.data")
#pragma comment(linker, "/MERGE:.text=.data")
#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(int Code, WPARAM wParam, LPARAM lParam)
{
if(lParam & 0x40000000)
{
GetKeyNameText(lParam, szKey, 32);
SendMessage(hWndMain, 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 = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInst,
GetCurrentThreadId());
if(hHook == NULL){
MessageBox(hWnd, "Cannot load keyboard hook", "Error", MB_OK |
MB_ICONERROR);
return FALSE;
}
RECT rcWindow; GetClientRect(hWndMain, &rcWindow);
hWndEdit = CreateWindow("Edit", "", 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(hWndEdit, EM_REPLACESEL, FALSE, (LPARAM)"{");
SendMessage(hWndEdit, EM_REPLACESEL, FALSE, (LPARAM)szKey);
SendMessage(hWndEdit, EM_REPLACESEL, FALSE, (LPARAM)"}");
return TRUE;

case WM_CLOSE:
EndDialog(hWnd, 0);
PostQuitMessage(0);
break;

case WM_DESTROY:
UnhookWindowsHookEx(hHook);
break;
}
return DefDlgProc(hWnd, uMessage, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE 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,
((GetSystemMetrics(SM_CXSCREEN)-400)/2),
((GetSystemMetrics(SM_CYSCREEN)-300)/2), 400, 300, NULL, NULL, hInst,
0);
if(hWndMain == NULL) return FALSE;

SetWindowLong(hWndMain, GWL_WNDPROC, (LONG)DlgProc);
SendMessage(hWndMain, WM_CREATE, 0, 0);
while(GetMessage(&Msg, NULL, 0, 0)) DispatchMessage(&Msg);

return 0;
}

--- END CODE ---
 
M

mlimber

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.
[snip]

This is platform-specific and should be taken to a newsgroup for your
platform. See this FAQ for what is on-topic here and for some
suggestions of where you might try to post:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

You might also try alt.go.phish and
alt.how.to.get.sent.to.a.white.collar.prison.quickly.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top