简体   繁体   English

pynput键盘侦听器在按下shift键时返回shift,但不会修改shift_pressed

[英]pynput Keyboard Listener returns shift when shift is pressed, but does not modify shift_pressed

I've got a simple program to familiarize myself with Keyboard Listeners using pynput. 我有一个简单的程序,可以使用pynput熟悉键盘监听器。 What it does is not important. 它做什么并不重要。 What is important is that the shift_pressed attribute never seems to change to True . 重要的是, shift_pressed属性似乎永远不会更改为True My program currently looks like this: 我的程序当前如下所示:

from pynput.keyboard import Controller, Listener

boo = True
keyboard = Controller()

fib_lst = [0, 1]


def on_press(key):
    print(key)
    print(keyboard.shift_pressed)


Listener(on_press=on_press).start()

while boo:
    nxt = fib_lst[-1] + fib_lst[-2]
    input(nxt)
    fib_lst.append(nxt)

I'm trying to do something like this in on_press (or on_release ): 我正在尝试在on_press (或on_release )中执行以下操作:

def on_press(key):
    if key == Key.delete:
        if keyboard.shift_pressed:
            func1()

        else:
            func2()

This code should perform func1 when shift is pressed or func2 if it is not. 按下shift键时,此代码应执行func1 ;否则应执行func2 But it is currently only doing func2 since shift_pressed is perpetually false. 但由于shift_pressed永远为假,因此它目前仅在执行func2 What can I do differently to get shift_pressed to work as it should? 为了使shift_pressed正常工作,我可以做些什么?

Edit 1: Specified the desired end result more clearly. 编辑1:更清楚地指定所需的最终结果。

Edit 2: Changed the appending string to two different functions to add more clarity. 编辑2:将附加字符串更改为两个不同的函数,以增加清晰度。

Edit 3: Changed the parameters of the final question to match the more recent example 编辑3:更改了最后一个问题的参数,以匹配最近的示例

I'm not hundred percent sure but when I have worked with pynput I noticed that I can handle action on key release, not key press so you can try something like below: 我不确定百分百,但是当我使用Pynput时,我注意到我可以处理按键释放操作,而不是按键操作,因此您可以尝试以下操作:

from pynput import keyboard

def on_press(key):
    if key == keyboard.Key.shift: # handles if key press is shift
        print('foo', end='')

def on_release(key):
    if key == keyboard.Key.shift:
        print()
    elif key == keyboard.Key.delete:
        print('bar')
    elif key == keyboard.Key.esc:
        return False

def get_current_key_input():
    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()

get_current_key_input()

if you need any other help let me know with your specific motive. 如果您需要任何其他帮助,请告诉我您的具体动机。

This is a bug in the pynput 1.3.5 documentation. 这是pynput 1.3.5文档中的错误。

The various modifier state properties ( alt_pressed , alt_gr_pressed , ctrl_pressed and shift_pressed ) reflect only the state of the Controller instance; 各种修饰符状态属性( alt_pressedalt_gr_pressedctrl_pressedshift_pressed )仅反映Controller实例的状态; it maintains an inner modifier state which is applied when various keys are pressed---for example to uppercase characters from scripts that support it. 它保持一个内部修饰符状态,该状态在按下各种键时会应用,例如,从支持该修饰符的脚本中输入大写字符。

This state is separate from the current operating system state and will only change when you send key presses using that specific controller. 此状态与当前操作系统状态是分开的,并且仅当您使用该特定控制器发送按键时才会更改。

There is no general pynput method to retrieve the current global modifier state. 没有通用的Pynput方法来检索当前的全局修饰符状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM