跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

提交记录
修改Core/Servicers/Instances/Sleepdiscover.cs+60 −7看文件
@@ -7,6 +7,7 @@ using System;
77using System.Collections.Generic;
88using System.Diagnostics;
99using System.Linq;
10新增:using System.Runtime.InteropServices;
1011using System.Text;
1112using System.Threading.Tasks;
1213using System.Windows;
@@ -24,16 +25,48 @@ namespace Core.Servicers.Instances
2425
2526 private SleepStatus status = SleepStatus.Wake;
2627
27删除: // 播放声音开始时间
28新增: /// <summary>
29新增: /// 播放声音开始时间
30新增: /// </summary>
2831 private DateTime playSoundStartTime;
2932
33新增: /// <summary>
34新增: /// 最后一次按键时间
35新增: /// </summary>
36新增: private DateTime pressKeyboardLastTime;
37新增:
3038 private readonly IObserver observer;
3139
40新增: private Win32API.LowLevelKeyboardProc keyboardProc;
41新增: private static IntPtr hookKeyboardID = IntPtr.Zero;
42新增:
3243 public Sleepdiscover(IObserver observer)
3344 {
3445 this.observer = observer;
3546 observer.OnAppActive += Observer_OnAppActive;
3647 SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
48新增:
49新增: keyboardProc = HookCallback;
50新增: }
51新增:
52新增: private IntPtr HookCallback(
53新增: int nCode, IntPtr wParam, IntPtr lParam)
54新增: {
55新增: if (nCode >= 0 && wParam == (IntPtr)Win32API.WM_KEYDOWN)
56新增: {
57新增: int vkCode = Marshal.ReadInt32(lParam);
58新增:
59新增: if (status == SleepStatus.Sleep)
60新增: {
61新增: status = SleepStatus.Wake;
62新增: SleepStatusChanged?.Invoke(status);
63新增: }
64新增: else
65新增: {
66新增: pressKeyboardLastTime = DateTime.Now;
67新增: }
68新增: }
69新增: return Win32API.CallNextHookEx(hookKeyboardID, nCode, wParam, lParam);
3770 }
3871
3972 private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
@@ -72,19 +105,26 @@ namespace Core.Servicers.Instances
72105 {
73106 timer = new DispatcherTimer();
74107 timer.Interval = new TimeSpan(0, 2, 0);
108新增:#if DEBUG
109新增: timer.Interval = new TimeSpan(0, 0, 10);
110新增:#endif
75111 timer.Tick += Timer_Tick;
76112 timer.Start();
77113
78114 Win32API.GetCursorPos(out lastPoint);
79115
80116 playSoundStartTime = DateTime.MinValue;
117新增:
118新增: pressKeyboardLastTime = DateTime.Now;
119新增:
120新增: // 设置键盘钩子
121新增: Win32API.SetKeyboardHook(keyboardProc);
81122 }
82123
83124 private void Timer_Tick(object sender, EventArgs e)
84125 {
85126 Point point;
86127 Win32API.GetCursorPos(out point);
87删除:
88128 if (point == null)
89129 {
90130 return;
@@ -118,7 +158,7 @@ namespace Core.Servicers.Instances
118158 // 判断声音时间是否超过2个小时
119159 TimeSpan timeSpan = DateTime.Now - playSoundStartTime;
120160
121删除: if (timeSpan.TotalHours >= 2)
161新增: if (timeSpan.TotalHours >= 2 && IsPressKeyboardOuttime())
122162 {
123163 // 超过表示可能睡着了,切换状态
124164 status = SleepStatus.Sleep;
@@ -131,9 +171,12 @@ namespace Core.Servicers.Instances
131171 }
132172 else
133173 {
134删除: // 没有播放声音切换状态
135删除: status = SleepStatus.Sleep;
136删除: SleepStatusChanged?.Invoke(status);
174新增: if (IsPressKeyboardOuttime())
175新增: {
176新增: // 没有播放声音且键盘休眠超时
177新增: status = SleepStatus.Sleep;
178新增: SleepStatusChanged?.Invoke(status);
179新增: }
137180 }
138181 }
139182 }
@@ -147,8 +190,18 @@ namespace Core.Servicers.Instances
147190 SleepStatusChanged?.Invoke(status);
148191 }
149192 }
150删除:
151193 Win32API.GetCursorPos(out lastPoint);
152194 }
195新增:
196新增:
197新增: /// <summary>
198新增: /// 通过按键时间判断(超过5分钟未有键盘操作视为超时)
199新增: /// </summary>
200新增: /// <returns></returns>
201新增: private bool IsPressKeyboardOuttime()
202新增: {
203新增: TimeSpan timeSpan = DateTime.Now - pressKeyboardLastTime;
204新增: return timeSpan.TotalMinutes >= 5;
205新增: }
153206 }
154207}