【问题标题】:Python Block Keyboard / Mouse InputPython 块键盘/鼠标输入
【发布时间】:2021-01-20 01:42:34
【问题描述】:

我目前正在尝试编写一个简短的脚本,该脚本将在用户观看且无法干预时进行 rickroll(打开 youtube 链接)。 我已经设法逐个字母地慢慢打开插入链接,现在正试图阻止用户输入。 我曾尝试使用ctypes 导入来阻止所有输入,运行脚本然后再次解除阻止,但它不会阻止输入。我刚刚收到我的 RuntimeError 消息。 我该如何解决它,所以输入被阻止? 提前致谢! 代码如下:

import subprocess
import pyautogui
import time
import ctypes
from ctypes import wintypes

BlockInput = ctypes.windll.user32.BlockInput
BlockInput.argtypes = [wintypes.BOOL]
BlockInput.restype = wintypes.BOOL

blocked = BlockInput(True)

if blocked:
    try:
        subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",])
        time.sleep(3)
        pyautogui.write('www.youtube.com/watch?v=DLzxrzFCyOs', interval= 0.5)
        pyautogui.hotkey('enter')
    finally:
        unblocked = BlockInput(False)
else:
    raise RuntimeError('Input is already blocked by another thread')

【问题讨论】:

    标签: python input block


    【解决方案1】:

    您可以使用键盘模块阻止所有键盘输入,并使用鼠标模块不断移动鼠标,防止用户移动它。

    查看这些链接了解更多详情:

    https://github.com/boppreh/keyboard

    https://github.com/boppreh/mouse

    这会阻止键盘上的所有键(150 足够大以确保所有键都被阻止)。

    #### Blocking Keyboard ####
    import keyboard
    
    #blocks all keys of keyboard
    for i in range(150):
        keyboard.block_key(i)
    

    这通过不断地将鼠标移动到位置 (1,0) 来有效地阻止鼠标移动。

    #### Blocking Mouse-movement ####
    import threading
    import mouse
    import time
    
    global executing
    executing = True
    
    def move_mouse():
        #until executing is False, move mouse to (1,0)
        global executing
        while executing:
            mouse.move(1,0, absolute=True, duration=0)
    
    def stop_infinite_mouse_control():
        #stops infinite control of mouse after 10 seconds if program fails to execute
        global executing
        time.sleep(10)
        executing = False
    
    threading.Thread(target=move_mouse).start()
    
    threading.Thread(target=stop_infinite_mouse_control).start()
    #^failsafe^
    

    然后是您的原始代码(不再需要 if 语句和 try/catch 块)。

    #### opening the video ####
    import subprocess
    import pyautogui
    import time
    
    subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",])
    time.sleep(3)
    pyautogui.write('www.youtube.com/watch?v=DLzxrzFCyOs', interval = 0.5)
    pyautogui.hotkey('enter')
    
    
    #### stops moving mouse to (1,0) after video has been opened
    executing = False
    
    

    只是一些注意事项:

    1. 鼠标移动很难从程序外部停止(程序执行时基本上不可能关闭,尤其是键盘也被挡住了),这就是为什么我设置了故障保险,它停止移动10 秒后鼠标移动到 (1,0)。
    2. (在 Windows 上)Control-Alt-Delete 确实允许打开任务管理器,然后可以从那里强制停止程序。
    3. 这不会阻止用户点击鼠标,这有时会阻止完整输入 YouTube 链接(即可以打开一个新标签页)

    在此处查看完整版本的代码:

    https://pastebin.com/WUygDqbG

    【讨论】:

    • “这不会阻止用户点击鼠标” - 任何人对此有任何解决方案吗?
    【解决方案2】:

    你可以这样做来阻止键盘和鼠标输入

        from ctypes import windll
        from time import sleep
        
        windll.user32.BlockInput(True) #this will block the keyboard input
        sleep(15) #input will be blocked for 15 seconds
        windll.user32.BlockInput(False) #now the keyboard will be unblocked
    

    【讨论】:

    • 嗨@señor.woofers,上面的代码对我不起作用,这段代码真的有效吗?你找到解决问题的方法了吗?
    • 它仅在以管理员身份运行时有效。正如您所料,大多数/所有有关键盘或输入阻止的解决方案都需要管理员。
    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 2011-12-09
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2021-07-01
    • 2017-11-21
    相关资源
    最近更新 更多