每当我按下键时,我如何才能将其递增和打印,而不仅仅是一次?

发布于 2025-01-21 18:09:31 字数 369 浏览 0 评论 0原文

使用以下程序,每当我按“ A”键时,键>变量 1 。问题是,如果我不立即放任键,键>键>变量将继续通过1递增。

每当我按键以忽略保留方面时,我如何才能将其递增(和打印)

import keyboard
keypresses = 0
while True:
    if keyboard.is_pressed("a"):
        keypresses = keypresses +1
        print(keypresses)   
        print("A Key Pressed")
        break

With the below program, whenever I press the "a" key, the keypresses variable increments by 1. The problem is, if I don't immediately let go fo the key, the keypresses variable continues to increment by 1.

How can I get it to increment (and print) whenever I press the key, ignoring the hold aspect?

import keyboard
keypresses = 0
while True:
    if keyboard.is_pressed("a"):
        keypresses = keypresses +1
        print(keypresses)   
        print("A Key Pressed")
        break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2025-01-28 18:09:31

我了解您想要它,以便仅一旦(不是无限期)按下打印,并且只要a键再次释放并再次按下,它就会再次打印。

定义一个变量,,以获取是否仍在按下或发布键:

import keyboard

keypresses = 0
pressing = False

while True:
    if keyboard.is_pressed("a"):
        if not pressing:
            pressing = True
            keypresses = keypresses +1
            print(keypresses)   
            print("A Key Pressed")
    else:
        pressing = False

I understand that you want it so that pressing and holding only prints once (not indefinitely), and it will print again as long as the a key is released and pressed again.

Define a variable, pressing, to get if the key is still being pressed, or released:

import keyboard

keypresses = 0
pressing = False

while True:
    if keyboard.is_pressed("a"):
        if not pressing:
            pressing = True
            keypresses = keypresses +1
            print(keypresses)   
            print("A Key Pressed")
    else:
        pressing = False
聽兲甴掵 2025-01-28 18:09:31

如果您删除这样的休息语句:

import keyboard
keypresses = 0
while True:
    if keyboard.is_pressed("a"):
        keypresses = keypresses +1
        print(keypresses)   
        print("A Key Pressed")
    break

这是不起作用的,并且运行它时,它会立即完成。
因为键盘.is_pressed仅检测现在
正是由于此事,

如果您在Windows中,这会导致CPU太多,您可以将msvcrt代替:-)

if you remove the break statement like this:

import keyboard
keypresses = 0
while True:
    if keyboard.is_pressed("a"):
        keypresses = keypresses +1
        print(keypresses)   
        print("A Key Pressed")
    break

this does not work, and when you run it, it is instantly finished the while cycle.
because the keyboard.is_pressed only detect now
and precisely because of this, which caused too many cpu

if you are in windows , you can use msvcrt to instead :-)

裂开嘴轻声笑有多痛 2025-01-28 18:09:31

我将删除break语句。
python docs 在/循环时,将最内向的退出,在您的情况下,该循环将是

while True:

I'd remove the break statement.
On the Python docs you can see that break exits the innermost for/while loop, in your case the that loop would be the

while True:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文