本文介绍了如何在函数运行期间忽略所有用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 模块,它使用 pynput 侦听组合键,然后,一旦按下它,它就会在文本程序中键入一个字符串.

I have a Python module that listens for a key combination using pynput then, once it's pressed, it types a string into a text program.

效果很好!除了...

在下面的示例中,用户的组合键设置为 shift + space.这很有意义,并且可能是运行我的程序的 Windows 用户最常用的键盘命令.问题是,当按住 shift 键时,它会改变 pynput 类型.它将输入 )!/20/2019,而不是 01/20/2019.

In the example below, the user's key combo is set to shift + space. This makes a lot of sense and will likely be the most common chosen key command for Windows users running my program. The trouble is, while the shift key is held down it changes what pynput types. Instead of 01/20/2019, it will type )!/20/2019.

我需要一种方法来禁用键盘,直到 pyautogui 完成输入字符串.非常感谢您的帮助!!!

I need a way to disable the keyboard until pyautogui is finished typing the string. Thanks a lot for your help!!!

额外问题:当组合键包含 ctrl 键时,我似乎无法得到结果.Key.ctrl 根本无法触发,而其他键工作正常.

Bonus question: I can't seem to get a result when the key combination includes a ctrl key. Key.ctrl simply fails to ever trigger, whilst other keys work fine.

from pynput.keyboard import Key, Controller, Listener
import time

keyboard = Controller()

def insert():                                 # check line 1 of config file
     keyboard.type('01/20/2019')

# The currently active modifiers
current = set()

def on_press(key):
    if key in COMBINATION:
        current.add(key)
        if all(k in current for k in COMBINATION):      # I don't know what this k in current for k shit is.
            current.remove(key)
            insert()                                # run insert

    if key == Key.esc:
        listener.stop()


def on_release(key):
    try:
        current.remove(key)
    except KeyError:
        pass

with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
``

推荐答案

您可以在输入之前使用 pyautogui.keyUp("shift").

You can use pyautogui.keyUp("shift") before you type.

def insert():
f=open('tc.txt')
line=f.readlines()
insert.timestamp=(line[0])
time.sleep(.1)
pyautogui.keyUp("shift")
pyautogui.typewrite(insert.timestamp)

这篇关于如何在函数运行期间忽略所有用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:42