Solved

C++ Key count

Posted on 2006-07-05
29
4,626 Views
Last Modified: 2008-01-09
Hello,

I'm sure some of you have heard of Tiny KeyCounter - it's pretty much pointless but it's fine...

What it does it counts how many keys presses, mouse clicks & mouse scrolls you've done and then submits them to a database.

Anyway, I was just wondering if anyone can point my in the right direction to make a KeyCounter application using Bloodshed Dev-C++ - something simple to start with, so a form with 3 boxes on it, within those boxes the ammount of key presses, mouse clicks or mouse scrolls depending on the box.

I'm pretty new to C++ so go easy on me :p

Thanks,
- Craig.
0
Comment
Question by:Craig321
  • 16
  • 11
29 Comments
 
LVL 18

Expert Comment

by:JoseParrot
ID: 17047402
Hi, Craig,

If you intend to count clicks and keying IN THE CONTEXT of your application, it is a simple task to do it with any C++ compiler.

If you want a program to show click and key count the user does in any other program (Outlook, Internet Explorer, Windows Explorer, Notepad), including the desktop, then we have a problem. For this, we need to intercept the keyboard and mouse services, which is totally controlled by Windows. These services are in protected memory areas. So, we need a Device Drive Kit. You can still use C, but need this auxiliary API.
http://www.microsoft.com/ddk

Jose
0
 
LVL 9

Author Comment

by:Craig321
ID: 17047407
Hmm, think developing a driver is a bit too far for me to go already, I only just started C++ :)

Maybe to start off it'd be best if I counted the keys in the content of my app? Could you point me in the right direction for doing that?

Thanks,
Craig.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17047603
No device driver kit is required.
Two SetWindowsHookEx() with callbacks to your own dll will suffice.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17047624
This is just one of a gazillion hits you'll get if you search for SetWindowsHookEx:

http://delphi.about.com/library/bluc/text/uc063001a.htm


There are plenty of C and VB examples out there for both local (monitoring just the calling process) and global (monitoring all processes) hooks.

Enjoy - it's great fun.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17060027
Cool, so I'll be able to count all mouse, keyboard etc. presses if I leatn SetWindowsHookEx?

Thanks,
Craig.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17061871
Hmm, can't seem to find any actual keyhook simple examples anywhere :(
Could anyone point me to some please?

Thanks,
Craig.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17061935
Yes.

What it does is let you see what messages (in the Window's Message Queue sense) are being passed to whom.  

Here's a good place to start:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/usinghooks.asp
0
 
LVL 22

Expert Comment

by:cookre
ID: 17061959
That last post was in response to your previous post - I just now got the notif of your last post.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17062174
Can this code provided by Microsoft work in Visual C++ ?

Also, does anyone know of any tutorials on coding buttons not visually? I'd like to be able to make application without the .NET framework but can't figure out how to make button etc. in BloodShed C++
0
 
LVL 9

Author Comment

by:Craig321
ID: 17062267
The code that MS gives... the long one at the end and the short one at the beginning.
How do I use those?

Thanks
0
 
LVL 22

Expert Comment

by:cookre
ID: 17062843
0
 
LVL 9

Author Comment

by:Craig321
ID: 17063416
OK, will look through them thanks.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17063472
Hmmm, I think it's harder than I thought... unless I'm making it harder for myself somehow.

None of the tutorials have really explained it to me well, they have given me simple examples of settings a key hook that logs to a text file or something.

I would have though it'd be something simple like entering this code into the area that VC++ creates for you when you double click a button:

SetWindowsHookEx(blah, blah, blah, blah);
if (WinHook KeyPress)
{
  TextBox1.Text + 1;
}

Obviously that's a bit of a silly example, but I would have thought it'd be something simple like that?

The main problem is I can't get the examples on the sites I have been given to compile, I have tried in Bloodshed and in VC++ - I'm not trying to steal examples and code to make this, I just find it ALOT easier when I have a working example to work with to make my own version.

Thanks,
Craig.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17066836
For global key hook

make dll:
#include <windows.h>

LRESULT CALLBACK GottaKey(int,WPARAM,LPARAM);

BOOL WINAPI LibMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
       {
       case DLL_PROCESS_ATTACH:
            {
            }
            break;

       case DLL_THREAD_ATTACH:
            {
            }
            break;

       case DLL_THREAD_DETACH:
            {
            }
            break;

       case DLL_PROCESS_DETACH:
            {
            }
            break;
       }
return TRUE;
}


LRESULT CALLBACK APIENTRY GottaKey(int PassItOn,WPARAM wParam,LPARAM lParam)
{
// This is where you deal with a keystroke
return CallNextHookEx(NULL,PassItOn,wParam,lParam);
}



