Jesus
Jesus

Reputation: 5

how do I get the input from sg.InputText and set the key inputted it to keyboard.press()

When I press "Run" it'll type the key that was provided by sg.InputText. The error it gives is ValueError(Key)

from pynput.keyboard import Key, Controller 
import time
import PySimpleGUI as sg

sg.theme('Black')
layout = [
            [sg.Text('Key To Click'), sg.InputText()],
            [sg.Button('Run'), sg.Button('Close Application')] ]
window = sg.Window('Macro', layout)

keyboard = Controller()



while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Close Application': 
        break
    print('You entered ', values[0])
    if event == 'Run':
        keyboard.press(values[0])
        if values == None:
            print('Please Type A Letter!')

    window.close()

Upvotes: 0

Views: 50

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

The event handler for 'Run' looks wrong, should check what entered, then what action next.

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED or event == 'Close Application':
        break

    print('You entered ', values[0])

    if event == 'Run':
        key = values[0]
        if len(key) == 1:
            keyboard.press(key)
        else:
            print('Please Type A Letter!')

window.close()

Upvotes: 1

Related Questions