1. Este site usa cookies. Ao continuar a usar este site está a concordar com o nosso uso de cookies. Saber Mais.

[Duvida] VB 2008

Discussão em 'Programação' iniciada por Dr4k3, 16 de Outubro de 2008. (Respostas: 2; Visualizações: 926)

  1. boas

    Há alguma maneira de detectar a inactividade do utilizador?

    Por exemplo:

    Se o utilizador nao mexesse no rato nem no teclado durante x minutos entao ele iria voltar ao login form.
     
  2. iznougud

    iznougud I quit My Job for Folding

    Pelo menos no teclado podes saber, tens de instalar um Keyboard hook que te capture tudo o que o user faz com o teclado. Com o rato nunca fiz.

    Código:
    
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    Imports System.Drawing
    Imports System.Threading
    
    Public Class KeyboardHook
        Public Declare Function UnhookWindowsHookEx Lib "user32" _
          (ByVal hHook As Integer) As Integer
    
        Public Declare Function SetWindowsHookEx Lib "user32" _
          Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
          ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
          ByVal dwThreadId As Integer) As Integer
    
        Private Declare Function GetAsyncKeyState Lib "user32" _
          (ByVal vKey As Integer) As Integer
    
        Private Declare Function CallNextHookEx Lib "user32" _
          (ByVal hHook As Integer, _
          ByVal nCode As Integer, _
          ByVal wParam As Integer, _
          ByVal lParam As KBDLLHOOKSTRUCT) As Integer
    
        Public Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scanCode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure
    
        ' Low-Level Keyboard Constants
        Private Const HC_ACTION As Integer = 0
        Private Const LLKHF_EXTENDED As Integer = &H1
        Private Const LLKHF_INJECTED As Integer = &H10
        Private Const LLKHF_ALTDOWN As Integer = &H20
        Private Const LLKHF_UP As Integer = &H80
    
        ' Virtual Keys
        Public Const VK_TAB As Integer = &H9
        Public Const VK_CONTROL As Integer = &H11
        Public Const VK_ESCAPE As Integer = &H1B
        Public Const VK_DELETE As Integer = &H2E
    
        Private Const WH_KEYBOARD_LL As Integer = 13&
        Public KeyboardHandle As Integer
    
        Private main As MainForm
    
        Public Sub New(ByVal m As MainForm)
            main = m
        End Sub
        ' Implement this function to block as many
        ' key combinations as you'd like
        Public Function IsHooked( _
          ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    
            
        End Function
    
        Private Sub HookedState(ByVal Text As String)
            Debug.WriteLine(Text)
        End Sub
    
      
        Public Function KeyboardCallback(ByVal Code As Integer, _
          ByVal wParam As Integer, _
          ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    
            Static flag As Boolean = False
    
            If (Code = HC_ACTION) Then
    
                  debug.WriteLine("Codigo: " & lParam.vkCode)
    
            End If
    fim:
            Try
    
                Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
            Catch ex As Exception
                Debug.WriteLine("Erro no keyboardhook: " & ex.Message)
            End Try
    
    
        End Function
    
    
        Public Delegate Function KeyboardHookDelegate( _
          ByVal Code As Integer, _
          ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
                       As Integer
    
        <MarshalAs(UnmanagedType.FunctionPtr)> _
        Private callback As KeyboardHookDelegate
    
        Public Sub HookKeyboard()
            callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    
            KeyboardHandle = SetWindowsHookEx( _
              WH_KEYBOARD_LL, callback, _
              Marshal.GetHINSTANCE( _
              [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    
            Call CheckHooked()
        End Sub
    
        Public Sub CheckHooked()
            If (Hooked()) Then
                Debug.WriteLine("Keyboard hooked")
            Else
                Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
            End If
        End Sub
    
        Private Function Hooked() as integer
            Hooked = KeyboardHandle <> 0
        End Function
    
        Public Sub UnhookKeyboard()
            If (Hooked()) Then
                Call UnhookWindowsHookEx(KeyboardHandle)
            End If
        End Sub
    
    End Class
    
    
    
    Esta class permite-te "escutar" o teclado. Recebe como parametro o form principal para poder usar as funcoes publicas dele, como por exemplo, actualizar a data e hora da ultima tecla premida pelo user.

    No form principal so precisas de fazer:

    Código:
            Dim kb As New KeyboardHook (me)
    	kb.HookKeyboard()
    
    Quanto ao rato vou investigar...

    ve aqui: http://www.colinneller.com/blog/PermaLink,guid,2838f59a-f4af-4c95-a322-b9ee5918a39c.aspx
     
    Última edição: 16 de Outubro de 2008
  3. pois mas preciso dos dois porque um sem o outro nao faz sentido porque imagina que o user anda la a mexer so com o rato e de repente volta ao login form ^_^ e capaz de ser um bocado chato :D mas obgd na mesma,eu ate ja tinha visto isso dos hooks mas o codigo e um bocado complicado :D
     

Partilhar esta Página