Link to home
Start Free TrialLog in
Avatar of monarch_ilhan
monarch_ilhanFlag for Türkiye

asked on

How to hook keyboard inputs globally in vb?

Hi,

I want hook the keyboard inputs made to all applications currently running on the systemby using visual basic. I have some examples but they hook only current thread's inputs. And also it is necessary to put these inputs into text box. Thus, I will be able to behave according to specified command typed by the user.
I know the mains of the this process but it is a little difficult for me to code it. If you help me in solving this problem I will be preciate.

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of imessvb
imessvb

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
Avatar of monarch_ilhan

ASKER

Hello again,
Thank you very much for you suggestions but unfortunatately it didnt work. May be there is a small trick which I couldnt see.
So,
Please could you check it again and inform me soon??

And for motivating you, I am increasing its point from 100 to 150.

Best Regards
Ilhan
Avatar of imessvb
imessvb

what part of it didn't work? did it give any error messages or it simply did not response? if you leave your email i can send my sample code to you.
oh, be sure you call StartHook() in Form_Load() event and EndHook() in Form_Unload() event, and your form should be named "frmMain" and has a public function whose signature is: KeyTyped(Byval KeyCode, Byval Shift)

(the code was originally written in win98, i'll check again to see if it works in win2k)
hHook = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf HookProc, 0&, 0&)

The return code of above function becomes Zero (0). So, StartHook process does not start. I did your suggestions but, since the SetWindowsHookEx does not start, it is unable to hook keyboard inputs. I think we may change the above function's parameters.

When I change tha functions as
hHook = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf HookProc, App.hInstance , 0&) it gives a return code and it only hooks in my application not globally.

my email address is monarch_ilhan@yahoo.com

I am looking forward to have your solution suggestions.

A problem with the code, is it's not in a DLL locally nor is it registered that way, so if say, someone hits the Windows key on the keyboard, that program goes dormant and stays that way (the hook is invalidated because the system can't find it after the process change).  I've run into the same situation, only means to fix that is to DLL the testing code and make the DLL locally registered and make sure the threadid is in the SetWindowsHookEx.  That way, any new threads that open up will also include that hook themselves (since a journalrecord is system wide, each thread will hook in if they don't have it present).

To hook the keyboard and mouse directly, you'd really need to write device drivers for them.

>hHook = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf HookProc, App.hInstance , 0&) it gives a return code and it only hooks in my application not globally.

This is what I meant by the threadid being present, if this was in a registered DLL, all future opened threads would also open this hook for their usage and unhook it when they leave.
Sorry monarch_ilhan, the code works fine in win98se, even not in a dll, but not in win2k, not matter acsii or unicode version of the APIs is used...

FunkyMeister, I'll try to put the code in a dll and test in win2k again, thanks for the explanation. btw, since hooking is not recommended except for debugging, do you know of any other methods to hook keyboard/mouse using existing APIs?
Actually, hooking them may not be "recommended", but any mouse driver out there (and Internet keyboard driver as well) does it.  Thought they all use DLL's because each thread must be able to get AT the hook, otherwise it's invalidated and the code stops working (the hook gets "canned").  I've seen it get canned on a 98 machine outside of a dll but not inside of one.  And using a program that shows tasks and module usage, I see my dll listed on every last task (which is what you want).  But the downside is, if you're "late" in the stream, you may be getting sloppy seconds from the system (and may be too late down the list of listeners) and be able to do very little but go, "Oh, thats nice."

What are you hooking the inputs for?  Might help me understand the need.
Avatar of DanRollins
Hi monarch_ilhan,
It appears that you have forgotten to close this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: imessvb and FunkyMeister

monarch_ilhan, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer