PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Global hooks-VBForums
Page 1 of 3 123 LastLast
Results 1 to 40 of 81

Thread: Global hooks

  1. #1

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

    Thumbs up Global hooks

    Some times I update the component and post the updated date and the assembly version number so it is recommended to check it once a while.
    I created this project as a single utility to hook the mouse, keyboard and the clipboard system wide. It is a library file 'WindowsHookLib' dll that can be referenced into various projects. The mouse and keyboard hooks are low level so you can use the 'Handled' property of the 'WindowsHookLib.MouseEventArgs' or the 'WindowsHookLib.KeyEventArgs' to prevent the windows messages be passed to the other applications. Note you need to use the dll file not the classes in your projects; otherwise they might not work correctly.

    Note, if you use any of these hooks than you MUST call their dispose methods when the application exits. The method will remove the hook and free the memory that is used by the hook component.

    MOUSE HOOK

    The 'LLMouseHook' class from 'WindowsHookLib' is designed to hook the mouse globally and fire some useful events. Since this hook is low level and low level mouse hook doesn't get MouseClick and MouseDoubleClick messages it simulates these events.
    I provided the 'Handled' property to 'MouseDown', 'MouseUp', 'MouseWheel' and 'MouseMove' event handlers. If you decide to set the 'Handled' property in the 'MouseUp' event then it is recommended to set it in the 'MouseDown' event as well for the application performance.

    Warning! If you set the 'Handled' property in the mentioned events unconditionally than you might not be able to use the mouse. You can check the 'WindowsHookLib Demo Project' examples to see how you can condition the mouse handled process.

    Class methods:
    • InstallHook – Installs the mouse hook globally (has no parameters).
    • RemoveHook – Removes the mouse hook (has no parameters).
    • Dispose – Unhooks the mouse (if it is hooked) and frees the memory used by the component (has no parameters).
    • SynthesizeMouseDown - Synthesizes a mouse down event system wide.
    • SynthesizeMouseUp - Synthesizes a mouse up event system wide.
    • SynthesizeMouseWheel - Synthesizes a mouse wheel event system wide.
    • SynthesizeMouseMove - Synthesizes a mouse move event system wide.

    Class properties:
    • State – Gets the hook state.

    Class events:
    • StateChanged – Fires the event if the mouse hook is installed/uninstalled.
    • MouseDown – Fires the event if the mouse button is down.
    • MouseMove – Fires the event if the mouse is moved.
    • MouseUp – Fires the event if the mouse button is up.
    • MouseWheel – Fires the event if the mouse vertical/horizontal wheels are rotated.
    • MouseClick – Fires the event if the mouse is clicked.
    • MouseDoubleClick – Fires the event if the mouse is double-clicked.


    KEYBOARD HOOK

    The 'LLKeyboardHook' class from 'WindowsHookLib' can be use to hook the keyboard globally.

    Class methods:
    • InstallHook – Installs the keyboard hook globally (has no parameters).
    • RemoveHook – Removes the keyboard hook (has no parameters).
    • Dispose – Unhooks the keyboard (if it is hooked) and frees the memory used by the component (has no parameters).

    Class properties:
    • State – Gets the hook state.
    • AltKeyDown – Gets a Boolean value indicating if the ALT key is down.
    • CtrlKeyDown – Gets a Boolean value indicating if the CTRL key is down.
    • ShiftKeyDown– Gets a Boolean value indicating if the SHIFT key is down.

    Class events:
    • StateChanged – Fires the event if the keyboard hook is installed/uninstalled.
    • KeyDown – Fires the event if a keyboard button is down.
    • KeyUp – Fires the event if the keyboard button is up.


    CLIPBOARD HOOK

    The 'ClipboardHook' class from 'WindowsHookLib' can be use to add a window to the clipboard chain and fire clipboard changed event.

    Class methods:
    • InstallHook – Adds a window to the clipboard chain (has no parameters).
    • RemoveHook – Removes the window from the clipboard chain (has no parameters).
    • Dispose – Removes the window from the clipboard chain and frees the memory used by the component (has no parameters).

    Class properties:
    • State – Gets the hook state.
    • HWnd – Gets the hooked window handle.

    Class events:
    • StateChanged – Fires the event if the clipboard hook is installed/uninstalled.
    • ClipboardChanged– Fires the event when the clipboard contents is changed (redrawn).


    The code examples are in the demo project with comments. You can run the project and test the hooks before you use them. If you need the methods' discriptions, than you need to copy the 'WindowsHookLib.xml' file into your project folder too.
    By the way, the mouse hook and the clipboard hook are used in the Easy Screen Shot tool app. You can see the main use of mouse hook when you capture an object (window, control, windows shortcut menus etc.) images. The clipboard hook is used to enable the clipboard captur button.

    Update History:

    Assembly Version 1.1.0.7
    File Version 1.0.0.1
    • Some internal changes.
    • The namespace WindowsHookLib is changed to WindowsHook.

    Assembly Version 1.1.1.0
    File Version 1.0.0.4
    • Minor internal improvements.

    Download the latest version here
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Re: Global mouse hook

    Quote Originally Posted by VBDT
    This class is designed to hook the mouse globally and fire some useful events. The class uses Api functions to hook the mouse. Since this hook is low level and low level mouse hook doesn’t get MouseClick and MouseDoubleClick messages the class simulates these events.

    Why do you say low level mouse hook doesnt capture click events? Sure it does. Not only single and double clicks, but also the delta value for scroll wheel. Your class to simulate this is not needed.
    Last edited by Traps; Apr 7th, 2007 at 04:17 PM.

  3. #3

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

    Re: Global mouse hook

    Quote Originally Posted by Traps
    Why do you say low level mouse hook doesnt capture click events? Sure it does. Not only single and double clicks, but also the delta value for scroll wheel. Your class to simulate this is not needed.
    Because it doesn’t!
    This is the only windows massages that the window gets when it hooked low level
    wParam
    [in] Specifies the identifier of the mouse message. This parameter can be one of the following messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_RBUTTONDOWN, or WM_RBUTTONUP.
    MSDN

  4. #4
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Re: Global mouse hook

    Quote Originally Posted by VBDT
    Because it doesn’t!
    This is the only windows massages that the window gets when it hooked low level
    MSDN

    Just debugged my hook program and your right!!! Looks like I could remove those from my select case statement for checking mouse messages. Cant believe I didnt notice that when I wrote my program.


    Sorry. Maybe a MOD can delete our posts. Dont mean to junk up your thread.

  5. #5
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Re: Global mouse hook

    How can i see the difference if im rolling up or down with my mouse?

    the e.Delta is the same when i roll up as down. (always 424)

  6. #6

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

    Re: Global mouse hook

    The Delta value is the value that the window gets by the WM_MOUSEWHEEL message. The value should be the same except that if you roll up it is positive and when you roll down it is negative.
    The value should be 120 or -120 roll up and down respectivly. Now, if you want to get the total rolled value you can have a class level variable and add the delta value to it in the MouseWheel event. Here is an example:
    vb Code:
    1. Imports WindowsHookLib
    2.  
    3. Public Class Form1
    4.  
    5.     Dim WithEvents gmh As New LLMouseHook
    6.     Dim delta As Integer
    7.  
    8.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    9.         'Dispose the object (it will uninstall the hook first)
    10.         Me.gmh.Dispose()
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         Try
    15.             'Install the mouse hook
    16.             Me.gmh.InstallHook()
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message)
    19.         End Try
    20.     End Sub
    21.  
    22.     Private Sub gmh_MouseWheel(ByVal sender As Object, ByVal e As WindowsHookLib.MouseEventArgs) Handles gmh.MouseWheel
    23.         'Add the delta value to the variable
    24.         Me.delta += e.Delta
    25.         Console.WriteLine(Me.delta)
    26.     End Sub
    27. End Class

  7. #7
    New Member
    Join Date
    May 2007
    Posts
    3

    Re: Global mouse hook

    Thanks for the code, it works great, except that it seems to return the same sender handle regardless of what is actually clicked.

    Any ideas?

  8. #8

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

    Re: Global mouse hook

    Quote Originally Posted by slk486
    Thanks for the code, it works great, except that it seems to return the same sender handle regardless of what is actually clicked.

    Any ideas?
    What kind of control is it? My guess is that it is a User Control that has some controls in it. In this case the sender has the handle of the User Control not the controls that it includes.

  9. #9
    New Member
    Join Date
    May 2007
    Posts
    3

    Re: Global mouse hook

    Thanks for your quick reply

    The app uses normal VB forms and a context menu opened from a notfyicon in the tray.

    Even when i Click elsewhere on the desktop, the sender handle is the same..

    The main form is hidden and only the tray icon is visible. when the user clicks the tray icon a context menu appears and from the context menu the user can open a form. It doesn't matter if I click the menu, the icon, the form or in any other (other apps) window on the desktop.

  10. #10

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

    Re: Global mouse hook

    Really, that sounds weird. In the class I use the ‘WindowFromPoint’ Api function to get the handle of the control. If it gives the same handle for everything you click then there must be some problem with your project or with Visual Studio I am not sure.
    Have you tried the demo I have posted? Does it work as it spouse to?
    Also I would suggest you to install the Screen Shot application in my signature and try to capture objects for a test. The app uses this class to capture controls and when you move the mouse, a red rectangle is being drawn around the controls which were passable to do with the Api function I mentioned above.

  11. #11
    New Member
    Join Date
    May 2007
    Posts
    3

    Re: Global mouse hook

    OK...

    The demo gave me the window full of buttons that I could navigate with the keyboard and find the correct 2, to unlock the mouse (no mouse movement at first, then no mouse clicking) - i believe that is the intended functionality?

    The Screen Shot app was rather weird. First time i couldn't click anything, then all of a sudden a red border appeared around a window and I could capture window images but not click anything in the Screen shot menu (i can click the images and they appear pressed, but nothing happens). Second time I could capture the windows I clicked on - no red border however, and I couldn't click anything in the Screen shot menu - not even cancel capture.

    Had to kill the process.

    Could this have anything to do with the fact that I'm using Windows XP x64 or dual monitors?

  12. #12

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

    Re: Global mouse hook

    Quote Originally Posted by slk486
    OK...

    The demo gave me the window full of buttons that I could navigate with the keyboard and find the correct 2, to unlock the mouse (no mouse movement at first, then no mouse clicking) - i believe that is the intended functionality?

    The Screen Shot app was rather weird. First time i couldn't click anything, then all of a sudden a red border appeared around a window and I could capture window images but not click anything in the Screen shot menu (i can click the images and they appear pressed, but nothing happens). Second time I could capture the windows I clicked on - no red border however, and I couldn't click anything in the Screen shot menu - not even cancel capture.

    Had to kill the process.

    Could this have anything to do with the fact that I'm using Windows XP x64 or dual monitors?
    In the demo at least you should be able to move the mouse inside the form and be able to click some not all the buttons. If you can’t then there has to be some problems with the fact that you mentioned. I am using xp too so that shouldn't be problem but i am not sure about x64 and dual monitors!

    The Screen Shot app works great in my computer and the others I tested. The problem you are having is effecting to Screen Shot too.

    One more suggestion, in the event that the sender is giving the same handle fore windows and controls use the ‘GetLastApiError’ function of the ‘GlobalMouseHook’ class to retrieve the last error accrued in the Api dlls and see what it says if there is one.

    One more thing! I posted the dll file 'WindowsHook' that is basicly the same code except the dll gets the module handl by reflection instead of Api wich in your case it may have giving a wrong one. Also the MS documentation states that it must be in the dll.

  13. #13
    New Member
    Join Date
    Jul 2007
    Posts
    1

    Re: Global hooks

    Hello VBDT

    I have a question regarding to your last post, saying "One more thing! I posted the dll file 'WindowsHook' that is basicly the same code except the dll gets the module handl by reflection instead of Api wich in your case it may have giving a wrong one. Also the MS documentation states that it must be in the dll.";

    Is this the reason I cannot produce a single assembly (without WindowsHookLib.dll)? I've copy/pasted the files in WindowsHookLib to Test folder but it throws an exception at ghk.InstallHook.

    Or do I miss something else?

  14. #14

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

    Re: Global hooks

    Quote Originally Posted by meraydin
    Hello VBDT

    I have a question regarding to your last post, saying "One more thing! I posted the dll file 'WindowsHook' that is basicly the same code except the dll gets the module handl by reflection instead of Api wich in your case it may have giving a wrong one. Also the MS documentation states that it must be in the dll.";

    Is this the reason I cannot produce a single assembly (without WindowsHookLib.dll)? I've copy/pasted the files in WindowsHookLib to Test folder but it throws an exception at ghk.InstallHook.

    Or do I miss something else?
    I mentioned it in the introduction in #1 post
    "Note you need to use the dll not the classes in your projects; otherwise they might not work correctly."
    All you need to do is to copy the two files (WindowsHookLib.dll & WindowsHookLib.xml) in the "WindowsHookLib.zip" in to your project folder and add reference to the "WindowsHookLib.dll" file from your project and add "Imports WindowsHookLib" on top of your class or form.

  15. #15
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Global hooks

    How would one get the raw wparam and lparam? I need to 'copy' a wm_mousewheel in order to send it elsewhere but reconstructing the parameters from e.X, e.Y, e.Button etc seems kind of tricky.
    VB 2005, Win Xp Pro sp2

  16. #16
    Lively Member 5applerush's Avatar
    Join Date
    Nov 2006
    Location
    Shelby, Mi
    Posts
    98

    Re: Global hooks

    Hi VBDT,
    I was in need of a keyboard hook procedure, and this class is superior to anything I would have been able to come up with myself. So firstly, thanks for posting it.

    If I many ask a few questions:

    1.) In this class, you use the WH_KEYBOARD_LL hook for the keyboard. This hook is available only as a global hook, whereas the WH_KEYBOARD hook is available as a thread specific, or global - so at first glance, it sounds like the latter is a more versatile version. The help docs don't offer any information (at least initially) as to the difference. Are there any advantages to the _LL version?

    2.) The docs say to use global hooks only when necessary. Could two thread-specific hooks be used in place of one global hook if an app involves two separate processes? (mine is a cad-like app with separate procs for menu and window).

    Thanks very much,
    Danny

  17. #17

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

    Re: Global hooks

    Hi 5applerush,
    1) I would say the only advantage I see is that the LL hook gets a keyboard message before any window, where else none LL hook gets the message when the window got the message and will process it. Honestly, I don’t see any reason for creating thread specific hook since with framework 2 maybe in earlier versions you can set the form’s KeyPreview property which will fire when a key is down or up regardless what control has the focus, which is similar to the thread specific hook. But there is no way to get the keyboard messages regardless what app has the focus unless using LL keyboard hook.

    2) Yes, they could, you can do it the way I described above or you can write thread specific keyboard hook but then there is no good reason for it.

    VBDT

  18. #18
    Lively Member 5applerush's Avatar
    Join Date
    Nov 2006
    Location
    Shelby, Mi
    Posts
    98

    Re: Global hooks

    Ahhh, I think I understand. At first I couldn't quite see the difference after reading over the descriptions of each on MSDN. After your post I read them again, and noticed that the callback procedure for LL returns much more information in wParam and lParam - namely the KBDLLHOOKSTRUCT. Is this accurate?

    I also have a noob question: The amount of code in your module is much more extensive than any other examples of hooks that I've found. Everything seems to be set up quite formally. Is this the type of code that one writes when they wish to "productionize" their code? I've always wondered because I often see working examples of concepts where the author throws in a disclaimer like, "This isn't production code, but it works..."

    Thanks,
    Danny

  19. #19

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

    Re: Global hooks

    Yes, I forgot to mention that LL hook returns a structure with the info. My component is based on the info values and they are very accurate.

    As why my code Is more then others, simply because I covered every possible angle, also you might noticed that the component fires to additional events “Mouse Click” and “Mouse DoubleClick” which I have never seen in any other mouse hook example. Also I added some attributes to the methods and classes to make the component more professional. I just tried to make the component close to .net components as much as it is possible.
    Did I have to apply this attributes? No, but I wanted to do it so that you can see how it is done. And yes, if you want to make a professional component then you should apply attributes to your methods and classes to make the componen as close to .net components as it is posible. The reason is that most of the developers are familiar with the .Net classes and methods and I think that they like to see components that behave the same way as in .Net (this includes naming, applying attributs etc.)

  20. #20
    Lively Member 5applerush's Avatar
    Join Date
    Nov 2006
    Location
    Shelby, Mi
    Posts
    98

    Re: Global hooks

    Thanks for the help VBDT. I'm out of reputation right now, but I'll certainly pass some along when I can! And thanks again for posting this excellent code.

    Danny

  21. #21
    Junior Member
    Join Date
    Mar 2006
    Posts
    22

    Re: Global hooks

    Wonderful! Thank you very much

  22. #22
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Re: Global hooks

    Im trying to use this in a C# project. Works fine.

    ...Except my project is whining for a signed assembly.

    IS there anyway you can compile and send me signed version.

    TIA

  23. #23

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

    Re: Global hooks

    Quote Originally Posted by cablehead
    Im trying to use this in a C# project. Works fine.

    ...Except my project is whining for a signed assembly.

    IS there anyway you can compile and send me signed version.

    TIA
    Hi, I updated the WindwosHookLib with a signed assembly version. Pleas let me know if it works.

  24. #24
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Re: Global hooks

    I managed to compile a signed version. Works great.

    Now I need to hunt down info on wheel "ticks"...

    I need to hold off on the wheel event until I can procress how many wheel turns there are before setting a value.

    Thanks

  25. #25
    New Member
    Join Date
    Feb 2008
    Posts
    3

    Re: Global hooks

    Thanks for library and simple demo.

    I'm trying to do like that thing :

    vb Code:
    1. Private Sub MouseHook_(ByVal sender As System.IntPtr, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook.MouseDown, MouseHook.MouseClick, MouseHook.MouseUp
    2.         If Handle = MouseHook.MouseDown Then
    3.             TextBox1.Text &= "MouseDown" & vbCrLf
    4.         ElseIf Handle = MouseHook.MouseClick Then
    5.             TextBox1.Text &= "MouseClick" & vbCrLf
    6.         ElseIf Handle = MouseHook.MouseUp Then
    7.             TextBox1.Text &= "MouseUp" & vbCrLf
    8.         End If
    9.     End Sub

    That code just explain what i want.
    Will be like that thing too :

    Private Sub MouseHook_(ByVal sender As System.IntPtr, ByVal e As WindowsHookLib.MouseEventArgs) Handles MouseHook.MouseDown, MouseHook.MouseClick, MouseHook.MouseUp
    TextBox1.Text &= Handle & vbCrLf
    End Sub

    But i can't figure out how can i do.

  26. #26
    New Member
    Join Date
    Mar 2008
    Posts
    2

    Re: Global hooks

    I have discovered that if there is a long time operation during event keydown the ALT ( may be other control key ) seems pressed in the next keydown event .
    Code sample is more easy I change the demo KeyDown event adding a time consuming on ALT F2 .
    If I press ALT-F2 the next F2 comes with ALT also if press only F2 without ALT.
    If I press ALT alone the situation reset normal.

    Here the piece of code I change

    Private Sub KeyboardHook_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles KeyboardHook.KeyDown

    'Set the key down Handled property
    e.Handled = Me.HandleKeyboardCheckBox.Checked
    If e.KeyCode = Keys.F2 And e.Alt Then
    Me.DisplayTextBox.AppendText(Environment.NewLine)
    Me.DisplayTextBox.AppendText("===================== Sleep 1000")
    System.Threading.Thread.Sleep(1000)
    End If
    .
    .
    .
    I hope I have well explanied .

  27. #27

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

    Re: Global hooks

    Quote Originally Posted by Lionsitaly
    I have discovered that if there is a long time operation during event keydown the ALT ( may be other control key ) seems pressed in the next keydown event .
    Code sample is more easy I change the demo KeyDown event adding a time consuming on ALT F2 .
    If I press ALT-F2 the next F2 comes with ALT also if press only F2 without ALT.
    If I press ALT alone the situation reset normal.

    Here the piece of code I change

    Private Sub KeyboardHook_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles KeyboardHook.KeyDown

    'Set the key down Handled property
    e.Handled = Me.HandleKeyboardCheckBox.Checked
    If e.KeyCode = Keys.F2 And e.Alt Then
    Me.DisplayTextBox.AppendText(Environment.NewLine)
    Me.DisplayTextBox.AppendText("===================== Sleep 1000")
    System.Threading.Thread.Sleep(1000)
    End If
    .
    .
    .
    I hope I have well explanied .
    Hi Lionsitaly,

    According to MS the global hook subs should not have long time operations. If they do then according to them windows pass the message to the other applications without waiting to return from the sub. In this case the sub is the keydown event handler. Anyways, I like to examine the problem and want to ask you for the exact steps to produce the problem.
    Thanks VBDT.

  28. #28
    New Member
    Join Date
    Mar 2008
    Posts
    2

    Smile Re: Global hooks

    ok many thanks for support !
    I try !
    Open change the event keydown in this way

    Code:
       Private Sub gkh_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles gkh.KeyDown
    
            'Set the key down Handled property
            e.Handled = Me.HandleKeyboardCheckBox.Checked
            If e.KeyCode = Keys.F2 And e.Alt Then
                Me.DisplayTextBox.AppendText("===================== Sleep 1000")
                System.Threading.Thread.Sleep(1000)
            End If        'Print the key down data
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("===================== KeyDown")
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyCode: " & e.KeyCode.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyValue: " & e.KeyValue)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyData: " & e.KeyData.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Alt: " & e.Alt)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Control: " & e.Control)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Shift: " & e.Shift)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Modifiers: " & e.Modifiers.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("===================== End KeyDown")
    
        End Sub
    Now run the demo press ALT-F2 and is ok after press ALT-F1 and ALT is lost in keydata.
    I hope to be clear ...
    You can also contact me on msn Lions_italyAThotmail.com and skype lionsitaly, many thanks for the library and support !
    Ciao from italy

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

    Re: Global hooks

    Firstly, I'd like to thank VBDT for an excellent piece of code.

    But, I've discovered a slightly embarassing typing error:
    In the ClickCheckBox object, the text says "Press sh***it + C to click me" (disregard the ***, I've written it this way to avoid the automatic replacement) instead of "Press Shift + C to click me".

    Also, GroupBox1's text is missing an 'e' in 'mouse'.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  30. #30

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

    Re: Global hooks

    Quote Originally Posted by obi1kenobi
    Firstly, I'd like to thank VBDT for an excellent piece of code.

    But, I've discovered a slightly embarassing typing error:
    In the ClickCheckBox object, the text says "Press sh***it + C to click me" (disregard the ***, I've written it this way to avoid the automatic replacement) instead of "Press Shift + C to click me".

    Also, GroupBox1's text is missing an 'e' in 'mouse'.
    Corrected; thanks
    VBDT

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

    Re: Global hooks

    Something weird is happening in my application:

    When the mouse is hooked, upon right-clicking on a control, its context menu shows up normally, however the MouseDown event of that control doesn't fire.

    When the mouse is not hooked, the context menu shows normally, AND the MouseDown event is fired.

    IMHO, it must be connected to the MouseHook in some way. What do I do to fix this?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  32. #32

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

    Re: Global hooks

    Quote Originally Posted by obi1kenobi
    Something weird is happening in my application:

    When the mouse is hooked, upon right-clicking on a control, its context menu shows up normally, however the MouseDown event of that control doesn't fire.

    When the mouse is not hooked, the context menu shows normally, AND the MouseDown event is fired.

    IMHO, it must be connected to the MouseHook in some way. What do I do to fix this?
    Hi obi1kenobi, I tested the hook and I couldn’t find any problems with it. In general when you right click on a form that has a ContextMenuStrip the right click is not fired; this is a normal windows behavior.

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

    Re: Global hooks

    Quote Originally Posted by VBDT
    Hi obi1kenobi, I tested the hook and I couldn’t find any problems with it. In general when you right click on a form that has a ContextMenuStrip the right click is not fired; this is a normal windows behavior.
    Strange, I rebuilt the project and now both the ContextMenuStrip is shown and the right click is fired. I don't get it... But, since we're talking about a Microsoft product, everything is possible...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  34. #34
    New Member
    Join Date
    Apr 2008
    Posts
    5

    Re: Global hooks

    Hey there,

    I'm trying to use your dll file in a Windows Mobile 5.0 VB application, but when I try to load the application, this is the error message it gives me. "File or assembly name 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089', or one of its dependencies, was not found."

    could you shed some light on this, perhaps?
    Thanks

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

    Re: Global hooks

    Try adding a reference to System.Windows.Forms in your project. (I'm not sure whether the assembly exists in the CF version...) Right-click the project, then select Properties, then the References tab and then press Add and select the System.Windows.Forms assembly.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  36. #36

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

    Re: Global hooks

    Quote Originally Posted by Lionsitaly
    ok many thanks for support !
    I try !
    Open change the event keydown in this way

    Code:
       Private Sub gkh_KeyDown(ByVal sender As Object, ByVal e As WindowsHookLib.KeyEventArgs) Handles gkh.KeyDown
    
            'Set the key down Handled property
            e.Handled = Me.HandleKeyboardCheckBox.Checked
            If e.KeyCode = Keys.F2 And e.Alt Then
                Me.DisplayTextBox.AppendText("===================== Sleep 1000")
                System.Threading.Thread.Sleep(1000)
            End If        'Print the key down data
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("===================== KeyDown")
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Handled: " & e.Handled)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyCode: " & e.KeyCode.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyValue: " & e.KeyValue)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("KeyData: " & e.KeyData.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Alt: " & e.Alt)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Control: " & e.Control)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Shift: " & e.Shift)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("Modifiers: " & e.Modifiers.ToString)
            Me.DisplayTextBox.AppendText(Environment.NewLine)
            Me.DisplayTextBox.AppendText("===================== End KeyDown")
    
        End Sub
    Now run the demo press ALT-F2 and is ok after press ALT-F1 and ALT is lost in keydata.
    I hope to be clear ...
    You can also contact me on msn Lions_italyAThotmail.com and skype lionsitaly, many thanks for the library and support !
    Ciao from italy
    Hi, do not use Dialog boxes or Thread sleep in the hook event subs.

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

    Re: Global hooks

    how would i go about recompiling your DLL so that the e.keycode.tostring of backspace is {backspace}? i also want to do this for all the other non alphanumeric keycodes.

  38. #38

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

    Re: Global hooks

    Quote Originally Posted by bagstoper
    how would i go about recompiling your DLL so that the e.keycode.tostring of backspace is {backspace}? i also want to do this for all the other non alphanumeric keycodes.
    Hi, that is a custom string and the hook doesn’t return value with that format. The only way is to handle the key event and format the string according to the key value return by the "e.KeyCode".

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

    Re: Global hooks

    Ok i was afraid that was what i would have to do. but thanks anyways for the good keyboard, mouse, and clipboard hook.

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

    Re: Global hooks

    Hi VBDT,

    WindowsHookLib doesn't support the "Windows Key" as a modifier. Is this viable? Do you intend to develop that?

    Thanks.

Page 1 of 3 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
  •  



Featured


Click Here to Expand Forum to Full Width