Register the hook in your app with:

hDLL=LoadLibrary("Full path to dll above");
if (hDLL==0)
   {
   oops
   }

func=GetProcAddress(hDLL,"GottaKey");
if (func==0)
   {
   oops
   }  

hookproc=SetWindowsHookEx(WH_KEYBOARD,(long (__stdcall *)(int,unsigned int,long))func,hDLL,0);
if (hookproc==0)
   {
   oops
   }

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Author Comment

by:Craig321
ID: 17068924
Cool, thanks very much. Would I put the code to output the ammount of keys pressed where you have put in "//This is where you deal with a keystroke" ?
0
 
LVL 22

Expert Comment

by:cookre
ID: 17069255
That's where you'd count them.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17069307
Okay, right. So I'm guessing it should be something easy like in visual basic:

Add a TextBox (TextBox1) and then where you have said put in TextBox1.Text + 1 ? Would that work as long as I put it in C++ style coding?

Also, I'd put the hDLL=LoadLibrary etc. code under the 'Hook' button wouldn't I?

Thanks,
Craig.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17069679
One generally doesn't want to do much of anything substantial in a hook callback, unless it's handling JUST keyboard messages.  For example, if you add a mouse hook then try to do smething guiish for each mouse message, you'll likely slow down machine performance significantly.

I'd recommend sending a message back to your main program for each trapped event, then, if your event processing becomes significant, the only impacted process is yours, and not the OS.

0
 
LVL 9

Author Comment

by:Craig321
ID: 17069927
So you're suggesting that where you have put the comment for me to make that, rather than directly adding +1 there, send a message back to my main program to add it there rather?

Craig.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17070121
Bumping a counter is fine.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17148786
Hi,

Sorry for the late reply, our broadband broke :(

Anyway, I've been trying that code above while it's been down putting this:

#include <windows.h>

LRESULT CALLBACK GottaKey(int,WPARAM,LPARAM);

BOOL WINAPI LibMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
       {
       case DLL_PROCESS_ATTACH:
            {
            }
            break;

       case DLL_THREAD_ATTACH:
            {
            }
            break;

       case DLL_THREAD_DETACH:
            {
            }
            break;

       case DLL_PROCESS_DETACH:
            {
            }
            break;
       }
return TRUE;
}


LRESULT CALLBACK APIENTRY GottaKey(int PassItOn,WPARAM wParam,LPARAM lParam)
{
// This is where you deal with a keystroke
return CallNextHookEx(NULL,PassItOn,wParam,lParam);
}

into a DLL project in Dev-C++ and then putting that created DLL in to the project folder for the actual keycount and insert this code under the attack key hook button:

hDLL=LoadLibrary("keycount.dll");
if (hDLL==0)
   {
   oops
   }

func=GetProcAddress(hDLL,"GottaKey");
if (func==0)
   {
   oops
   }  

hookproc=SetWindowsHookEx(WH_KEYBOARD,(long (__stdcall *)(int,unsigned int,long))func,hDLL,0);
if (hookproc==0)
   {
   oops
   }
0
 
LVL 9

Author Comment

by:Craig321
ID: 17148798
Carried on from last post (pressed submit by mistake):

When I try build that code it gives an error on the hDLL part.

Is there anything else I need to do to get it working (also, I replaced the 'oops' sections with a popup error box.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17149281
and that error was...?
0
 
LVL 9

Author Comment

by:Craig321
ID: 17149782
Sorry, forgot to post it :P

Screenshot coming up...
0
 
LVL 9

Author Comment

by:Craig321
ID: 17149856
Here are the errors:

http://www.planetuploads.com/home/imgfull.php?image=HygkQXyd_errors.jpg
http://www.planetuploads.com/home/imgfull.php?image=cHJhjVQs_highlighted.jpg

I created a new DLL project and inserted the DLL code, it compiled alright so I put it in my keycount project folder and added the other code under a button in my main application and get those errors when I try compile :(

Thanks,
Craig.
0
 
LVL 22

Accepted Solution

by:
cookre earned 125 total points
ID: 17150811
Most of my code samples are just to give the idea, and I seldom include declarations.

In any case, Here, for example, we look up LoadLibrary() and see it returns an HMODULE, so

HMODULE  hDLL;

Similarly, we look up SetWindowsHookEx() and see it returns an HHOOK.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17151153
Ah, ok. Will go through it and include declarations.

Craig.
0
 
LVL 9

Author Comment

by:Craig321
ID: 17373414
Have accepted cookres answer.

Thanks,
Craig.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 76
Enhanced For loop vs ForEach Loop 4 56
Exe program is not a valid Win 32 application 15 95
groovy example issue 10 67
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now