WindowsHookLib doesn't support the "Windows Key" as a modifier. Is this viable? Do you intend to develop that?
Thanks.
Hi pedromorfeu, I am not sure what you mean by “Windows Key”. Do you mean the window keyboard key? Just to be clear the WindowsHookLib supports all the keys. Can you be more specific about what you are trying to achieve?
Rating is a way of saying thank you. Don't forget to rate always!
Thanks for replying. Yes, I meant the windows keyboard key. What I want to get is to use that key as a modifier like Control, Alt or Shift in order to make combinations with other keys. Most of the times the windows keyboard key is used as a modifier (WIN+E to open explorer, for instance) but in programming it is not included in the modifiers. When I get a KeyDown event I need to know if windows key is also pressed.
Hi pedromorfeu, the WIN key is not a modifier in Visual Studio environment. The only modifiers are Alt, Control and Shift. But what I would sagest to do is having a class level Boolean variable let say called isWinDown that would be set to true in KeyDown event if the win key is pressed. And when it is released you set it false in the KeyUp event. In this way you can know the win key status at any time.
I hope this can be helpful.
Rating is a way of saying thank you. Don't forget to rate always!
Hi VBDT, thank you for this fantastic piece of code.
I'm trying to get you lib file to return the actual key that was pressed. Example, if I press the "4" key, I currently get "D4" & if I press the "f" key, it returns "F" (capital F). See below.
Code:
Private Sub kHook_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyBoardEventArgs) Handles kHook.KeyDown
My.Application.DoEvents()
Dim KeyThatWasPressed As String = e.KeyCode.ToString
MsgBox(KeyThatWasPressed)
My.Computer.FileSystem.WriteAllText("C:\Test.txt", KeyThatWasPressed, True)
End Sub
Forgive me my stupid question, but where are the LL* classes now? Or can I use the KeyboardHook / MouseHook classes now?
I have used version 1.1.0.2 before and want to upgrade it.
Also is it possible to catch mouse wheel events if using the scroll area of a touch pad? This haven't worked in the old version.
@VBDT:
Ok I just upgraded to the latest version and now the hook doesn't work:
I'm using both a keyboard and a mouse hook and I'm installing them properly but they aren't raising any events. =/ Secondly, sometimes upon trying to remove the hook, I get the following exception:
WindowsHookLib.KeyboardHookException: Invalid hook handle
at WindowsHookLib.KeyboardHook.RemoveHook()
Any help would be greatly appreciated.
Please rate helpful ppl's posts. It's the best 'thank you' you can give
Hi obi1kenobi,
I have updated the component to the version 1.1.0.7 some time ago and forgot to update it in here. I don’t know why I didn’t update here maybe for some reason but I can’t remember it if there is one. I normally update a component if I know there are no issues with it. Anyway, I have tested and updated the component so you can download the new version and see if it works without any problems. Unfortunately I don’t have 1.1.0.5 version to see what the problem is but I think one of the reasons that I have updated it to 1.1.0.7 might be for the problem you are having. So try the new version and let me know if it works without any problems.
Note the components namespace is changed from WindowsHookLib to WindowsHook so you will need to change it in your project. When you reference the new version in your project the name WindowsHookLib will be underlined in your code as it can’t find it. You just need to change it to WindowsHook.
Best regards,
VBDT
Last edited by VBDT; Mar 5th, 2009 at 04:40 PM.
Rating is a way of saying thank you. Don't forget to rate always!
I just updated to the newest version. The KeyUp and MouseUp events fire just fine, however the MouseWheel event is not raised when the mouse wheel is turned.
Please rate helpful ppl's posts. It's the best 'thank you' you can give
I just updated to the newest version. The KeyUp and MouseUp events fire just fine, however the MouseWheel event is not raised when the mouse wheel is turned.
Obi1kenobi thanks for testing. I think there is some thing is wrong with your project. Try to run the demo project I posted and see if the MouseWheel event fires. I tried it and I don’t see any problem with it.
Rating is a way of saying thank you. Don't forget to rate always!
I will do that as well, however what could be wrong with my project?
There is only one thing I can think of: this time I used a notebook computer and the scrolling action was due to the side region of the touchpad, so it was being synthesized by the mouse driver - there was no actual scrolling. Could this have anything to do with it?
Please rate helpful ppl's posts. It's the best 'thank you' you can give
I just downloaded the Demo and I'm sorry to say that it doesn't register the mouse wheel events either.
Btw, the dll is missing in your demo app, I had to download it separately.
Hmm, obi1kenobi I have downloaded the demo and the hook .dll and tested it. It works as it spouse to. Here is the screenshot of it. I really don’t know why it doesn’t work for you.
Rating is a way of saying thank you. Don't forget to rate always!
That really is weird... Btw I notice you used XP to test the app. ATM, I'm using Vista. Could that be the cause? Anything else I could do to fix the issue?
Please rate helpful ppl's posts. It's the best 'thank you' you can give
That really is weird... Btw I notice you used XP to test the app. ATM, I'm using Vista. Could that be the cause? Anything else I could do to fix the issue?
Maybe that could be a reason. I should see the specifications of the API functions for vista. Maybe there are some changes that I don’t know. If I find something, than I will post it here.
Rating is a way of saying thank you. Don't forget to rate always!
since im not too familiar with library's, could you explain me how the code should look if i want my timer to run all the time LMB is down and LMB is up again to stop the timer
After installing the hook, test the MouseDown event for the left mouse button and start the timer if so. In the MouseUp event, test for the left mouse button and stop the event. See the example given in post #1 for detailed info as to how to install the hook and handle the events.
Please rate helpful ppl's posts. It's the best 'thank you' you can give
My question would be, as per your other thread, what have you tried?
It is not enough to simply say that it does not work. Myself, Obi and VBDT have all used the library to do variations of what you are asking, so it definitely DOES work.
Here are the steps that you need to follow:
1) Add a reference to the library in your project (Right Click on the Project in the solution explorer and select Add Reference, and browse to the DLL)
2) Create two buttons and put the following in their click events:
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
'Install the mouse hook
Me.MouseHook1.InstallHook()
Catch ex As Exception
MessageBox.Show(ex.Message)
Form1.Log.WriteException(ex, TraceEventType.Error)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
'Remove the mouse hook
Me.MouseHook1.RemoveHook()
Catch ex As Exception
MessageBox.Show(ex.Message)
Form1.Log.WriteException(ex, TraceEventType.Error)
End Try
End Sub
And then you will need to create two events handlers similar to these:
Code:
Private Sub MouseHook1_MouseDown(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles MouseHook1.MouseDown
'Set the mouse down Handled property
e.Handled = Me.HandleMouseCheckBox.Checked And Not Me.mList.Contains(CType(sender, IntPtr))
'Print the mouse down data
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("===================== MouseDown")
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("===================== End MouseDown")
End Sub
Private Sub MouseHook1_MouseUp(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles MouseHook1.MouseUp
'Set the mouse up Handled property
e.Handled = Me.HandleMouseCheckBox.Checked And Not Me.mList.Contains(CType(sender, IntPtr))
'Print the mouse up data
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("===================== MouseUp")
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Button: " & e.Button.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Clicks: " & e.Clicks)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Delta: " & e.Delta)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Location: " & e.Location.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("Control Handle: " & sender.ToString)
Me.DisplayTextBox.AppendText(System.Environment.NewLine)
Me.DisplayTextBox.AppendText("===================== End MouseUp")
End Sub
These event handlers handle the events that are raised by the library. You will obviously need to remove the code which writes to the textbox, and rather start and stop your timer instead.
You will also need the following code to declare an instance of MouseHook:
Code:
Friend WithEvents MouseHook1 As WindowsHook.MouseHook
In the Button3_Click and Button4_Click event handlers, it would also be wise to check the proper state of the hook - you wouldn't want to try and install a hook which has already been installed.
Please rate helpful ppl's posts. It's the best 'thank you' you can give
Sure, there is nothing to stop you putting the installation of the hook somewhere else, but as obi mentioned, you should make sure that the hook isn't already registered.
Not meaning to be blunt, but if you have followed all the steps that I have lined out, then the application should be working.
To recap, here are the exact steps that I have just carried out, and the application is working as I expect it to....
1) Create a new project
2) Add a reference to the WindowsHookLib.dll
3) Added a New Object called MouseHook1 and a New Timer called myTimer
4) Created event handlers for the Form Load and Form Closing Event
5) Added code to Install and Remove the hook in these event handlers
6) Created the event handlers for the global mouse up and down events
7) Added code to Start and Stop the Timer in these event handlers
8) Ran the application
9) It works
Here is the final code (obviously, this is a basic version that doesn't account for error handling and which will need to be modified):
Code:
Public Class Form1
Dim WithEvents MouseHook1 As New WindowsHook.MouseHook
Dim myTimer As New Timer
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.MouseHook1.RemoveHook()
Me.MouseHook1.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MouseHook1.InstallHook()
End Sub
Private Sub MouseHook1_MouseDown(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles MouseHook1.MouseDown
myTimer.Start()
End Sub
Private Sub MouseHook1_MouseUp(ByVal sender As Object, ByVal e As WindowsHook.MouseEventArgs) Handles MouseHook1.MouseUp
myTimer.Stop()
End Sub
End Class
i followed those steps, and checked about 10 times the code but this keeps coming up when i try to start the form: NullReferenceException was unhandled
this was the problem (i finnaly got it working now):
firs i declared this:
Friend WithEvents MouseHook1 As WindowsHook.MouseHook
and now i used obi1kenobi's way
thx, works great
The sample application seems to have separated the instantiation of the MouseHook into two separate lines, hence, what I put in post #63 does not have the New Keyword, and that was an oversight on my part.
However, you will see that the code in post #70 does have the New Keyword declared, and copying and pasting of this code would have resulted in it working.