RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 2986 | Replies: 4
Reply
Join Date: Jul 2005
Posts: 6
Reputation: BenPage is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
BenPage BenPage is offline Offline
Newbie Poster

Will Keyboard Hooks solve my problem?

  #1  
Jul 16th, 2005
Hi there.

I am currently working on a project to identify users using their typing dynamics. I want to record the intervals
between characters as a user type in their passwords and use these recorded intervals to identify the users.

I am using Windows XP and Visual Studio C++ 2003 to develope the project. I want to use a dialog with an edit box
to allow the user to enter his/her username and password.

I cannot process the WM_KEYDOWN and WM_KEYUP messages because when you type in the edit box, the keyboard focus is
on the edit box and not on the dialog.

Will a keyboard hook help me in my mission or will I have to use something else.


If you could help me I would appreciate it alot.


Thank You.
Ben
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: Iceland
Posts: 104
Reputation: Alvein is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Alvein's Avatar
Alvein Alvein is offline Offline
Junior Poster

Re: Will Keyboard Hooks solve my problem?

  #2  
Jul 16th, 2005
With some care, of course they will do it, but being the edit box *a window itself*, why don't you try to create a custom WndProc for it and capture the WM_KEY's messages there?
Reply With Quote  
Join Date: Jul 2005
Posts: 6
Reputation: BenPage is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
BenPage BenPage is offline Offline
Newbie Poster

Re: Will Keyboard Hooks solve my problem?

  #3  
Jul 16th, 2005
Hi Alvein, thanx for the rep.

If creating a custom WndProc is easier than installing a keyboard hook, I would be interrested in trying it.

Could you give me some sort of guide as to how I will create a custom WndProc to specifically process the WM_KKEY* messages that is send to the edit box. I am using Visual Studio 2003 and a MFC project. I dont know in which source file I have to create the WndProc function and how to link it to the edit box.

I am still pretty new to MFC projects, but I have worked through a Sam's Teach Yourself Visual Studio C++ book. So I know all the basics.

Thanx.
Reply With Quote  
Join Date: Jul 2005
Location: Iceland
Posts: 104
Reputation: Alvein is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Alvein's Avatar
Alvein Alvein is offline Offline
Junior Poster

Re: Will Keyboard Hooks solve my problem?

  #4  
Jul 16th, 2005
Hi,

It's rather easy and a lot safer, but it also requires the knowledge of some basic Windows' internals. Not only C++.

I would do it this way, but first of all, I'm not skilled on MFC. I even don't have the thing installed in my PC, so my sample is for a simple dialog:

Look for the source file where your dialog processor is defined. For example, if your dialog is named "Login", you surely have the function "LRESULT CALLBACK Login(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)" defined somewhere.

That function should have this structure:

LRESULT CALLBACK Login(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam) {
  switch(message) {
    case <some message>:
      ....
      break;
    case <another message>:
      ....
      break;
    ....
    }
  return(FALSE);
}

So, do the following edits:

Add the declaration
HWND hCtrl=GetDlgItem(hDlg,IDC_PASSWORD);
at the beginning of the function (IDC_PASSWORD would be the Control ID of our EditBox).

Locate the line "case WM_INITDIALOG" and insert
SetProp(hCtrl,PREVWNDPROC,(HANDLE)SetWindowLong(hCtrl,GWL_WNDPROC,(LONG)MyEditBoxWndProc));
at the beginning of the case.

Locate the line "case WM_CLOSE" (if it does not exist, create it) and insert
SetWindowLong(hCtrl,GWL_WNDPROC,(LONG)GetProp(hCtrl,PREVWNDPROC));
RemoveProp(hCtrl,PREVWNDPROC);

That's all. Now note two undefined things there, PREVWNDPROC and MyEditBoxWndProc. PREVWNDPROC is easy. Just place
#define PREVWNDPROC "PREVWNDPROC"
at the beginning of the file.

Finally, MyEditBoxWndProc should be something like:

LRESULT CALLBACK MyEditBoxWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) {
TCHAR szBlah[40]; // Custom
  switch(message) {
    case WM_KEYUP:
      /////// Custom ///////
      wsprintf(szBlah,"%X %X",wParam,lParam);
      SetWindowText(GetDlgItem(GetParent(hWnd),IDC_KEYUP),szBlah);
      lstrcpy(szBlah,"");
      SetWindowText(GetDlgItem(GetParent(hWnd),IDC_KEYDOWN),szBlah);
      /////////////////////
      break;
    case WM_KEYDOWN:
      /////// Custom ///////
      lstrcpy(szBlah,"");
      SetWindowText(GetDlgItem(GetParent(hWnd),IDC_KEYUP),szBlah);
      wsprintf(szBlah,"%X %X",wParam,lParam);
      SetWindowText(GetDlgItem(GetParent(hWnd),IDC_KEYDOWN),szBlah);
      /////////////////////
    }
  return(CallWindowProc((WNDPROC)GetProp(hWnd,PREVWNDPROC),hWnd,message,wParam,lParam));
}

Both case's, WM_KEYUP and WM_KEYDOWN will capture the respective messages. Everything under "Custom" is optional. In the sample, I'm using two extra controls (labels) to display the values of wParam and lParam for each message. You will need to read about those messages to have an idea of what those values means.


Hope this helps.
Reply With Quote  
Join Date: May 2005
Location: London, UK
Posts: 145
Reputation: Electrohead is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
Electrohead's Avatar
Electrohead Electrohead is offline Offline
Junior Poster

Re: Will Keyboard Hooks solve my problem?

  #5  
Jul 19th, 2005
Blimey, that's complex stuff!
: :: Click here to join my website :: :
Brand new online community! Help my forum grow!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:52 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC