屏保的没试过,锁屏的我倒处理过:
class WinSessionManager:
def __init__(self):
self.last_unlock = 0
self.is_active = None
self.is_locked = None
def update_lock_status(self):
hwnd = win32gui.GetForegroundWindow()
if hwnd == 0:
self.is_locked = True
else:
self.is_locked = False
def get_current_rdp_session(self):
'''
get current rdp session id, *also force an update of lock status*
:return: current rdp session id
'''
self.update_lock_status()
complete = subprocess.run('chcp 437 | query session', capture_output=True, shell=True, encoding='gbk')
cmd_text: str = complete.stdout
logging.debug('get_current_rdp_session():')
logging.debug(cmd_text)
for line in cmd_text.splitlines():
line = line.strip()
if line.startswith('>'):
line_splits = line.split()
rdp_session_id = int(line_splits[2])
if line_splits[3].lower() == 'disc':
self.is_active = False
elif line_splits[3].lower() == 'active':
self.is_active = True
return rdp_session_id
def lock_screen(self):
rdp_session = self.get_current_rdp_session()
if self.is_active:
logging.debug('Calling tsdiscon..')
return_code = subprocess.call(['tsdiscon.exe', str(rdp_session)], shell=True)
logging.debug(f'tsdiscon: return code: {return_code}')
while True:
self.get_current_rdp_session()
if not self.is_active:
break
def unlock_screen(self):
rdp_session = self.get_current_rdp_session()
self.last_unlock = time.time()
if self.is_active and self.is_locked:
# locked from console, disconnect the session first
logging.debug('Locked from console detected, disconnect first.')
self.lock_screen()
if not self.is_active:
logging.debug('Calling tscon..')
return_code = subprocess.call(['tscon.exe', str(rdp_session), '/dest:console'],
shell=True)
logging.debug(f'tscon: return code: {return_code}')
while True:
self.get_current_rdp_session()
if self.is_active:
break
【 在 Algoquant 的大作中提到: 】
: 发现很多python库 或者 nircmd.EXE (nircmd.exe sendkeypress ctrl)都是 模拟键盘或者鼠标操作,能够在当前会话 控制不用进入屏保或者 当前会话发起的屏保能够自动解除,
: 但是当系统发起的屏保时,这些程序就没法工作了,AI 说是windows系统发起的具有UI隔离,输入通道受限 ,所以没办法 用程序定时 取消屏保。
: 有没人知道相关的解决方案,除了 直接关闭系统 屏保功能。
: ...................
--
修改:adamhj FROM 182.96.96.*
FROM 182.96.96.*