Search code examples
pythonkeyboardkeyboard-events

Why is this not sending the enter key? [Python]


Why does it not send the enter key? I want it to type the message and then press enter to send it but the enter key is never pressed?

import keyboard, time     

while True:

    if keyboard.is_pressed('ctrl'):
        keyboard.write("This text will be sent.")
        keyboard.press('enter')
        

    if keyboard.is_pressed('esc'):
        break                       

Solution

  • Just needed a time buffer

    import keyboard, time     
    
    while True:
    
        if keyboard.is_pressed('ctrl'):
            keyboard.write("This text will be sent.")
            time.sleep(.1)
            keyboard.send('enter')
            
        if keyboard.is_pressed('esc'):
            break