【问题标题】:python read keyboard key bad performancepython读取键盘键性能不佳
【发布时间】:2022-01-20 08:31:19
【问题描述】:

我正在开发一个程序,我需要用户输入进行选择,而不是专注于控制台窗口。我想使用的方法是在小键盘上使用键盘输入。我找到了这个库Python keyboard lib 来实现这一点。 我这里的问题是python注册按键需要很长时间并且给人一种性能不佳的感觉。 我需要知道是否按下了 numpad 4 或 numpad 6 进行导航。在 lib 的 wiki 中提到你不应该使用:

while True:
if keyboard.is_pressed('space'):
    print('space was pressed!')

This will use 100% of your CPU and print the message many times.

所以,这是我的代码:

print("Choose Attacker or Defender operator:")
print("  Attacker    Defender")
att_state = False
def_state = False
while True:
    if keyboard.read_key() == "4":
        clear()
        print("->Attacker    Defender")
        def_state = False
        att_state = True
    if keyboard.read_key() == "6":
        clear()
        print("  Attacker  ->Defender")
        att_state = False
        def_state = True
    if keyboard.read_key() == "5" and att_state:
        clear()
        printAllOp(attackers)
        break
    if keyboard.read_key() == "5" and def_state:
        clear()
        printAllOp(defenders)
        break

selection = 0
while att_state:
    if keyboard.read_key() == "4":
        if selection > 0:
            selection -= 1
            clear()
            printAllOp(attackers, selection)
    if keyboard.read_key() == "6":
        if selection < 31:
            selection += 1
            clear()
            printAllOp(attackers, selection)
    if keyboard.read_key() == "2":
        if selection < 23:
            selection += 7
            clear()
            printAllOp(attackers, selection)
    if keyboard.read_key() == "8":
        if selection > 6:
            selection -= 7
            clear()
            printAllOp(attackers, selection)
    if keyboard.read_key() == "5":
        clear()
        searchOp(attackers, selection, att_source)
        att_state = False
        break

我也意识到使用 if 和 elif 时的性能是不同的,这就是为什么现在一切都是用 ifs 编写的。

【问题讨论】:

    标签: python performance optimization input keyboard


    【解决方案1】:

    您可以为每个 while 迭代设置微秒或毫秒睡眠,睡眠 10 微秒,

    import time
    
    while True:
        if keyboard.is_pressed('space'):
            print('space was pressed!')
        time.sleep(seconds/100000.0)
    

    这应该会减少您的 CPU 使用率,因此应该会提高整体性能。

    更好的是,使用keyboard.on_press 之类的键盘挂钩。这才是正确的做法。

    此外,如果您可以绑定到操作系统(Windows 或特定版本的 Linux 发行版),您可以compile your python as binary,使用 PyInstaller 非常简单。

    【讨论】:

    • 如果焦点不在运行 Python 的控制台窗口上,我认为 keyboard.on_press 不会起作用。这就是为什么我在回答中建议使用add_hotkey() 回调。我也不认为向busyloop添加睡眠会提高通过轮询检测击键的性能..?
    • 不是CPU问题。使用率低于 1%。这与代码有关。
    【解决方案2】:

    您根本不应该使用keyboard.read_key(),而是使用keyboard.add_hotkey() 来注册修改全局状态的回调。

    例子:

    att_state = False
    def_state = False
    
    def set_state(new_state):
        att_state = (new_state == 'att')
        def_state = not att_state
        # add code to update display
    
    # Register callbacks for 4 and 6
    keyboard.add_hotkey('4', lambda: set_state('att'))
    keyboard.add_hotkey('6', lambda: set_state('def'))
    
    # Process keystrokes until '5' is pressed
    keyboard.wait(hotkey='5')
    

    注意:我从来没有使用过这个库,也没有测试过这个代码(没有 Windows)所以买家要小心 :)

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 2012-08-15
      • 1970-01-01
      • 2013-06-03
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多