Link to home
Start Free TrialLog in
Avatar of SPD24
SPD24

asked on

Monitoring Keyboard Input

I'm beginning to write a VB application that runs as a Windows NT Service and monitors keyboard input.  If a certain application has the focus, I want to strip-off certain characters if they are part of an entered string (such as the tilde (~) if it is the first character of the entered string).  I would like to use existing API's or functions to do some parts of this.  Are there any API's, functions, or existing code which would help me monitor keyboard input and strip-off these characters.
Avatar of andyclap
andyclap
Flag of United Kingdom of Great Britain and Northern Ireland image

This is a quite complicated area to get into - syshooks - but if you're writing services, I guess you can cope...

You set up a window hook with SetWindowsHookEx
with an id of WH_KEYBOARD, the addressof the KeyboardProc hook you want to call, a hmod of null, and a dwthreadid of 0; it returns a handle for the hook
Your Keyboard proc should have the prototype:
public function KeyboardProc(byval l as long, byval wParam as long, byval lParam as long)

(Note this may be a bit wrong... try the first param as an int...)

in this proc you should call CallNExtHookEx with the hook's handle to allow other system hooks to work, and when you're finished hooking you should call UnhookWindowsHookEx passing the handle

I know that's very cursory, but it's quite a complicated procedure - there's also a caveat on the MSDN viz:
"An error may occur if the hMod parameter is null and the dwThreadId is zero" probably because NT might not allow this to be done unless the user (or the user for the service) has certain permissions.


Does this help, or is it a bit sketchy? :)
Avatar of Ark
System wide keyboard hook can not be done for NT with VB only. You have to make standard dll (C or delphi) with callback (KeyboardProc) procedure. There is a trick wich allow to do this with w9x (http://www.freevbcode.com/ShowCode.Asp?ID=1610), but it doesn't work on NT.
Another way - use timer and GetAsyncKeyState API, but there is a dillema - if you use long interval, you can miss some chars, while short interval couse using a lot of system recources.

Cheers
Thanks Ark, Interesting point - hence the MS caveat I suppose. As you can probably guess, I'd not tried it myself.
Avatar of SPD24
SPD24

ASKER

Ark,
Can you please exlain the limitations of using VB for a system wide hook.  My application will look to see if a certain application(s) has the focus; if it does, then I will check if certain characters are entered from the keyboard or a wedge scanner.  Do you think VB would be able to handle this or is this a limitation of using VB to generate system wide keyboard hooks?  

Thanks for the info you and andyclap have given.  
Hi
About hooks:
In win32 applications run in their own thread and know nothing about other threads/applications. So there are 2 tipes of hooks - thread defined and ssystem defined. First one works only inside thread(application) and hook keyboard/mouse and other hook types onli within this app.
Second can intersept events system wide. But according MSDN, for this purpose CallBack procedure must be in standard dll. VB can not produce such dll (only ActiveX). But you still can yous already made dlls for this purpose.
For more info (or if you not understand my awful English) take a look at http://vbaccelerator.com/codelib/hook/vbalhook.htm
There are good article and hook library you can use.
I tried to implement system-wide hooks directly from vb (to tell the truth, I don't belive some MS info), and was succesfull (found a hole in MS), but only with w9x.
BTW, I've developed system-wide shell hook (http://www.freevbcode.com/ShowCode.Asp?ID=1308). Seems it can be usefull for your app - checking app activation.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial