|
|
Comments and Discussions
|
|
 |

|
I see what you use for the mouse hook,why not SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,(HINSTANCE)g_hModule,nCode);
looks like the WH_MOUSE should be better than WH_GETMESSAGE?
Many thanks for your sample
hook = SetWindowsHookEx( WH_GETMESSAGE, (HOOKPROC)MouseMsgProc,
hInst, 0);
|
|
|
|

|
How to Disable these keys.
|
|
|
|

|
Hi VBDT,
I've tried to use this under Windows Form and it all work fine. But when I had tried it to Windows Service, it does not work. Do you have any idea what I had missed?
I have configured it as running with logon as Local System and Enable to interact with desktop.
Please, I need your expert guidance on this.
|
|
|
|
|

|
very nice project,i vote of 5
|
|
|
|

|
I am wondering is it possible to use this global hook lib in a windows service program?
Thx a lot.
|
|
|
|

|
FINALLY! Someone has a perfect example of a MouseHook. Thank you so much. My question is I am using this in conjunction with a screen shot function, is there a way to disable 'mouse selection'? What I mean is as I 'MouseDown' and then 'MouseMove' it selects whatever is within the 'box'. Whether it be desktop icons, or text. I just want to disable that for a second until I 'MouseUp'. Is that even possible? Thanks again for providing a great example of hooks.
|
|
|
|

|
This is exactly what I am looking for. But I can not get it too work in an InterOP dll. I keep getting an "Invalid Procedure call or argument" on the InstallHook call.
Any ideas?
Thanks
Dave
|
|
|
|
|
|

|
I really enjoyed this, not only for technical merit, but for demonstrating .net use in multiple languages.
|
|
|
|

|
Hello Sir,
I have generate a problem.The Problem is when i am changing my keyboard Language to Russian (From Control Panel - Regional and Language Settings -Keyboard Settings). Then my keyboard shows russian character but Keyboard Hook Capture a US format.
Thanks in advanced.
Thanks
If you can think then I Can.
|
|
|
|

|
Hi,
Well, that is not a problem of the hook. Hook uses and returns Dot Net "Keys" enumeration object in event handler parameter and there are no Russian keys in there. If you want to get the Russian chars, than you need to do the conversion yourself.
Regards,
VBDT
|
|
|
|

|
You can capute the Keystrock according to selected Keyboard Layout.
With the use of below function API u can use the unicode with ur Keyhook.
MapVirtualKeyEx
ToUnicodeEx
If you can think then I Can.
|
|
|
|

|
I am running Windows 7 x64 and .NET 4.0. The demo works fine for me, but I am having trouble getting it to working in my own project. I had to convert it to Any CPU in order for it to be compatible with the rest of my project, but it is not reporting anything. I have told it to install the hooks. Do you know what the problem is and how I can fix it?
|
|
|
|

|
The dll is 32 bit, there for you should have your project set to x86 not Any CPU. x86 will work on 32bit and x64 systems thanks to a layer (WoW64) that Microsoft added to x64 systems so that they can run 32bit applications.
http://en.wikipedia.org/wiki/WoW64[^]
|
|
|
|

|
I have done that, but it still does not work
Here is my code. Perhaps you could discern the problem?
Dim WithEvents MainMouseHook As New MouseHook()
Private Sub QuickBulletin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MainMouseHook.InstallHook()
End Sub
Private Sub MyMouseHook_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MainMouseHook.MouseDown
Debug.Print("Global Click")
End Sub
My Debug panel never prints "Global Click". I have looked at your demo application (which works), but I can't find the difference. Thanks for your assistance
|
|
|
|

|
Hi,
If you are using the previous version of the hook and framework 4.0 than it will not work. The previous version of the hook works on 3.5 and above. The current assembly version of the dll is 1.1.1.3. Download it by clicking on the "Download latest version" or product website on top of this form.
|
|
|
|

|
I just figured it out. It was not a problem with the library. Somehow, the Define Debug constant got unchecked in my project at one point so it was not printing. Thanks for the help though!
|
|
|
|

|
Hi VBDT,
I have been using your component for a while on WinXP and it has worked perfectly. When we started using it on Win7 everything appeared fine but as we tested it harder we stop receiving events from the ‘MouseMove’ event after several minutes. After some additional testing we noticed the same thing with the ‘KeyDown’ event. The mouse and keyboard event do not stop working at the same time and fail in no specific order. Again, everything works perfectly at first and then randomly after a few minutes events stop firing, only on Win7.
When we notice the mouse events are no longer firing, I added a button that executes the following 2 lines and everything works for a few more minutes.
gmh = New MouseHook
gmh.InstallHook()
When we first noticed the problem we were using the 1.0 version of your component with the .Net 2.0 framework. Now we are using 1.0.0.7 and .Net 3.0 framework and experience the same issues.
I am not seeing any exceptions being thrown or anything in the event viewer. Any help would be much appreciated.
- Jon
|
|
|
|

|
Hi Jon,
What kind of tests are you performing? I will test it at my work on windows 7 and will post the results in here.
|
|
|
|

|
Our testing is pretty simple it is just clicking around in our application but it needs to be done for several minutes. We have tested on 4 different Win7 computers and experienced the same result on all of the computers. I am willing to send you the application and detailed instructions if you will send me your email address. We should keep all of the technical info as part of this thread but some of the private will need to be taken off line. Please contact me at jon.gross < at > talario < dot > com
|
|
|
|

|
I have a simple test program that is able to reproduce the error. It appears that when the computer is under load the error appears. Then when I close the program and release the hook I get an "Invalid hook handle". Glad to send you the source code if that would help you.
|
|
|
|

|
Hi,
Make sure you have the latest version of the hook. You can get the latest version of the hook by clinking the "Download latest version" link above.
|
|
|
|

|
Hi Jon,
I have tested the hook demo on windows 7 at my work for hours and have not seen the problem you are describing. Try the hook demo project and see if you encounter the problem. The problem could be system related since my tests do not show any problem whit the library.
You can download the demo project from my website http://www.code2point.com/Project.aspx?proj=4[^]
VBDT
|
|
|
|

|
This is a known issue with all low level hooks (mouse and keyboard) in win 7. So this bug has nothing to do with this proposed hooking class here at all.
The solution I discovered for my real world freeware "Transmiti" (www.transmiti.org) is a extra thread which checks permanently the hooks and rehooks automatically in case the hook is lost.
It is an not so easy task as it sounds, because you have to be aware of some important details, like a call to the windows api function SystemParametersInfo with the parameters (SPI_SETBLOCKSENDINPUTRESETS,TRUE,NULL,0) to set the screensaver timer not to be reseted by a virtual keystroke which I send every one second to check the low level keyboard hook.
If you are interested in my real world source I can post some code snippets here.
|
|
|
|

|
Dear VBDT,
Now I succed to make your demo working (at home on a Win7), but I am now stucked with the SetWindowsHookEx call when I set he WH_CBT flag.
I got a "Impossible d’établir un hook non local sans un descripteur de module
" message (meaning moreorless: Impossible to set a global hook without a module description).
Can I ask you if you can try just to try modify the SetWindowsHookEx call and let me know if you experience any problem?
Many thanks in advance
Paolo
|
|
|
|

|
Hi Paolo,
It seems that in framework 4 getting module handle with 'Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);' returns the wrong handle id. This was not a problem in framework 3.5, it seems things got changed or it is a bug in framework 4. Anyways, I changed that line above to an API (GetModuleHandle) function call that gets the right module handle. You can download the updated version of the assembly (1.1.1.3) from 'Download latest version' (my website) link on the top of this page. I don't have the project solution in there but only the code and assembly. If you want the project than you can download it from this page but you need to change the above line to the api function as I have discussed.
For installing WH_CBT hook I can only give you a reference link to the (CBTProc) Callback function that you should use to get notifications. Just read the specifications and try to do it. Now I don't have time for it, I am sorry.
Good luck!
modified on Saturday, October 16, 2010 10:27 PM
|
|
|
|

|
Dear VBDT,
first of all, let me thank you for your help.
Yesterday, I tried to follow your instructions and the hook now work without any problem.
Then I did a further step and I just changed your hook parameter in the SetWindowsHookEx call where I replaced UnsafeNativeMethods.WH_KEYBOARD_LL with the WH_CBT value.
Unfortunately, as result of this "setup" call, I got a "Impossible d’etabilir un hook non local sans un descripteur de module" (impossible to set a non-local hook without a module descriptor ??)
I tried as well with with UnsafeNativeMethods.WH_KEYBOARD param but I got the same.
Here I am stucked again, have you another hint?
Many thank in advance,
Paolo
|
|
|
|

|
Hi,
really a great job.
I have a question: it is possible as well to make a global hook that intercepts other hook types (e.g. the WH_CBT events)? I would like use it to detect when a new window is opening/closing.
I am a little bit confused, because some says that is possible some other don't.
In case, do you have any plan to integrate this in your project?
Many thanks in advance,
Paolo
|
|
|
|

|
Hi Ambaraba111,
Currently I don't have time to add other hooks to the library but the library project is provided so you can extend it if you want.
Check the API SetWindowsHookEx function. This function takes hook type (flag) like WH_CBT which will Install a hook procedure that receives notifications useful to a CBT application.
|
|
|
|

|
Hallo VBDT,
thank you for your answer.
I started once again from your demo, thet I converted it in VB2010 and c#2010 without any problem.
I complied the .DLL project without any hassle and then in the VB project I added a reference to the new DLL (in \bin\x86\Release directory)
When I run the application and I hook the mouse, I get an (error) mesaage saying "The operation completed succesfully". The problem is that the hook is not activated (and in the groupbox header it shows "Mouse hook unistalled!"
The same happens with the keyboard hook, but it works when I hook the clipboard.
I tried as well to point to your original DLL, but the results are the same.
I am currently using XP professional version 2002 SP 3
Have you any hint? Many thaks in advance
Paolo
(update) the problems is in the
this._hMouseHook = UnsafeNativeMethods.SetWindowsHookEx(...)
that returns 0, but I don't know how to make it work ...
modified on Thursday, October 7, 2010 7:49 AM
|
|
|
|

|
Hello
Really it’s nice article. Thanks for publish such type of article. It’s really imagine to seen such type of nice article.
I ask small question when I type small 'a' then show capital 'A' as well as when type capital 'A' then also show capital 'A'. Can we difference Capital and small.
|
|
|
|

|
Unfortunately the key down event gets the capital letter code only. The only way to know that the key is lower case or not is to check if the shift key or cops lock keys are down.
|
|
|
|
|

|
Thanks
|
|
|
|

|
Can I use your DLL to Hook Keyboard and mouse events only in my application?
Regards
|
|
|
|

|
I am not sure that I understand your question. Can you give more information?
|
|
|
|

|
I would prevent some KeyBoard and mouse events.
For example:
I would intercept the F1 keypress to open application Help in all opened forms, without manage KeyPreview in every opened forms.
I would intercept the CTRL+TAB combination such as in Visual Studio.
I would LogOff my application if there's no user activity (by using Mouse or KeyBoard) for 2 minutes, without manage KeyPreview and mouse move in every opened forms.
...
I hope my thinking is clearer
Thank u
Ps. Sorry for my poor english
|
|
|
|

|
The mouse and the keyboard hooks are global so yes you can prevent key combination to be passed to the other applications. Check the demo project to see how it is done.
|
|
|
|

|
Of course, but if I start, for example, the Hook of CTRL+TAB combination it is intercepted in all opened application. For example, if I'm writing in Word application and press CTRL+TAB, the Hook activated in my application takes control, instead I would intercept only if CTRL+TAB is pressed in my application.
I do not know if it makes the idea
Thank for reply, Luka
|
|
|
|

|
The Keyboard hook is a global hook (low level hook) there for the key down message is passed to the hook before any window.
If you want to set a key combination for your application than it makes sens to set it as a hotkey for your application. I created a class that will help you to set hotkeys for applications. Here is the link to the Hotkeys class. You can use and register a hotkey in your application and handle the "HotkeyPressed" event in your main form where the Hotkeys object is declared.
Or if you still want to use the Keyboard hook than you need to check to see if your application is active (has focus) when the key combination is down.
As far as I understand, you need to register a hotkey for your application. This dll can be used to achieve what you want as I mentioned above but this is not the best way of doing it. The right way is to register a hotkey.
modified on Monday, October 4, 2010 8:02 PM
|
|
|
|

|
Hey,
I like the look of this, must try it!
Anthony
|
|
|
|

|
Hi,
I have just tried to run your demo with VB 2010 and an error is thrown:
'Module not found'?
Is there a fix or new version for solving this problem?
|
|
|
|

|
Yes this is REALLY annoying.
No comments?
|
|
|
|

|
After some tinkering I found the problem...
KeyboardHook.cs, Line 146:
//this._hKeyboardHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_KEYBOARD_LL, this._keyboardProc, hinstDLL, 0);
Change to:
this._hKeyboardHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_KEYBOARD_LL, this._keyboardProc, IntPtr.Zero, 0);
MouseHook.cs, Line 191:
//this._hMouseHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_MOUSE_LL, this._mouseProc, hinstDLL, 0);
Change to:
this._hMouseHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_MOUSE_LL, this._mouseProc, IntPtr.Zero, 0);
|
|
|
|

|
The suggestion is not correct. The API specification states:
"hMod [in]
HINSTANCE
A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process."
This means that you can't have "IntPtr.Zero, 0" for the last two parameters. If the first parameter of these two is IntPtr.Zero than the second one should contain a tread ID not zero. This is an assembly (dll) and the procedure is in it. there for the assembly handle should be specified and not be IntPtr.Zero.
|
|
|
|

|
VBDT wrote: The suggestion is not correct. The API specification states:
Doesn't really matter if it's correct or not, it works.
Unless you can provide me a build that works in the .Net 4.0 framework as you suggest, then being "correct" is irrelevant.
Can YOU get it to work under .Net 4.0 as you describe? If so than there is another bug somewhere else.
|
|
|
|

|
The component works with .net 4.0. Download it from the latest version link on the top of this page. The new version is 1.1.1.3.
|
|
|
|

|
The VS version I am using is 2008. You might want to check the reference path of the library under your project properties. It might be pointing to a bad path.
modified on Sunday, October 3, 2010 9:38 PM
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
A single component that contains various Windows hooks
| Type | Article |
| Licence | CPOL |
| First Posted | 3 Aug 2007 |
| Views | 161,268 |
| Downloads | 3,856 |
| Bookmarked | 209 times |
|
|