Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Classic Visual Basic (VB 6 or earlier) » General Visual Basic » Keyboard Hook


Keyboard HookExpand / Collapse
Author
Message
Posted 8/19/2004 11:58:30 AM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 7/15/2011 3:57:36 PM
Posts: 371, Visits: 208

Hi Everybody,

I was reading an earlier thread about the start button being disabled.  That sparked my interest, 'cause as a programmer I sure would like to be able to control the keyboard at any point in my application. The solution lead to an API(which from lack of knowledge makes me ill:sick  So I go on my search to learn more about API and to create some reusable code to do the job.  I found an article describing such code.  But it doesn't seem to be working for me.  I'm sure the problem rests between the keyboard and the back of my chair!  If someone has the time would you go to this site and see if you can get this to work?  Here's the site:

http://www.codeguru.com/vb/gen/vb_system/keyboard/article.php/c4829

It seems that SetWindowsHookEx in the following sub returns 0 for me every time

Public Sub HookKeyboard()
  KeyboardHandle = SetWindowsHookEx( _
    WH_KEYBOARD_LL, AddressOf KeyboardCallback, _
    App.hInstance, 0&

Thus the following code is executed.

Debug.Print "Keyboard hook failed: " & Err.LastDllError

Also, as I'm a noobe in this area:

Can I Debug and StepInto such code?

In code listing at the end of the article, is ControlEscape the only key combination being blocked?

Is ControlEscape the same as the Windows Key(ya know the one between Ctrl and Alt)?

If someone's got the time I would be estatic!

TIA



TallOne
Post #2768
Posted 8/20/2004 3:16:14 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/24/2009 3:34:33 AM
Posts: 511, Visits: 265

Public Sub HookKeyboard()
  KeyboardHandle = SetWindowsHookEx( _
    WH_KEYBOARD_LL, AddressOf KeyboardCallback, _
    App.hInstance, 0&

 

Try adding a "ByVal" before that "0&".

Post #2787
Posted 8/20/2004 2:56:12 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 7/15/2011 3:57:36 PM
Posts: 371, Visits: 208

Hey DigiRev,

Thanks for replying!  This has been racking my brain for two days now!    I shouldn't spend so much time on it, but if I could get this working I believe I could trap about any type of message.  This code just happens to test for keyboard message

If (Code = HC_ACTION) Then 'Its a keyboard message

After it's trapped it's then passed on to windows or the next hook that's doing the same thing.  Seems like a powerful technique to know!

All parameters in the function SetWindowsHookEx IS declared ByVal so I was doubtful.

Public Declare Function SetWindowsHookEx Lib "user32" _
  Alias "SetWindowsHookExA" (ByVal idHook As Long, _
                             ByVal lpfn As Long, _
                             ByVal hmod As Long, _
                             ByVal dwThreadId As Long) As Long

But I tried it anyways like so..

Public Sub HookKeyboard()
  KeyboardHandle = SetWindowsHookEx( _
    WH_KEYBOARD_LL, AddressOf KeyboardCallback, _
    App.hInstance, ByVal 0&......

And still NoGo!  But thanks for trying.



TallOne
Post #2837
Posted 8/20/2004 5:10:37 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/24/2009 3:34:33 AM
Posts: 511, Visits: 265

I think I know the problem.

You're not passing the ThreadID...like this:

Public Sub HookKeyboard()
  KeyboardHandle = SetWindowsHookEx( _
    WH_KEYBOARD_LL, AddressOf KeyboardCallback, _
    App.hInstance, App.ThreadID

Post #2840
Posted 8/20/2004 9:02:36 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 7/15/2011 3:57:36 PM
Posts: 371, Visits: 208

Hi

Thanks but that didn't work either.  It did get me a little excited for a couple of minutes though!



TallOne
Post #2847
Posted 8/22/2004 11:54:56 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 3/13/2006 2:34:13 AM
Posts: 794, Visits: 338
From that link you gave at start thread...
"If the Code is 0 then we want to convert the lParam to the type we know it is, a KBDLLHOOKSTRUCT, handle the keyboard input, and return 1 to indicate that the input was handled."
try looking in that area.

Returning zero is 'good' using his special wrapper, but I think the problem is coming into how it is handled.
I'll check back here later.

Post back with resolve if/when it comes please.
I think you can get! Yes I do!





Post #2883
Posted 8/23/2004 5:48:34 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/24/2009 3:34:33 AM
Posts: 511, Visits: 265

This might be able to help you out a little.

http://www.mentalis.org/apilist/SetWindowsHookEx.shtml#

Post #2891
Posted 8/23/2004 5:21:47 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 3/13/2006 2:34:13 AM
Posts: 794, Visits: 338
I've tried to Impliment the Keyboard hook (class, form, module - oh my), into my own project, but get same results. I wonder if that Kimmel guy just gave enough info for an empty wrapper....

Here is the next best example I found on net:
+ = Keyboard Hook example that works.






Post #2903
Posted 8/23/2004 8:35:18 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/24/2009 3:34:33 AM
Posts: 511, Visits: 265

I've never tried to hook the keyboard, I just used the GetAsyncKeyState() API function:

Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

http://www.mentalis.org/apilist/GetAsyncKeyState.shtml for more info.

Post #2905
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod, Mark, Keithuk

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 4:34am