Capturing Mouse Movements in Application

redbrad0

Member
Joined
May 31, 2007
Messages
14
Programming Experience
Beginner
I have a application that I would like to capture mouse movements in. The only thing in the application is a form and AxWebBrowser1 control. Basically what I am wanting is if no one is active on the form (typing or mousemovement) for 5 mins then the form can switch to rotating thru images (like a screen saver). Then when someone comes back and moves the mouse or hits a key the images (screen saver) will disappear and the main form will display.

Can anyone help guide me to capturing mouse movements in vb.net?
 
Use the .Net WebBrowser control with .Net 2.0, not the ActiveX control. You have to attach events for the hosted webpage document, see this thread. (in case your forum profile is wrong and should read .Net 1.1 see How To Handle Document Events in a Visual Basic .NET Application, or upgrade)

Each new navigation is a new document that you have to attach events to, use the DocumentCompleted and Navigating events to control attaching and detaching of document events.

When the document events you want to catch for mouse and keyboard input is fired you record the current Date.Now. Using a Timer you can calculate and check the TimeSpan from now back to the last recorded document event.
 
I tried to use the WebBrowser control but I was having all sort of problems with Javascript and popup windows. When I switched to the ActiveX control (AxWebBrowser) then I was able to get everything correct.

Are you saying there is no way to get a mouse movement using the AxWebBrowser control? I am writing a kiosk application so this will be in full screen mode and nothing else will be running on the computer except for this application.
 
No, but it is more difficult and awkward to handle in all, the .Net 1.1 article I linked you to describe this.
Edit note: I tried it and it didn't work for me copy-paste, it did when removing the handler code in "Form1" and changing this one code line in IEEvents class (plus fixing the implements):
VB.NET:
'in DocumentComplete changed
'Dim guid As Guid = System.Type.GetType("MSHTML.HTMLDocumentEvents2").GUID
'to
Dim guid As Guid = GetType(mshtml.HTMLDocumentEvents2).GUID

I don't know what problems you have with Javascript and Popups with the .Net 2.0 WebBrowser control, but there is a ScriptErrorsSuppressed property and a NewWindow event you can cancel.
 
Last edited:
Capturing Mouse & Keyboard Movements

I have a application that I will be using in Full Screen mode and need to capture anytime the mouse is moved or a key is pushed so i can turn off this applications "screen saver" so the user can start to use the system. I do not need to know where the mouse is located or what key is pressed.

Searching the internet I found the following class that I guess handles this but I am not sure how to use this in my application and return to my application that the mouse was moved or a key was pressed.

I added a class to my Project called "MouseHook" and below is the entire text inside that class.

VB.NET:
Imports System.Runtime.InteropServices

Public Class MouseHook
    ' DECLARATION

    Public Event KeyPressed(ByVal KeyChar As Int32)
    Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    Declare Auto Function SetWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
    Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hhk As IntPtr) As Boolean
    Declare Function CallNextHookEx Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Const WH_KEYBOARD_LL As Integer = 13
    Shared KeyBoardHook As IntPtr



    Public Function MyLLKbdProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        RaiseEvent KeyPressed(Marshal.ReadInt32(lParam))
        Return CallNextHookEx(KeyBoardHook, nCode, wParam, lParam)
    End Function

    Public Sub KeyBoardListen()
        Dim hp1 As HookProc = AddressOf MyLLKbdProc
        KeyBoardHook = SetWindowsHookEx(WH_KEYBOARD_LL, hp1, Marshal.GetHINSTANCE(GetType(MouseHook).Module), 0)
        GC.KeepAlive(hp1)
    End Sub

    Public Sub KeyBoardStopListen()
        UnhookWindowsHookEx(KeyBoardHook)
    End Sub

End Class
 
You don't need a system hook, just catch the Key+Mouse events for the Picturebox you're using as application "screensaver".
 
I am coming back to report what I have found. I have found a very easy way to monitor mouse movements no matter where the mouse is being moved. If you add Imports System.Runtime.InteropServices to your project then you can just reference the location of the mouse by calling..

'# Lets set the mouse positions
mousePositionX = Cursor.Position.X
mousePositionY = Cursor.Position.Y

Now time just for the keypress. As for John's statement, everything is using the AxWebBrowser control so that way even the ads are dynamic
 
Cursor class is from System.Windows.Forms namespace, you already have that imported, but anyway it isn't really relevant for what you are doing and ask for.
 
Back
Top