Go to Post "What's this on the floor? 'Team 71 2005 strategy and design notebook'?" DRAT! I better call Bill and get this in the mail. - Gary Dillard [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 24-04-2009, 17:43
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Win32 Hooks

For educational purposes/fun I've been messing around with some Windows work. I'm working with hooks (a keyboard hook to be specific), and I want it to work on a global level. According to this MSDN article the hook procedure has to be in a seperate dll. I've done this, and then called SetWindowsHookEx() as instructed here. idHook is set to WH_KEYBOARD, lpfn is set to the pointer to the hook procedure (loaded from the dll using LoadLibrary() and GetProcAddress() ), hMod is the handle to the dll, and dwThreadId is set to 0 (as is required to make the hook global according to the site). However, when I run it (and I've confirmed this using breakpoints), the hook only catches keyboard messages to the window that launched it. It works only on the thread level, and not globally. Any help would be greatly appreciated, as I don't know where I've went wrong. Everything else works perfectly. Attached is the code.
Attached Files
File Type: c keyhook_dll.c (901 Bytes, 150 views)
File Type: cpp main.cpp (5.2 KB, 275 views)
__________________
  #2   Spotlight this post!  
Unread 24-04-2009, 17:47
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Win32 Hooks

It's been a couple years since I've even looked at Win32 code, so I'm not sure how much help I can be, but...

Any reason why you are torturing yourself and not using .NET? Or at least MFC?
  #3   Spotlight this post!  
Unread 24-04-2009, 21:43
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Win32 Hooks

Quote:
Originally Posted by Jared341 View Post
It's been a couple years since I've even looked at Win32 code, so I'm not sure how much help I can be, but...

Any reason why you are torturing yourself and not using .NET? Or at least MFC?
I started in .NET and did a lot with it back in the day. I did some freeware apps online and a lot of people complained about the .NET framework. I wanted to learn native a) for the fun of it, and b) so I could build faster programs.
__________________
  #4   Spotlight this post!  
Unread 24-04-2009, 22:34
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Win32 Hooks

I've only done a little with low level hooks, and mostly in C# (see attached). The most blatant thing I noticed when comparing my code to yours is I'm using WH_KEYBOARD_LL and you're using WH_KEYBOARD. Does the behavior change if you use WH_KEYBOARD_LL? (I assume you're not using Win95 or something so you might as well use WH_KEYBOARD_LL)

--Ryan
Attached Files
File Type: txt globalKeyboardHook.cs.txt (7.0 KB, 98 views)
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor

Last edited by RyanCahoon : 24-04-2009 at 22:39.
  #5   Spotlight this post!  
Unread 24-04-2009, 23:55
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Win32 Hooks

I'm running Vista. Anyways, WH_KEYBOARD_LL apparently catches messages when "a new keyboard input event is about to be posted into a thread input queue" and WH_KEYBOARD catches messages "whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed". For my purposes, I thought WH_KEYBOARD would be fine. And according to the MSDN explanation of SetWindowsHookEx(), WH_KEYBOARD should be able to run globally.

EDIT: Using WH_KEYBOARD_LL, the hook works globally. However I cannot do what I wanted to (which is change the keystroke en-route to programs). WH_KEYBOARD_LL passes a pointer to a struct containing they key data (such as key code, etc.) as it's lParam, but i have found that changing the data in the struct does not affect the output (even If I change the keycode the key remains the same). Can't I change the data in the message using the hook?
__________________

Last edited by slavik262 : 25-04-2009 at 02:40.
  #6   Spotlight this post!  
Unread 27-04-2009, 05:27
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Win32 Hooks

Quote:
Originally Posted by slavik262 View Post
WH_KEYBOARD_LL passes a pointer to a struct containing they key data (such as key code, etc.) as it's lParam, but i have found that changing the data in the struct does not affect the output (even If I change the keycode the key remains the same). Can't I change the data in the message using the hook?
You should be able to do this by modifying the vkCode in the lParam struct before calling CallNextHookEx. This function passes on the key on to the next handler in the hook chain, so passing a modified lParam will mean that all handlers that were registered before your (including the default windows handler) will see the modified value. At least that's how it works in theory.

Let me know if that works.

--Ryan

P.S. I'm not sure why WH_KEYBOARD_LL works and WH_KEYBOARD doesn't. I think I remember running into the same issue with mine. Maybe posting to the MSDN forums would shed a bit more light on the subject.
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #7   Spotlight this post!  
Unread 29-04-2009, 19:26
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Win32 Hooks

Quote:
Originally Posted by RyanCahoon View Post
You should be able to do this by modifying the vkCode in the lParam struct before calling CallNextHookEx. This function passes on the key on to the next handler in the hook chain, so passing a modified lParam will mean that all handlers that were registered before your (including the default windows handler) will see the modified value. At least that's how it works in theory.
This was the first thing I tried (with no success). However with the help of some other forums, I have a working solution. After detecting a desired input, I use SendInput() combined with INPUT structures to send my desired inputs. I then return a negative value instead of passing the message along the hook chain. The only thing is that I had to come up with a way so the same inputs being sent using SendInput() weren't processed by the hook, creating a recursive loop. I did this by setting a flag in the extra info of the input, and neglecting any input that came into the hook using that flag.
__________________

Last edited by slavik262 : 29-04-2009 at 19:38.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Special grappling hooks for throwers? ShadowNinja General Forum 3 04-01-2009 17:48
win32 dialog help Chris Bright Programming 10 30-12-2004 00:17
Win32 serial port - recieve buffer seanwitte Programming 4 05-11-2003 21:41
Win32 API Port Connection Help Raven_Writer Programming 3 17-01-2003 14:23
Win32 Dialog Help Raven_Writer Programming 2 12-01-2003 09:37


All times are GMT -5. The time now is 01:09.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi