Home > Software engineering >  How can I write a sentence or word using this syntax?
How can I write a sentence or word using this syntax?

Time:06-24

Here is the code I have:

import os
from pynput.keyboard import Key, Controller
import time

os.startfile('C:\\Users\\A\\Desktop\\Blank_Document.docx')
time.sleep(3)

keyboard = Controller()
key = "Books are enjoyable to read."

keyboard.press(key)

But if the key is a word or a sentence it doesn't work. Is there a way to do it without pasting the same syntax multiple times? I'd like to write the sentence 'Books are enjoyable to read.'

CodePudding user response:

You can use a loop.

sentence = "Books are enjoyable to read."
for key in sentence:
    # time.sleep(.05) you may need to make a small sleep between inputs.
    keyboard.press(key)

  • Related