Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pynput press return key, or some other line of code that presses return

Tags:

python

After each 0 is typed, I want it so it'll press enter afterward, I've tried different lines but it didn't, I'm a complete beginner, pls help.

from pynput.keyboard import Key, Controller

import time

keyboard = Controller()

time.sleep(1)

for char in " 0 0 0 0 0 0 0":

 keyboard.press('0')

keyboard.release('0')

time.sleep(0.25)
like image 213
Sandy Patty Avatar asked Oct 15 '22 04:10

Sandy Patty


1 Answers

Pressing an enter with pynput is similar to what you have done, see here from the docs. You would want to do something like the following.

word = " 0 0 0 0 0 0 0"
for i, char in enumerate(word):
    if char == "0":
      keyboard.press('0')
      keyboard.release('0')

    #last iteration
    if char == "0" and len(word)-1 == i:
       keyboard.press(Key.enter)
       keyboard.release(Key.enter)
       break #break the loop here
like image 125
AzyCrw4282 Avatar answered Oct 18 '22 15:10

AzyCrw4282