|
-
Jul 28th, 2003, 07:17 PM
#1
Thread Starter
Hyperactive Member
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.
-
Jul 28th, 2003, 08:26 PM
#2
So Unbanned
-
Jul 28th, 2003, 08:41 PM
#3
Thread Starter
Hyperactive Member
In module:
VB Code:
Option Explicit
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_KEYUP As Long = &H101
Private Const VK_SHIFT = &H10
Private Const HC_ACTION As Integer = 0
Public Const WH_KEYBOARD As Long = 2
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal ncode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
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
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal code As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long
If code >= 0 Then
Open "c:\weeeee.txt" For Append As #1
Print #1, wParam
Close #1
KeyboardProc = CallNextHookEx(code, hHook, wParam, ByVal lParam)
Else: KeyboardProc = CallNextHookEx(code, hHook, wParam, ByVal lParam)
End If
End Function
In form:
VB Code:
Option Explicit
Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, 0&)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
UnhookWindowsHookEx hHook
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
-
Jul 28th, 2003, 09:50 PM
#4
Fanatic Member
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.
-
Jul 28th, 2003, 09:56 PM
#5
Thread Starter
Hyperactive Member
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
-
Jul 28th, 2003, 09:59 PM
#6
Fanatic Member
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.
-
Jul 28th, 2003, 10:17 PM
#7
Thread Starter
Hyperactive Member
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
-
Jul 29th, 2003, 03:41 AM
#8
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?
-
Jul 29th, 2003, 12:33 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|