-
please reply how to bind a key for example enter key with textfield |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Could you please elaborate what do you mean or what are you trying to achieve? |
Beta Was this translation helpful? Give feedback.
-
You can handle global keyboard events with the If you want the keyboard event to only work if the TextField is highlighted you'll have to add a variable that is set to True with the text_field_in_focus = False
def on_focus_callback(_):
global text_field_in_focus
text_field_in_focus = True
def on_blur_callback(_):
global text_field_in_focus
text_field_in_focus = False
def run_program(event: ft.KeyboardEvent):
if event.key == 'Q' and text_field_in_focus:
print("Run program!")
page.add(
ft.TextField(on_focus=on_focus_callback, on_blur=on_blur_callback)
)
page.on_keyboard_event = run_program Alternatively if the key is a printable character you can check the # Check if the last character entered in a TextField is 'q'.
def on_change_callback(event):
if event.control.value[-1].lower() == 'q':
print("Run program!")
page.add(
ft.TextField(on_change=on_change_callback)
) I highly recommend taking a bit of time to read through the docs, they are very good and can answer many questions quickly. |
Beta Was this translation helpful? Give feedback.
You can handle global keyboard events with the
page.on_keyboard_event
callback, see the docs here: https://flet.dev/docs/guides/python/keyboard-shortcuts/If you want the keyboard event to only work if the TextField is highlighted you'll have to add a variable that is set to True with the
on_focus
callback of your TextField and set to False with theon_blur
callback of your TextField. The you can check that the variable is set before running your program, see the docs here: https://flet.dev/docs/controls/textfield/#on_focus