Windowsの勝手にロックされるのを防ぐ

```
import pyautogui
import time
import random
import string
import subprocess
import pygetwindow as gw

def open_text_editor():
    # メモ帳を開く
    return subprocess.Popen(['notepad.exe'])

def focus_on_window(window_title):
    # 特定のウィンドウにフォーカスを合わせる
    try:
        window = gw.getWindowsWithTitle(window_title)[0]
        if window is not None:
            window.activate()
            return True
    except Exception as e:
        print(f"Error focusing on window: {e}")
        return False

def clear_text():
    # すべてのテキストを選択し、削除する
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.press('backspace')

def simulate_keypress():
    clear_text()
    pyautogui.write('I am Lord Voldemort', interval=0.25)
    pyautogui.press('enter')

    # 各文字を入力するたびにフォーカスを合わせる
    for _ in range(random.randint(1, 10)):
        time.sleep(random.randint(1, 5))
        if focus_on_window('無題 - メモ帳'):
            pyautogui.write(random.choice(string.ascii_letters), interval=0.25)
        else:
            print("Unable to find or focus on the Notepad window.")

# テキストエディタを開く
notepad_process = open_text_editor()

# エディタが開くのを待つ
time.sleep(5)

while True:
    # メモ帳ウィンドウにフォーカスを合わせる
    if focus_on_window('無題 - メモ帳'):
        simulate_keypress()
    else:
        print("Unable to find or focus on the Notepad window.")
    
    # 10秒待機
    time.sleep(10)
```

```
import pyautogui
import time
import random
import subprocess
import pygetwindow as gw

# ハリーポッターの名言
quotes = [
    "It is our choices, Harry, that show what we truly are, far more than our abilities.",
    "We've all got both light and dark inside us. What matters is the part we choose to act on.",
    "Happiness can be found even in the darkest of times, if one only remembers to turn on the light.",
    "You're just as sane as I am.",
    "Words are, in my not-so-humble opinion, our most inexhaustible source of magic.",
    "It does not do to dwell on dreams and forget to live.",
    "After all this time? Always.",
    "Fear of a name increases fear of the thing itself.",
    "To the well-organized mind, death is but the next great adventure.",
    "The ones that love us never really leave us.",
    "The world isn't split into good people and Death Eaters.",
    "Numbing the pain for a while will make it worse when you finally feel it.",
    "Every human life is worth the same, and worth saving.",
    "It matters not what someone is born, but what they grow to be.",
    "Do not pity the dead, Harry. Pity the living.",
    "We are only as strong as we are united, as weak as we are divided.",
    "Differences of habit and language are nothing at all if our aims are identical and our hearts are open.",
    "Of course it is happening inside your head, Harry, but why on earth should that mean that it is not real?",
    "We must all face the choice between what is right and what is easy.",
    "Understanding is the first step to acceptance, and only with acceptance can there be recovery.",
    "It's the unknown we fear when we look upon death and darkness, nothing more.",
    "It takes a great deal of bravery to stand up to our enemies, but just as much to stand up to our friends.",
    "You sort of start thinking anything’s possible if you’ve got enough nerve.",
    "Time will not slow down when something unpleasant lies ahead.",
    "Until the very end.",
    "The truth. It is a beautiful and terrible thing, and should therefore be treated with great caution.",
    "I am what I am, an’ I’m not ashamed.",
    "Curiosity is not a sin.... But we should exercise caution with our curiosity.",
    "What’s comin’ will come, an’ we’ll meet it when it does.",
    "Things we lose have a way of coming back to us in the end, if not always in the way we expect."
]


def open_text_editor():
    # メモ帳を開く
    return subprocess.Popen(['notepad.exe'])

def focus_on_window(window_title):
    # 特定のウィンドウにフォーカスを合わせる
    try:
        window = gw.getWindowsWithTitle(window_title)[0]
        if window is not None:
            window.activate()
            return True
    except Exception as e:
        print(f"Error focusing on window: {e}")
        return False

def clear_text():
    # すべてのテキストを選択し、削除する
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.press('backspace')

def simulate_keypress():
    clear_text()
    # ランダムな名言を選択
    quote = random.choice(quotes)
    pyautogui.write(quote, interval=0.25)
    pyautogui.press('enter')

# テキストエディタを開く
notepad_process = open_text_editor()

# エディタが開くのを待つ
time.sleep(5)

while True:
    # メモ帳ウィンドウにフォーカスを合わせる
    if focus_on_window('無題 - メモ帳'):
        simulate_keypress()
    else:
        print("Unable to find or focus on the Notepad window.")
    
    # 10秒待機
    time.sleep(10)
```