Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1468to1472
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

UserForm schließen, wenn keine Tastaturaktivität

UserForm schließen, wenn keine Tastaturaktivität
18.01.2016 16:39:51
Oliver
Hallo!
Ich komme leider nicht alleine weiter und suche Rat. Mein Problem:
Eine UserForm soll nach Ablauf von z.B. 120 sec. geschlossen werden, wenn keine Tastatur- oder Mausaktivität vorliegt, sprich niemand im Programm arbeitet ("Auto-Log off"). Hintergrund: Es soll dann eine erneute Anmeldung mit Password erforderlich sein.
Folgender Code funktioniert für die Feststellung von Mausaktivität gut:
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare

Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Function MausPositionVeraendert() As Boolean
lRetVal = GetCursorPos(pTargetPoint)
If pTargetPoint.x = x_alt And pTargetPoint.y = y_alt Then
MausPositionVeraendert = False
Else
MausPositionVeraendert = True
End If
x_alt = pTargetPoint.x
y_alt = pTargetPoint.y
End Function
Sub TimerRoutine()
If MausPositionVeraendert = True Then
timer_count = timer_multiplier
Else
timer_count = timer_count - 1
End If
If timer_count = 0 Then
LogIn.Show  ' Anmeldebildschirm
Else
Call TimerSetzen(timer_count)
End If
End Sub
Sub TimerSetzen(c As Integer)
TimerGestartet = Now
Application.OnTime earliestTime:=TimerGestartet + TimeValue(timerintervall$), Procedure:="  _
_
TimerRoutine"
timer_count = c
End Sub
Sub TimerAktualisieren()
Call TimerLoeschen
Call TimerSetzen(timer_multiplier)
End Sub
Leider weiß ich jetzt nicht weiter, wie ich die Tastatur analog zur Mausposition abfragen soll, um Aktivität festzustellen.
Ich habe mit GetInputState experimentiert, aber keine Lösung gefunden.
Hat jemand eine zündende Idee?
Vielen Dank für jede Antwort
Oliver

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
UserForm schließen, wenn keine Tastaturaktivität
18.01.2016 18:45:32
Nepumuk
Hallo,
du kannst die Maus und Tastatur gleichzeitig überwachen.
Im Modul des Userforms:
Option Explicit

Private Sub UserForm_Activate()
    Call StartTimer
End Sub

In einem Standardmodul:
Option Explicit
Option Private Module

Private Declare PtrSafe Sub GetLastInputInfo Lib "user32.dll" ( _
    ByRef plii As LASTINPUTINFO)
Private Declare PtrSafe Function GetTickCount Lib "kernel32.dll" () As Long
Private Declare PtrSafe Function SetTimer Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal nIDEvent As LongPtr, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByVal nIDEvent As LongPtr) As Long

Private Type LASTINPUTINFO
    cbSize As Long
    dwTime As Long
End Type

Private Const TIME_TO_CLOSE As Single = 120 ' Zeit nach das Userform geschlossen werden soll in Sekunden

Public Sub StartTimer()
    Call SetTimer(Application.hwnd, 0, 1000&, AddressOf TimerRun)
End Sub

Private Sub StopTimer()
    Call KillTimer(Application.hwnd, 0)
End Sub

Sub TimerRun(ByVal hwnd As LongPtr, ByVal uMsg As LongPtr, _
        ByVal nIDEvent As Long, ByVal dwTimer As LongPtr)

    If IdleTime >= TIME_TO_CLOSE Then
        Call StopTimer
        Call Unload(Object:=UserForm1)
    End If
End Sub

Private Function IdleTime() As Single
    Dim udtInputInfo As LASTINPUTINFO
    udtInputInfo.cbSize = LenB(udtInputInfo)
    Call GetLastInputInfo(udtInputInfo)
    IdleTime = (GetTickCount - udtInputInfo.dwTime) / 1000
End Function

Gruß
Nepumuk

Anzeige
Nachtrag
18.01.2016 19:29:33
Nepumuk
Hallo,
ich hab gerade gesehen, du verwendest noch Excel 2003. Da müssen die Deklarationen der API's so lauten:
Private Declare Function GetLastInputInfo Lib "user32.dll" ( _
    ByRef plii As PLASTINPUTINFO) As Long
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Declare Function SetTimer Lib "user32.dll" ( _
    ByVal hWnd As Long, _
    ByVal nIDEvent As Long, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32.dll" ( _
    ByVal hWnd As Long, _
    ByVal nIDEvent As Long) As Long

und die Zeile für den Timer-Aufruf so:
Sub TimerRun(ByVal hwnd As Long, ByVal uMsg As Long, _
    ByVal nIDEvent As Long, ByVal dwTimer As Long)


Gruß
Nepumuk

Anzeige
AW: UserForm schließen, wenn keine Tastaturaktivität
19.01.2016 10:43:34
Oliver
Hallo ransi, Hallo Nepumuk,
Vielen Dank für Eure beiden superschnellen Antworten, ihr habt mir beide sehr geholfen!
Der Code von Nepumuk funktioniert genauso wie von mir erhofft und der Link von ransi hat mir auch einen wichtigen Abfrageweg aufgezeigt.
Toll, das es dieses Forum gibt!
Beste Grüße
Oliver

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige