Page 2 of 3 FirstFirst 123 LastLast
Results 41 to 80 of 85

Thread: Global hooks

  1. #41

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    Quote Originally Posted by pedromorfeu
    Hi VBDT,

    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?

  2. #42
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Global hooks

    Hi VBDT,


    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.

    Thanks.

  3. #43

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    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.

  4. #44
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Re: Global hooks

    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
    Thanks in advance for your help.

  5. #45
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: Global hooks

    I had the same problem. But I just set up select case statement and i think it went something like this:

    Code:
            Select Case e.keycode
                Case d1
                    Dim KeyThatWasPressed As String = "1"
                    MsgBox(KeyThatWasPressed)
            End Select

  6. #46
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Re: Global hooks

    sounds pretty simple but LONG. If that what needs to be done then I'll have @ it. Thanks for your help

  7. #47
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: Global hooks

    It was long but it works and I couldn't think of any other way around it.

  8. #48
    New Member
    Join Date
    Jan 2009
    Posts
    1

    Re: Global hooks

    Hello,
    First of all thanks for this great work

    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.

    Thanks

  9. #49
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    @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

  10. #50

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    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

  11. #51
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    Will do. I have a lot of work to do at the moment, I'll update to the new version as soon as I can. I'll post the results here as well.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  12. #52
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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

  13. #53

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    Quote Originally Posted by obi1kenobi View Post
    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.

  14. #54
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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

  15. #55
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  16. #56

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    Quote Originally Posted by obi1kenobi View Post
    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.
    Attached Images Attached Images  

  17. #57
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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

  18. #58

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    Quote Originally Posted by obi1kenobi View Post
    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.

  19. #59
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    Thanks Your help is appreciated
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  20. #60
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    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

  21. #61
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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

  22. #62
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    i cant get it to work, if u know how exactly it should look plz post it here

  23. #63
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Hey,

    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
    Gary

  24. #64
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    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

  25. #65
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    can i put the hook to install on openning and close on exiting the form?

  26. #66
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Hey,

    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.

    Gary

  27. #67
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    ok, i put two buttons, and when i click to install hook this comes up:

    and, when not debugging theres no errors

  28. #68
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Hey,

    What does the IDE tell you is going wrong? There has got to be an error generated?!?

    What is it?

    Did you follow all the steps as I laid them out?

    Gary

  29. #69
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    it says: NullReferenceException was unhandled
    and, i followed all the steps u said

  30. #70
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Hey,

    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
    Gary

  31. #71
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    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

  32. #72
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Have you copied and pasted the code that I have just given you?

    I have literally just run that code, and it works fine.

    Gary

  33. #73
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    i wrote the SAME code earlier and it wont work

  34. #74
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    Try adding the New keyword when declaring the hook.

    Instead of
    Code:
    Dim MouseHook1 As MouseHook
    use
    Code:
    Dim MouseHook1 As New MouseHook
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  35. #75
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: Global hooks

    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

  36. #76
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Quote Originally Posted by Erco21 View Post
    i wrote the SAME code earlier and it wont work
    You are missing my point, you can't have written the SAME code!! All other things being equal, the code that I have posted works, your doesn't.

    Gary

  37. #77
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Global hooks

    @gep13: In post #63, you seem to have missed the constructor call and the MouseHook was never instantiated, thus leading to the exception.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  38. #78
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Global hooks

    Hey,

    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.

    Gary

  39. #79
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Re: Global hooks

    Is there any chance that this example could be updated to support VB 2010 ?

    Thanks!

  40. #80

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Global hooks

    Quote Originally Posted by Calab View Post
    Is there any chance that this example could be updated to support VB 2010 ?

    Thanks!
    Hi,
    I am not aware of any issues with framework 4. If there is one than provide the error please.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width