简体   繁体   English

为什么keyboard.is_pressed() 函数在按下时拒绝工作?

[英]why does the keyboard.is_pressed() function refuse to work when pressed?

i have less than 3 months of python programming under my belt but basically, i have a program that pulls values from the CoinGecko API indefinitely and creates processes so the functions that pull the data can run independently from one another, but id like for it to break its loop after i press the specified hotkey which is set to 'q'.我有不到 3 个月的 Python 编程经验,但基本上,我有一个程序可以无限期地从 CoinGecko API 中提取值并创建进程,因此提取数据的函数可以彼此独立运行,但我喜欢它在我按下设置为“q”的指定热键后打破它的循环。 whenever i press the hotkey, nothing happens and the loop just keeps running.每当我按下热键时,什么都没有发生,循环只是继续运行。 i've tried using the keyboard.read_key() function, but that just stops my program from running until i press the q button, which causes the program to run the loop once and then close.我尝试过使用keyboard.read_key()函数,但这只会停止我的程序运行,直到我按下q 按钮,这会导致程序运行一次循环然后关闭。 i have no idea why the is_pressed() function refuses to work and id like some help from more advanced people我不知道为什么is_pressed()函数拒绝工作并且像更高级的人的帮助一样

Piece of Code in question:有问题的代码段:

from multiprocessing.dummy import freeze_support                
from pycoingecko import CoinGeckoAPI
import time
from multiprocessing import Process
from multiprocessing import Pool
import multiprocessing
import keyboard as kb
import psutil




cg = CoinGeckoAPI()


class CGCoin:
    def __init__(self, coinname, coinid):
        self.coinname = coinname
        self.coinid = coinid
   

    def pulldata(self):
       
        while True:
             
            wishtoquit = False
            if kb.is_pressed('Q'):
                print('ending after this loop')
                wishtoquit = True
                
            
            timestarted = time.asctime()
            
            self.prices = []
            self.daychanges = []
            self.volumes = []
            self.marketcaps = []
            self.weekchanges = []
            self.highs = []
            self.lows = []
            self.times = []
            print(f'starting {self.coinname} reading at {timestarted}')
            loops = 0 
            maxloops = 2
            while loops < maxloops:
                
                time.sleep(15)
                coin = cg.get_coin_by_id(f'{self.coinid}')
                time.sleep(5)
                coinvalues = coin.get('market_data')
                coinprices = coinvalues.get('current_price')
                coinvolumes = coinvalues.get('total_volume')
                mrktcaps = coinvalues.get('market_cap')
                dayhigh = coinvalues.get('high_24h')
                daylow = coinvalues.get('low_24h')
                daychangepercentage = coinvalues.get('price_change_percentage_24h')
                weekchangepercentage = coinvalues.get('price_change_percentage_7d')
                coinprice = coinprices.get('usd') 
                coinvolume = coinvolumes.get('usd')
                coincap = mrktcaps.get('usd')
                coindayhigh = dayhigh.get('usd')
                coindaylow = daylow.get('usd')
                timepulled = time.asctime()


                self.prices.append(coinprice)
                self.daychanges.append(daychangepercentage)
                self.volumes.append(coinvolume)
                self.marketcaps.append(coincap)
                self.weekchanges.append(weekchangepercentage)
                self.highs.append(coindayhigh)
                self.lows.append(coindaylow)
                self.times.append(timepulled)
                loops = loops + 1 
                print(loops)
            timeended = time.asctime()



            })
            print(f'stopping {self.coinname} reading at {timeended}')
            if wishtoquit:
                print('ending loops')
                
                break

            time.sleep(5)

           


            
bitcoin = CGCoin('Bitcoin', 'bitcoin')
ethereum = CGCoin('Ethereum', 'ethereum')
if __name__ == '__main__':
    freeze_support()
    btcpul = Process(target=bitcoin.pulldata, name=bitcoin.coinname)
    btcpul.start()
 

if anyone has any ideas or fully-functional workarounds id really like to hear them.如果有人有任何想法或功能齐全的解决方法,我真的很想听听。 id be extremely grateful for any help recieved我非常感谢收到的任何帮助

It looks like PyPi keyboard needs root permissions on linux.看起来 PyPi 键盘在 linux 上需要 root 权限。

You could just do kb.on_press_key("p", lambda _: sys.exit(0)) and just do a sys.exit(0) to end the script.您可以只执行kb.on_press_key("p", lambda _: sys.exit(0))并执行 sys.exit(0) 来结束脚本。

If you're running this in the terminal you should just be able to press ctrl+c to interrupt its execution.如果你在终端中运行它,你应该可以按ctrl+c来中断它的执行。

ref: How to detect key presses?参考: 如何检测按键?

I don't have time to add everything you've imported to my computer, but is it because the "Q" is a capital?我没有时间将您导入的所有内容添加到我的计算机中,但是否因为“Q”是大写字母?

if kb.is_pressed('Q'):如果 kb.is_pressed('Q'):

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

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