Volevo fare qualcosa di simile.
Ho provato il freeware Caffeine ma era bloccato dalle nostre politiche IT. Ho finito per scrivere uno script Python che fa una cosa simile (inviando la sequenza di tasti F15 ogni xx secondi).
(Può sicuramente essere tagliato al minimo di linee, ma ho solo 15 minuti da dedicargli, quindi la prima parte è un grande copia-incolla di altro codice).
Ecco qui:
#!/python
import ctypes
import random
import re
import time
from argparse import ArgumentParser, HelpFormatter
LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort
class MOUSEINPUT(ctypes.Structure):
_fields_ = (
('dx', LONG), ('dy', LONG), ('mouseData', DWORD),
('dwFlags', DWORD), ('time', DWORD),
('dwExtraInfo', ULONG_PTR)
)
class KEYBDINPUT(ctypes.Structure):
_fields_ = (
('wVk', WORD), ('wScan', WORD),
('dwFlags', DWORD), ('time', DWORD),
('dwExtraInfo', ULONG_PTR)
)
class _INPUTunion(ctypes.Union):
_fields_ = (
('mi', MOUSEINPUT),
('ki', KEYBDINPUT)
)
class INPUT(ctypes.Structure):
_fields_ = (('type', DWORD), ('union', _INPUTunion))
def SendInput(*inputs):
nInputs = len(inputs)
LPINPUT = INPUT * nInputs
pInputs = LPINPUT(*inputs)
cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
def Input(structure):
if isinstance(structure, MOUSEINPUT):
return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
elif isinstance(structure, KEYBDINPUT):
return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
else:
raise TypeError('Cannot create INPUT structure (keyboard)!')
keys = {
'DEFAULT': 0x7E, # F15 key
'SNAPSHOT': 0x2C, # PRINT SCREEN key
'F1': 0x70, # F1 key
'F2': 0x71, # F2 key
'F3': 0x72, # F3 key
'F4': 0x73, # F4 key
'F5': 0x74, # F5 key
'F6': 0x75, # F6 key
'F7': 0x76, # F7 key
'F8': 0x77, # F8 key
'F9': 0x78, # F9 key
'F10': 0x79, # F10 key
'F11': 0x7A, # F11 key
'F12': 0x7B, # F12 key
'F13': 0x7C, # F13 key
'F14': 0x7D, # F14 key
'F15': 0x7E, # F15 key
'F16': 0x7F, # F16 key
'F17': 0x80, # F17 key
'F18': 0x81, # F18 key
'F19': 0x82, # F19 key
'F20': 0x83, # F20 key
'F21': 0x84, # F21 key
'F22': 0x85, # F22 key
'F23': 0x86, # F23 key
'F24': 0x87, # F24 key
}
def Keyboard(code, flags=0):
# Code for key 0..9 or A..Z: it corresponds to the the ASCII code
if len(code) == 1 and re.match(r'[0-9A-Za-z]', code):
key = ord(code.upper())
# Keys 'F...': we use code in the dictionary
else:
key = keys.get(code.upper(), keys['DEFAULT'])
return Input(KEYBDINPUT(key, key, flags, 0, None))
############################################################################
sentences = [
"Don't sleep!",
"Stay awake!",
"Are you still here?",
"Hello...",
"Want some coffee?",
"What are you doing?"
]
def keep_alive(delay, nb_cycles=-1, key='F15'):
"""
Send keystroke F15 at a given delay for a given nb of cycles
Args:
delay(int): delay in seconds
nb_cycles(int): number of cycles (set to -1 for unlimited)
key(str): Key to send (default: 'F15')
"""
print("Trust me, I will keep you alive!\n")
while nb_cycles != 0:
time.sleep(delay)
SendInput(Keyboard(key))
print(random.choice(sentences))
nb_cycles -= 1
if __name__ == ' __main__':
# Information on the Program
copyright_year = 2018
prog = "stay_awake"
version_str = "%s v1.0" % prog
help_string = """\
Purpose: Send a keystroke (F15) to simulate user activity
"""
# Options
parser = ArgumentParser(
description=help_string, prog=prog,
formatter_class=lambda prog:
HelpFormatter(prog, max_help_position=60)
)
parser.add_argument(
"-k", "--key",
type=str, default='F15',
help="Key to send [Dflt: F15]"
)
parser.add_argument(
"-d", "--delay",
type=int, default=234,
help="Delay (in s) between keystrokes [Dflt: 234]"
)
parser.add_argument(
"-r", "--duration",
type=int, default=-1,
help="Duration (in s) or negative value for infinite"
)
options = parser.parse_args()
# Run
nb_cycles = options.duration if options.duration < 0 \
else int(options.duration/options.delay)
keep_alive(options.delay, nb_cycles, key=options.key)