首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python块键盘/鼠标输入

Python块键盘/鼠标输入
EN

Stack Overflow用户
提问于 2021-01-20 01:42:34
回答 3查看 4.1K关注 0票数 2

我目前正在尝试写一个简短的脚本,它将在用户观看时打开youtube链接,不能干预。我已经设法打开,慢慢插入链接一个字母,现在正试图阻止用户的输入。我尝试过使用ctypes导入来阻止所有输入,运行脚本,然后再取消阻塞,但它无论如何不会阻止输入。我刚收到我的RuntimeError信息。如何修复它,这样输入就会被阻塞?提前感谢!这是代码:

代码语言:javascript
复制
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')
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-01-21 13:25:28

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

有关更多细节,请参见这些链接:

https://github.com/boppreh/keyboard

https://github.com/boppreh/mouse

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

代码语言:javascript
复制
#### Blocking Keyboard ####
import keyboard

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

这有效地阻止鼠标移动,不断移动鼠标定位(1,0)。

代码语言:javascript
复制
#### 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块不再必要)。

代码语言:javascript
复制
#### 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. ( opened )控件--Alt-Delete允许打开任务管理器,然后可以强制执行。
  3. 这并不能阻止用户单击鼠标,这有时会阻止YouTube链接被完整键入(即可以打开一个新的选项卡)

请参阅代码的完整版本:

https://pastebin.com/WUygDqbG

票数 3
EN

Stack Overflow用户

发布于 2021-07-14 15:38:52

您可以这样做来阻止键盘和鼠标的输入。

代码语言:javascript
复制
    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
票数 1
EN

Stack Overflow用户

发布于 2022-11-08 21:40:47

这里有一个阻止键盘和鼠标输入的函数。您可以向blockMouseAndKeys函数传递一个数字以调整超时时间:

代码语言:javascript
复制
import os
import time
import pyautogui
from threading import Thread
from keyboard import block_key

def blockMouseAndKeys(timeout=5):
    global blocking
    blockStartTime = time.time()
    pyautogui.FAILSAFE = False
    blocking = True
    try: float(timeout)
    except: timeout = 5

    def blockKeys(timeout):
        global blocking
        while blocking:
            if timeout:
                if time.time()-blockStartTime > timeout:
                    print(f'Keyboard block timed out after {timeout}s.')
                    return
            for i in range(150):
                try: block_key(i)
                except: pass
    def blockMouse(timeout):
        global blocking
        while blocking:
            def resetMouse(): pyautogui.moveTo(5,5)
            Thread(target=resetMouse).start()
            if timeout:
                if time.time()-blockStartTime > timeout:
                    print(f'Mouse block timed out after {timeout}s.')
                    return
    def blockTimeout(timeout):
        global blocking
        time.sleep(timeout)
        blocking = False
        pyautogui.FAILSAFE = False
        print('Done blocking inputs!')

    print('Blocking inputs...')
    Thread(target=blockKeys, args=[timeout]).start()
    Thread(target=blockMouse, args=[timeout]).start()
    Thread(target=blockTimeout, args=[timeout]).start()


blockMouseAndKeys(timeout=10)
os.startfile('https://www.youtube.com/watch?v=DLzxrzFCyOs')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65801957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档