Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intercepting KeyStrokes CAN be done within VB!!!!!!!! :-) 1

Status
Not open for further replies.

LuckyLuke

Programmer
Mar 28, 2001
188
NL
Hi guys,

This is especially for jmarler and Alt255! I finally found out how to do it and it's pretty easy to be exactly...

Here it is:

Form code:

Option Explicit

Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim lunHook As Long
If hHook Then
lunHook = UnhookWindowsHookEx(hHook)
hHook = 0
End If
End
End Sub


Module:

Option Explicit

Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
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 Const WH_KEYBOARD = 2

Public hHook As Long

Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If idHook = 0 Then
MsgBox "You've pressed a key"
End If
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End Function


WOHOOOOOOOOOOOO :-D

LuCkY
 
LuckyLuke,
Yes, that is exactly how you do it in VB! Good Job! :) But the real problem is to make it monitor all Processes on the computer. If your callback function is in a bas module, the hook is only for the current process. In order to hook into all processes (i.e. Global) you beed the call back to be in a DLL (not an ActiveX DLL, but rather a standard Win32 DLL). That is where (as far as I know) you have to use C++.
But still, Good Job . . . you got this part exactly right!
- Jeff Marler B-)
 
Ups lol...

I was a bit 2 happy and I posted this message before I really started it ;-) I tested it minimized in the IDE from VB and of course, it works there... As soon as I exported it as an exe it stopped working lol...

OK, back to my old dll...

LuCkY
 
Yup! Been there . . . done that . . . :)Basically, you did exactly what I fell for the first time I started looking at this. But seriously, this technique does work great if you want to monitor all keypresses (or for that matter any other event that goes through the OS event queue . . . i.e. almost everything) for a specific process W/O having to have a form up and visible.



- Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top