GetLastInputInfo()
,例如detecting idle time using python,是敏感的鼠标和键盘输入,但它也干扰游戏控制器输入,我想忽略.
有没有简单的方法可以做到这一点?
一种方法是使用原始输入,例如Having trouble using winapi to read input from a device,它需要由同步的写和读线程包装,这些线程记录事件并可以查询.
GetLastInputInfo()
,例如detecting idle time using python,是敏感的鼠标和键盘输入,但它也干扰游戏控制器输入,我想忽略.
有没有简单的方法可以做到这一点?
一种方法是使用原始输入,例如Having trouble using winapi to read input from a device,它需要由同步的写和读线程包装,这些线程记录事件并可以查询.
您可以使用pynput模块通过键盘和鼠标侦听器获取用户输入.我创建了一个不考虑控制器输入的简短示例.(至少在我的PC上)
from time import time
from pynput.keyboard import Listener as KeyListener
from pynput.mouse import Listener as MouseListener
idle_time = time()
txt = "You've been idle for {} seconds."
def tell_time():
global idle_time
print(txt.format(time() - idle_time))
idle_time = time()
if __name__ == '__main__':
KeyListener(on_press=lambda key: tell_time()).start()
with MouseListener(
on_move=lambda x, y: tell_time(),
on_click=lambda x, y, btn, b: tell_time(),
on_scroll=lambda a, b, c, d: tell_time()
) as listener:
listener.join()