第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在python中檢測按鍵,其中每次迭代可能需要超過幾秒鐘的時間?

在python中檢測按鍵,其中每次迭代可能需要超過幾秒鐘的時間?

楊魅力 2022-08-16 10:51:12
編輯:下面使用keyboard.on_press(回調(diào),suppress=False)的答案在ubuntu中工作正常,沒有任何問題。但是在Redhat/Amazon linux中,它無法正常工作。我使用了此線程中的代碼片段import keyboard  # using module keyboardwhile True:  # making a loop    try:  # used try so that if user pressed other than the given key error will not be shown        if keyboard.is_pressed('q'):  # if key 'q' is pressed             print('You Pressed A Key!')            break  # finishing the loop    except:        break  # if user pressed a key other than the given key the loop will break但上面的代碼要求每次迭代都要在納秒內(nèi)執(zhí)行。在以下情況下會失?。篿mport keyboard  # using module keyboardimport timewhile True:  # making a loop    try:  # used try so that if user pressed other than the given key error will not be shown        print("sleeping")        time.sleep(5)        print("slept")        if keyboard.is_pressed('q'):  # if key 'q' is pressed             print('You Pressed A Key!')            break  # finishing the loop    except:        print("#######")        break  # if user pressed a key other than the given key the loop will break
查看完整描述

4 回答

?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個贊

您可以使用模塊中的事件處理程序來實(shí)現(xiàn)所需的結(jié)果。keyboard


一個這樣的處理程序是keyboard.on_press(回調(diào),suppress=False):它為每個事件調(diào)用一個回調(diào)。您可以在鍵盤文檔中了解更多信息key_down


以下是您可以嘗試的代碼:


import keyboard  # using module keyboard

import time


stop = False

def onkeypress(event):

    global stop

    if event.name == 'q':

        stop = True


# ---------> hook event handler

keyboard.on_press(onkeypress)

# --------->


while True:  # making a loop

    try:  # used try so that if user pressed other than the given key error will not be shown

        print("sleeping")

        time.sleep(5)

        print("slept")

        if stop:  # if key 'q' is pressed 

            print('You Pressed A Key!')

            break  # finishing the loop

    except:

        print("#######")

        break  # if user pressed a key other than the given key the loop will break


查看完整回答
反對 回復(fù) 2022-08-16
?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個贊

對于將來可能需要它的人,您可以使用它基本上等到按鍵被按下keyboard.wait()


keyboard.wait("o")

print("you pressed the letter o")

請記住,它會阻止代碼執(zhí)行。如果你想運(yùn)行代碼,如果鍵沒有被按下,我建議做


if keyboard.is_pressed("0"): 

    #do stuff

else: 

    #do other stuff


查看完整回答
反對 回復(fù) 2022-08-16
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個贊

沒關(guān)系,另一個答案使用幾乎相同的方法


這是我可以想到的,使用相同的“鍵盤”模塊,請參閱下面的代碼內(nèi)注釋


import keyboard, time

from queue import Queue 


# keyboard keypress callback

def on_keypress(e): 

    keys_queue.put(e.name)


# tell keyboard module to tell us about keypresses via callback

# this callback happens on a separate thread

keys_queue = Queue() 

keyboard.on_press(on_keypress)


try:

    # run the main loop until some key is in the queue

    while keys_queue.empty():  

        print("sleeping")

        time.sleep(5)

        print("slept")

    # check if its the right key

    if keys_queue.get()!='q':

        raise Exception("terminated by bad key")

    # still here? good, this means we've been stoped by the right key

    print("terminated by correct key")

except:

    # well, you can 

    print("#######")

finally:

    # stop receiving the callback at this point

    keyboard.unhook_all()


查看完整回答
反對 回復(fù) 2022-08-16
?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個贊

您可以使用線程


import threading


class KeyboardEvent(threading.Thread):

    def run(self):

        if keyboard.is_pressed('q'):  # if key 'q' is pressed 

            print('You Pressed A Key!')

            break  # finishing the loop


keyread = KeyboardEvent()

keyread.start()

這將與主線程中的任何內(nèi)容并行運(yùn)行,并且專門用于基本上偵聽該按鍵。


查看完整回答
反對 回復(fù) 2022-08-16
  • 4 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號