Hallo,
ich versuche gerade die User Inactivity festzustellen und dachte das beste wäre das mit Hooks zu machen, jedoch kommt es dann immer wieder vor, dass die Maus furchtbar langsam wird. Was mache ich denn falsch? Hier ist der Code:
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Imports PVRScheduler.SchedulerData
Public Class clsHookMonitor
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 HookDelegate, ByVal hmod As IntPtr, _
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 IntPtr, ByVal lParam As IntPtr) As Integer
Public Delegate Function HookDelegate(ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As Integer
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WH_MOUSE_LL As Integer = 14
Private SchedulerIni As clsInit = clsInit.Instance
Public KeyboardHandle As Integer
Public MouseHandle As Integer
Private c_LastInput As Date
Public Function KeyboardCallback(ByVal nCode As Integer, _
ByVal wParam As IntPtr, _
ByRef lParam As IntPtr) As Integer
If nCode >= 0 Then
Reset()
End If
Return CallNextHookEx(KeyboardHandle, _
nCode, wParam, lParam)
End Function
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private KeyboardHandler As HookDelegate
Public Sub HookKeyboard()
If KeyboardHandle = 0 Then
KeyboardHandler = New HookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx( _
WH_KEYBOARD_LL, KeyboardHandler, _
Marshal.GetHINSTANCE( _
[Assembly].GetExecutingAssembly.GetModules()(0)), 0)
If KeyboardHandle = 0 Then
SchedulerIni.LogWrite("Keyboard hook failed: " & _
Err.LastDllError)
End If
End If
End Sub
Public Sub UnhookKeyboard()
If (KeyboardHandle <> 0) Then
If UnhookWindowsHookEx(KeyboardHandle) Then
SchedulerIni.LogWrite("Keyboard unhook failed: " & _
Err.LastDllError)
KeyboardHandle = 0
KeyboardHandler = Nothing
End If
End If
End Sub
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private MouseHandler As HookDelegate
Public Function MouseCallback(ByVal nCode As Integer, ByVal wParam As _
IntPtr, _
ByRef lParam As IntPtr) As Integer
If nCode >= 0 Then
Reset()
End If
Return CallNextHookEx(MouseHandle, _
nCode, wParam, lParam)
End Function
Public Sub HookMouse()
If MouseHandle = 0 Then
MouseHandler = New HookDelegate(AddressOf MouseCallback)
MouseHandle = SetWindowsHookEx( _
WH_MOUSE_LL, MouseHandler, _
Marshal.GetHINSTANCE( _
[Assembly].GetExecutingAssembly.GetModules()(0)), 0)
If MouseHandle = 0 Then
SchedulerIni.LogWrite("Mouse hook failed: " & Err.LastDllError)
End If
End If
End Sub
Public Sub UnhookMouse()
If (MouseHandle <> 0) Then
If UnhookWindowsHookEx(MouseHandle) Then
SchedulerIni.LogWrite("Mouse unhook failed: " & _
Err.LastDllError)
MouseHandle = 0
MouseHandler = Nothing
End If
End If
End Sub
Public Sub New()
End Sub
Public Sub Reset()
LastInput = Now
End Sub
Property LastInput() As Date
Get
Return c_LastInput
End Get
Set(ByVal Value As Date)
c_LastInput = Value
End Set
End Property
End Class Danke! |