1
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 归档

4

1 回答 1

1

我认为你想要这样的东西,你会无限循环,因为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循环

于 2018-03-10T15:55:39.770 回答