在 python 中重复 pynput

Repeating in python pynput

from pynput.keyboard import Key,Controller import time

keyboard = Controller()

time.sleep(60)

# 1
keyboard.press(Key.enter)
keyboard.release(Key.enter)

# 2
i = 0
while i < 4:
    keyboard.press(Key.tab)
    keyboard.release(Key.tab)
    i = i + 1;

# 3
i= 0
while i < 3:
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    i = i + 1;

# 4
i = 0
while i < 4:
    with keyboard.pressed(Key.shift):
        keyboard.press(Key.tab)
        keyboard.release(Key.tab)
        i = i + 1;

我想让上面的代码无限循环,但它不起作用。怎么了?

你能给我一些建议吗?关于GUI和EXE归档

我想你想要这样的东西你无限期地循环,因为 True 好吧......总是评估为真,所以循环永远不会中断,除非你明确告诉它。

from pynput.keyboard import Key,Controller 
import time

keyboard = Controller()

while True:
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    for i in range(0, 4):
        keyboard.press(Key.tab)
        keyboard.release(Key.tab)
    for i in range(0, 3):
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
   ...

   time.sleep(60)

最好将 while 语句替换为 for 循环。这是典型的做法,当您打算执行一段代码确定的次数时,即 4 次,而不是当您想要执行一段代码的时间不确定时,您使用 while 循环