Created
July 12, 2020 23:06
-
-
Save Y4rd13/7f2bf0ac7c649ab1bd7eb7710f57d84d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import Structure, windll, c_uint, sizeof, byref | |
from threading import Thread, Event | |
from pynput import keyboard | |
from time import sleep | |
keys = [] | |
message = ' ' | |
# LAST INPUT INFO | |
class LASTINPUTINFO(Structure): | |
_fields_ = [ | |
('cbSize', c_uint), | |
('dwTime', c_uint), | |
] | |
def get_idle_duration(): | |
while True: | |
lastInputInfo = LASTINPUTINFO() | |
lastInputInfo.cbSize = sizeof(lastInputInfo) | |
windll.user32.GetLastInputInfo(byref(lastInputInfo)) | |
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime | |
millis = millis / 1000.0 | |
print(f'\t{millis}') | |
sleep(1) | |
if int(millis) == 3: # every 3 sec user is AFK, trigger event.set() | |
print(f'\t{millis}') | |
event.set() | |
def foo(k): | |
event.wait() | |
print(f'Event set: {event.is_set()}') | |
print(f'{k}') | |
print('PROCESS FINISHED\n') | |
sleep(3) | |
event.clear() | |
def write_keys(keys): | |
global message | |
for key in keys: | |
k = str(key).replace("'", "") | |
print(k, len(k)) | |
message += k # concatenate keys | |
if (len(message)) >= 3: | |
foo(message) | |
def on_press(key): | |
global keys | |
keys.append(key) | |
write_keys(keys) | |
keys = [] | |
if __name__ == "__main__": | |
event = Event() | |
listener = keyboard.Listener(on_press=on_press) | |
timer = Thread(target=get_idle_duration) | |
listener.start() | |
timer.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment