python windows 不锁屏

创建日期: 2023-08-22 10:10 | 作者: 风波 | 浏览次数: 16 | 分类: Python

下面代码配合一个叫 “西西挂机锁”的锁屏软件使用,可以做到 1.设置自己的临时锁屏密码,2. 达到不触发系统锁屏。

# -*- coding: utf-8 -*-

import sys
import time
import signal
import win32gui, win32api, win32con, win32com
import pythoncom
import win32com.client
import keyboard
import threading
from threading import Thread


_dict = dict()
dict_lock = threading.Lock()
_dict["exit"] = False
def quit(signum, frame):
    with dict_lock:
        _dict["exit"] = True
    sys.exit()


def get_windows():
    hwnd_title=dict()
    def get_all_hwnd(hwnd,mouse): #设置EnumWindows的回调函数
        if win32gui.IsWindow(hwnd) and \
            win32gui.IsWindowEnabled(hwnd) and \
            win32gui.IsWindowVisible(hwnd) and \
            win32gui.GetWindowText(hwnd):
            #hwnd是窗口,启用且处于可视状态,就加入dict
            hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
    win32gui.EnumWindows(get_all_hwnd,0)
    return hwnd_title 


def tab_windows():
    win_dict = get_windows()
    win_list = [h for h in win_dict]

    google_hwnd = None
    tty_hwnd = None
    for h, n in win_dict.items():
        if n.startswith("Google"):
            google_hwnd = h
        if n.startswith("root@host:"):
            tty_hwnd = h

    if None == google_hwnd and None == tty_hwnd:
        win_list.sort()
        google_hwnd = win_list[0]
        tty_hwnd = win_list[-1]

    pythoncom.CoInitialize()
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%')

    fhwnd = win32gui.GetForegroundWindow()
    if google_hwnd == fhwnd:
        win32gui.SetForegroundWindow(tty_hwnd)
    elif tty_hwnd == fhwnd:
        win32gui.SetForegroundWindow(google_hwnd)
    else:
        win32gui.SetForegroundWindow(tty_hwnd)


_dict["pressed"] = time.time()
def monitor_keypress(args):
    exit_stat = False
    while exit_stat != True:
        keyboard.read_key()
        #print("somekey pressed")
        sys.stdout.flush()
        with dict_lock:
            _dict["pressed"] = time.time()
            exit_stat = _dict.get("exit", False)


def active(timeout):
    with dict_lock:
        prex = _dict.get("prex", 0)
        prey = _dict.get("prey", 0)
        press_time = _dict.get("pressed", 0)
        switch = _dict.get("switch", 0)

    x, y = win32api.GetCursorPos()
    if x != prex or y != prey:
        prex, prey = win32api.GetCursorPos()
        _dict["prex"] = prex
        _dict["prey"] = prey
        #print("mouse moved")
        return True

    #print("get keytime")
    keytime = time.time() - press_time
    if keytime < timeout:
        return True

    if time.time() - switch < timeout:
        return True

    return False


signal.signal(signal.SIGINT, quit)


def main():
    t = Thread(target=monitor_keypress, args=(dict(), ))
    t.start()
    #x, y = win32api.GetCursorPos()
    prex, prey = win32api.GetCursorPos()
    with dict_lock:
        _dict["prex"] = prex
        _dict["prey"] = prey
        _dict["pressed"] = time.time()
        _dict["switch"] = time.time()

    print("while")
    timeout = 60
    while True:
        if active(timeout):
            time.sleep(10)
            continue
        #print("change window")
        tab_windows()
        with dict_lock:
            _dict["switch"] = time.time()
        sys.stdout.flush()
        time.sleep(10)
        #print(x, y)
        #sys.stdout.flush()
        #win32api.SetCursorPos((x + 10, y))
        #win32api.SetCursorPos((x + 10, y + 10))
        #win32api.SetCursorPos((x, y + 10))
        #win32api.SetCursorPos((x, y))


if "__main__" == __name__:
    main()
16 浏览
11 爬虫
0 评论