Results 1 to 9 of 9

Thread: Global (system wide) keyboard hooks - resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506

    Global (system wide) keyboard hooks - resolved

    Hi, I'm developing a global high level keyboard hook, low level is of no use in this case because it doesn't trap the shift key fast enough.

    Unfortunately I have a problem with the high level hook, explorer crashes everytime a key is depressed - of course this is not good. Does anyone know why this happens? And is there a possible work around for this?

    Cheers.
    -adehh
    Last edited by adzzzz; Jul 29th, 2003 at 02:38 PM.

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Show some code.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    In module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const WM_KEYDOWN As Long = &H100
    4. Private Const WM_KEYUP As Long = &H101
    5. Private Const VK_SHIFT = &H10
    6. Private Const HC_ACTION As Integer = 0
    7.  
    8. Public Const WH_KEYBOARD As Long = 2
    9.  
    10. Private Declare Function CallNextHookEx Lib "user32" _
    11.     (ByVal hHook As Long, _
    12.     ByVal ncode As Long, _
    13.     ByVal wParam As Long, _
    14.     lParam As Any) As Long
    15.    
    16. Private Declare Function GetKeyState Lib "user32" _
    17.     (ByVal nVirtKey As Long) As Integer
    18.    
    19. Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
    20.     (ByVal idHook As Long, _
    21.     ByVal lpfn As Long, _
    22.     ByVal hmod As Long, _
    23.     ByVal dwThreadId As Long) As Long
    24.    
    25. Public Declare Function UnhookWindowsHookEx Lib "user32" _
    26.     (ByVal hHook As Long) As Long
    27.    
    28. Public hHook As Long
    29.  
    30. Public Function KeyboardProc(ByVal code As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long
    31.    
    32.     If code >= 0 Then
    33.         Open "c:\weeeee.txt" For Append As #1
    34.             Print #1, wParam
    35.         Close #1
    36.        
    37.         KeyboardProc = CallNextHookEx(code, hHook, wParam, ByVal lParam)
    38.     Else: KeyboardProc = CallNextHookEx(code, hHook, wParam, ByVal lParam)
    39.     End If
    40. End Function

    In form:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, 0&)
    5. End Sub
    6.  
    7. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    8.     UnhookWindowsHookEx hHook
    9. End Sub

    The above code works fine when I'm in the form but if I lose the focus of the form and then go back to it, it stops working.
    -adehh

  4. #4
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    You can't do global hooks in VB -- they must be in a DLL (and I don't think an ActiveX dll will work, but don't quote me on that).
    I have two simple requests: 1) Use useful and specific topics. 2) Modify your topic to include [Resolved] when you problem has been resolved. Both of these make the bulletin boards more useful and efficient. Thanks.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    No, you're quite right, it is stated all over that it cannot be done in 'pure' VB. It's just the fact that it works to the point where explorer.exe always crashes out and I don't understand why.

    Looks like I'm going to be learning C or whatnot, do you know if you can compile standard DLLs in C? - I'm clueless.
    -adehh

  6. #6
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    Visual C++ for Visual Basic Developers, by Bill Locke

    It tells you how to do hooks and controls and lots of good stuff. Yes, you can do a dll in C.
    I have two simple requests: 1) Use useful and specific topics. 2) Modify your topic to include [Resolved] when you problem has been resolved. Both of these make the bulletin boards more useful and efficient. Thanks.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    Heh, cheers. I've always wanted to get into C\++ but for now I think I'll just go for the low level approach as I had that working globally.
    -adehh

  8. #8
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    Hi, I'm developing a global high level keyboard hook, low level is of no use in this case because it doesn't trap the shift key fast enough.
    Since the low level hook (WH_KEYBOARD_LL) gets called before the standard keyboard hook (WH_KEYBOARD) I don't know how it can not be 'fast enough'....could you explain what the problem is?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    Hi, It's actually gone alright now and the low level hook works fine globally.
    My previous problem was that when using GetKeyState(VK_SHIFT) to find out if the shift key is depressed and then using that value to determine if input is capitol or another alternative key etc, was sometimes not registering that the key entered was 'shifted' - so I would be left with a lowercase key whereas the thread the input was 'sent' to actually got an upercase key.
    I've got round it now by using my own boolean shift variable and declaring it true or false as the shift key is pressed up/down. Maybe it was my PC being silly the other night, I'm not sure.
    Cheers all the same!
    -adehh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width