@
xausky #1
下面是原神的,将****替换成四空格就行。我是根据以前的代码改的,所以中间有些条件可能是多余的,因为我切换到后台暂停了一次,切换到后台 30 秒后又暂停了一次,我之前是 30 秒后要做的事情不一样。并且暂停之后只要没有再将原神切换到前台,那么就一直不再次暂停,防止反复操作。
import time, os
import win32process, win32gui
import psutil
import win32com.client
def get_yuanshen_pid():
****try:
********process_name = 'YuanShen.exe'
********WMI = win32com.client.GetObject('winmgmts:')
********processes = WMI.InstancesOf('Win32_Process')
********pid = next((process.ProcessId for process in processes if process.Name.lower() == process_name.lower()), None)
********if isinstance(pid, int):
************return pid
********else:
************return None
****except:
********return None
# 如果刚启动脚本后发现已经暂停就先恢复,有时候要是暂停了多次可能要恢复多次。
pid = get_yuanshen_pid()
if pid:
****for i in range(5):
********psutil.Process(pid).resume()
last_process_name = ''
last_yuanshen_time = 0
paused = False
while True:
****try:
********handle = win32gui.GetForegroundWindow()
********pid = win32process.GetWindowThreadProcessId(handle)[1]
********process_name = psutil.Process(pid).name()
********last = last_process_name
********last_process_name = process_name
********if process_name != last:
************if process_name == 'dwm.exe': #将已暂停程序切换到前台,windows 检测到的前台程序是 dwm.exe
****************pid = get_yuanshen_pid()
****************if pid:
********************psutil.Process(pid).resume()
********************paused = False
************elif process_name != 'YuanShen.exe' and last_yuanshen_time != 0:
****************pid = get_yuanshen_pid()
****************if pid and not paused:
********************psutil.Process(pid).suspend()
********************paused = True
********if process_name == 'YuanShen.exe':
************last_yuanshen_time = int(time.time())
********elif int(time.time()) - last_yuanshen_time > 30 and last_yuanshen_time != 0:
************pid = get_yuanshen_pid()
************if pid and not paused:
****************psutil.Process(pid).suspend()
****************paused = True
************last_yuanshen_time = 0
********time.sleep(1)
****except BaseException as e:
********print(e)
********time.sleep(1)