简体   繁体   English

当我在键盘模块中按下一个键时如何枚举数字?

[英]How to enumerate number when I press a key in the Keyboard module?

How to enumerate number when I press a key in the Keyboard module?当我在键盘模块中按下一个键时如何枚举数字?

But no all the number at once但是一次没有所有的数字

For example if I have the numbers 1,2,3,4,5例如,如果我有数字 1,2,3,4,5

when I press 'r' once, the number 1 is displayed then If I press R a second time it's 2 that is displayed and then 3 etc当我按一次“r”时,会显示数字 1,然后如果我第二次按 R,则会显示 2,然后显示 3 等

Until it reaches 5 the next time we press the key we'll got 1 again.直到下次我们按下键时它达到 5,我们将再次得到 1。

Idk if I'm clear but when I make the loop it display all of them at once. Idk 如果我很清楚,但是当我制作循环时,它会立即显示所有这些。

Use a variable count that holds the amount of clicks you have made.使用变量count来保存您的点击量。 To reset it to 1, you can use an if statement like so:要将其重置为 1,您可以使用如下if语句:

if count > 5:
    count = 1

Alternatively, you can use a modulo % operator like you mentioned:或者,您可以使用您提到的%运算符:

count = count % 5

EDIT: to increment count variable when key is pressed, use a loop.编辑:要在按下键时增加计数变量,请使用循环。 Full code:完整代码:

count = 0
while True:
    # check if 'r' is pressed
    if keybord.is_pressed('r'):
        # increment count variable
        count += 1
        # reset it to 1 if it is greater than 5
        # alternatively use modulo operator
        if count > 5:
            count = 1

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

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