我想在沒有人使用我的應用程式時執行后臺任務(更新、備份、計算……)。
因此,我想確定自上次擊鍵和/或滑鼠在我的應用程式中移動以來的時間。如果超過特定時間沒有用戶活動,則不打擾用戶的機會很高。多執行緒不是我的選擇。
我想避免觸摸應用程式中每個組件的每個 onm ouseDown-/OnKeyPress-Event,因為這沒有任何意義。
我怎樣才能得到
a) 自上次在 Windows 中輸入以來的時間
b) 自上次在我的應用程式中輸入以來的時間
uj5u.com熱心網友回復:
此解決方案適用于問題
a) 自上次在 Windows 中輸入以來的時間
每次滑鼠移動或鍵盤輸入都會將時間重置為零。
function GetTimeSinceLastUserInputInWindows(): TTimeSpan;
var
lastInput: TLastInputInfo;
currentTickCount: DWORD;
millisecondsPassed: Double;
begin
lastInput := Default(TLastInputInfo);
lastInput.cbSize := SizeOf(TLastInputInfo);
Win32Check( GetLastInputInfo(lastInput) );
currentTickCount := GetTickCount();
if (lastInput.dwTime > currentTickCount) then begin // lastInput was before 49.7 days but by now, 49.7 days have passed
millisecondsPassed :=
(DWORD.MaxValue - lastInput.dwTime)
(currentTickCount * 1.0); // cast to float by multiplying to avoid DWORD overflow
Result := TTimeSpan.FromMilliseconds(millisecondsPassed);
end else begin
Result := TTimeSpan.FromMilliseconds(currentTickCount - lastInput.dwTime );
end;
end;
https://www.delphipraxis.net/1504414-post3.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/527489.html
