Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4693
  • Last Modified:

C++ Key count

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
Craig321
Asked:
Craig321
  • 16
  • 11
1 Solution
 
Jose ParrotGraphics ExpertCommented:
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
 
Craig321Author Commented:
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
 
cookreCommented:
No device driver kit is required.
Two SetWindowsHookEx() with callbacks to your own dll will suffice.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cookreCommented:
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
 
Craig321Author Commented:
Cool, so I'll be able to count all mouse, keyboard etc. presses if I leatn SetWindowsHookEx?

Thanks,
Craig.
0
 
Craig321Author Commented:
Hmm, can't seem to find any actual keyhook simple examples anywhere :(
Could anyone point me to some please?

Thanks,
Craig.
0
 
cookreCommented:
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
 
cookreCommented:
That last post was in response to your previous post - I just now got the notif of your last post.
0
 
Craig321Author Commented:
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
 
Craig321Author Commented:
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
 
Craig321Author Commented:
OK, will look through them thanks.
0
 
Craig321Author Commented:
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
 
cookreCommented:
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
 
Craig321Author Commented:
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
 
cookreCommented:
That's where you'd count them.
0
 
Craig321Author Commented:
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
 
cookreCommented:
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
 
Craig321Author Commented:
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
 
cookreCommented:
Bumping a counter is fine.
0
 
Craig321Author Commented:
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
 
Craig321Author Commented:
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
 
cookreCommented:
and that error was...?
0
 
Craig321Author Commented:
Sorry, forgot to post it :P

Screenshot coming up...
0
 
Craig321Author Commented:
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
 
cookreCommented:
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
 
Craig321Author Commented:
Ah, ok. Will go through it and include declarations.

Craig.
0
 
Craig321Author Commented:
Have accepted cookres answer.

Thanks,
Craig.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 16
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now