Home All Groups Group Topic Archive Search About

Detect mouse movement from minimized application

Author
20 Sep 2006 8:57 AM
squeak
Hi there,

I'm new to VB2005 so its probably a very simple answer too! But i just
can't think how to do it...

Basically i need to detect mouse (and preferably keyboard) movement and
kepresses when my application is just minimized and not the active
forground app.

I only need to triger an event which is going to tell my app if the
user has moved the mouse recently and decide if the workstation is now
idle, nothing more - so any ideas?

cheers in advance!

- - rob - -

Author
20 Sep 2006 9:26 AM
fcetrini
squeak ha scritto:

Show quoteHide quote
> Hi there,
>
> I'm new to VB2005 so its probably a very simple answer too! But i just
> can't think how to do it...
>
> Basically i need to detect mouse (and preferably keyboard) movement and
> kepresses when my application is just minimized and not the active
> forground app.
>
> I only need to triger an event which is going to tell my app if the
> user has moved the mouse recently and decide if the workstation is now
> idle, nothing more - so any ideas?
>
> cheers in advance!
>
> - - rob - -

hi rob, you got to attach a hook routine to the keyboard or mouse
messages:

create a public class with two public sub, startListen and stopListen,
and a public event, KeyPressed returning key's been pressed.

here's the keyboard procedure:

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

and this is the calling routine;

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

hope this help, fabio
Author
20 Sep 2006 9:57 AM
squeak
hi fabio!

thankyou very much for your rapid reply!

now i'm sure for most people this would be all they need to get things
going - but i'm afraid i'm not quite up to that level yet and am still
slightly stuck even with what you have given me!

sorry to be a pain but could you be a bit more detailed with the class
i need to create? from what you have said i assume i need something
like:

Public Class KeyboardListen

    Public Sub startListen()

    End Sub

    Public Sub stopListen()

    End Sub

    Public Event KeyPressed()

End Class

could you give me some more detail on where i put the other code
samples you gave me and how to use it?

once again sorry for being stupid, but i'm still very new to this!

thanks again!!
- - rob - -
Author
20 Sep 2006 10:25 AM
fcetrini
no problem, let's be more detailed :-D

Public Class Hook

   ' 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(Hook).Module), 0)
            GC.KeepAlive(hp1)
    End Sub

    Public Sub KeyBoardStopListen()
            UnhookWindowsHookEx(KeyBoardHook)
    End Sub

End Class

enjoy ;-)