我正在为 Windows 10 通用应用程序(仅限移动和桌面设备系列)开发一个类库。如果用户空闲(没有触摸、鼠标移动、按键等)x 秒,我需要调用一个事件。这个方法可以用来在android上解决这个问题。但我在 Windows UWP 上找不到解决方案。
UWP 中是否有可用的 API 来实现这一点?
我正在为 Windows 10 通用应用程序(仅限移动和桌面设备系列)开发一个类库。如果用户空闲(没有触摸、鼠标移动、按键等)x 秒,我需要调用一个事件。这个方法可以用来在android上解决这个问题。但我在 Windows UWP 上找不到解决方案。
UWP 中是否有可用的 API 来实现这一点?
您可以使用应用程序的 CoreWindow 上的各种事件检测全局输入:
使用 CoreWindow.PointerPressed、PointerMoved 和 PointerReleased 进行触摸和鼠标输入。
键盘输入:KeyUp 和 KeyDown(软键)和 CharacterReceived(用于通过和弦和文本建议生成的字符)
如果没有任何这些事件的时间过长,使用这些来检测用户是否处于活动状态和空闲状态。
我知道这确实是个老问题,但我认为您现在可以使用RegisterBackgroundTask获得相同的结果
只需设置:
new TimeTrigger(15, false) //For time trigger
new SystemCondition(SystemConditionType.UserNotPresent)) //And so you want to know so user is not present
App.xaml.cs 中的示例用法:
var builder = new BackgroundTaskBuilder();
builder.Name = "Is user Idle";
builder.SetTrigger(new TimeTrigger(2, false)); //two mins
builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));
// Do not set builder.TaskEntryPoint for in-process background tasks
// Here we register the task and work will start based on the time trigger.
BackgroundTaskRegistration task = builder.Register();
task.Completed += (sender, args) =>
{
//Handle user not present (Idle) here.
};