Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ Key count

Posted on 2006-07-05
29
4,655 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

837 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