473,503 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need some help with this capturing keys

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 ---

May 22 '06 #1
1 2156
tm********@gmail.com wrote:
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-lit...t.html#faq-5.9

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

Cheers! --M

May 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7836
by: James Collier | last post by:
Is it possible to capture the results of repeating group matches in the python regular expression module? To illustrate, what I want is: >>> re1 = re.compile("(W)(X)+(Y)"); >>> mo1 =...
3
10552
by: coolsti | last post by:
Can someone help me enhance this code snippet which works only for IE so that it will also work for Firefox? I have been trying all sorts of things and have gotten to where I can capture the...
4
15309
by: Vazz | last post by:
I am trying to capture the key combination "Control + Shift + Enter" in a KeyDown event. I am able capture "Control + Enter" using (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control). How do I...
1
1976
by: Jonny | last post by:
Hi all, Does anyone know how to capture the Windows Keys (I know that they are out of the ascii key range) and how to capture and record Mouse movements? Thanks, /Jonny
1
2570
by: Pjotr Wedersteers | last post by:
Hi I want to write a small prog/applet that does nothing but the following: Display icons representing the keys pressed on the keyboard in a small "always on top" but transparent window. The...
2
6065
by: Phil Galey | last post by:
I'm using the ProcessCmdKey event to capture various keys that are pressed. However, I'm having trouble capturing the combination of the CTRL key and, say, the DOWN key. If I just press the CTRL...
2
2607
by: Sagaert Johan | last post by:
Hi I have a usercontrol , the keypress and keyup/down events do not seem to work on the cursor keys . How can i capture the cursor keys when my control has the focus ? Johan
2
1604
by: Mohammad Omer Nasir | last post by:
Hi, I am working on vs2k5 IDE for C# development. I made one Form which has one button on it. I tried to override KeyDown message on From but KeyDown message received. if Form does not...
2
3086
by: John | last post by:
Hi How can I capture control keys Ctrl-A, Ctrl-B, Ctrl-C etc. in a form? Thanks Regards
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.