473,549 Members | 2,615 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=.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 ---

May 22 '06 #1
1 2164
tm********@gmai l.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. quickly.

Cheers! --M

May 22 '06 #2

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

Similar topics

4
7847
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 = re.match("aWbXcXdXeXfY"); >>> print mo1.groupsButNotAsWeKnowIt() ('aW','bX','cX','dX','eX','fY')
3
10555
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 keydown and keypress events in Firefox, but then I can't seem to get the key code. I also don't know if the window.event.srcElement.type works with...
4
15319
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 check if there are two Modifiers?
1
1983
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
2573
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 keys must always be read - even when the proggy has no focus- but not erased from the keyboard buffer so the active app still catches them. I don't...
2
6069
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 key alone, the KeyData value is 131089. But all the possibilities I'm finding in the Keys enumeration are the following: Keys.ControlKey = 17...
2
2609
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
1613
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 containing any Item. When I drop any thing on Form, it does not receive KeyDowm message. I want to change button location according to Arrow keys. Please...
2
3092
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
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.