forked from carrot69/keep-presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keep-presence.py
126 lines (86 loc) · 3.52 KB
/
keep-presence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import argparse
import time
from datetime import datetime
from pynput.mouse import Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController
mouse = MouseController()
keyboard = KeyboardController()
MOVE_MOUSE = False
PRESS_SHIFT_KEY = False
PIXELS_TO_MOVE = 1
move_mouse_every_seconds = 300
def define_custom_seconds():
global move_mouse_every_seconds, PIXELS_TO_MOVE, PRESS_SHIFT_KEY, MOVE_MOUSE
parser = argparse.ArgumentParser(
description="This program moves the mouse or press a key when it detects that you are away. "
"It won't do anything if you are using your computer. "
"Useful to trick your machine to think you are still working with it.")
parser.add_argument(
"-s", "--seconds", type=int,
help="Define in seconds how long to wait after a user is considered idle. Default 300.")
parser.add_argument(
"-p", "--pixels", type=int,
help="Set how many pixels the mouse should move. Default 1.")
parser.add_argument(
"-m", "--mode",
help="Available options: keyboard, mouse, both; default is mouse. "
"This is the action that will be executed when the user is idle: "
"If keyboard is selected, the program will press the shift key. "
"If mouse is selected, the program will move the mouse. "
"If both is selected, the program will do both actions. ")
args = parser.parse_args()
mode = args.mode
if args.seconds:
move_mouse_every_seconds = int(args.seconds)
if args.pixels:
PIXELS_TO_MOVE = int(args.pixels)
is_both_enabled = 'both' == mode
is_keyboard_enabled = 'keyboard' == mode or is_both_enabled
is_mouse_enabled = 'mouse' == mode or is_both_enabled or mode is None
print('--------')
if is_keyboard_enabled:
PRESS_SHIFT_KEY = True
print(get_now_timestamp(), "Keyboard is enabled")
if is_mouse_enabled:
MOVE_MOUSE = True
print(get_now_timestamp(), "Mouse is enabled, moving", PIXELS_TO_MOVE, 'pixels')
print(get_now_timestamp(), 'Running every', str(move_mouse_every_seconds), 'seconds')
print('--------')
def move_mouse_when_unable_to_move(expected_mouse_position):
if expected_mouse_position != mouse.position:
mouse.position = (0, 0)
def move_mouse():
new_x = currentPosition[0] + PIXELS_TO_MOVE
new_y = currentPosition[1] + PIXELS_TO_MOVE
new_position = (new_x, new_y)
mouse.position = new_position
move_mouse_when_unable_to_move(new_position)
current_position = mouse.position
print(get_now_timestamp(), 'Moved mouse to: ', current_position)
return current_position
def press_shift_key():
keyboard.press(Key.shift)
keyboard.release(Key.shift)
print(get_now_timestamp(), 'Shift key pressed')
def get_now_timestamp():
now = datetime.now()
return now.strftime("%H:%M:%S")
def execute_keep_awake_action():
print(get_now_timestamp(), 'Idle detection')
if MOVE_MOUSE:
move_mouse()
if PRESS_SHIFT_KEY:
press_shift_key()
define_custom_seconds()
lastSavePosition = (0, 0)
while 1:
currentPosition = mouse.position
is_user_away = currentPosition == lastSavePosition
if is_user_away:
execute_keep_awake_action()
currentPosition = mouse.position
if not is_user_away:
print(get_now_timestamp(), 'User activity detected')
lastSavePosition = currentPosition
print('--------')
time.sleep(move_mouse_every_seconds)