KeyboardListener
Examples#
Press any keys#
import flet as ft
async def main(page: ft.Page):
pressed_keys = set()
def key_down(e: ft.KeyDownEvent):
pressed_keys.add(e.key)
keys.controls = [ft.Text(k, size=20) for k in pressed_keys]
def key_up(e: ft.KeyUpEvent):
pressed_keys.remove(e.key)
keys.controls = [ft.Text(k, size=20) for k in pressed_keys]
page.add(
ft.Text("Press any keys..."),
ft.KeyboardListener(
content=(keys := ft.Row()),
autofocus=True,
on_key_down=key_down,
on_key_up=key_up,
),
)
ft.run(main)
KeyboardListener
#
Bases: Control
A control that calls a callback whenever the user presses or releases a key on a keyboard.
autofocus
#
autofocus: bool = False
True if this control will be selected as the initial focus when no other node in its scope is currently focused.
col
#
col: ResponsiveNumber = 12
If a parent of this control is a [ResponsiveRow
][flet.],
this property is used to determine
how many virtual columns of a screen this control will span.
Can be a number or a dictionary configured to have a different value for specific
breakpoints, for example col={"sm": 6}
.
This control spans the 12 virtual columns by default.
Dimensions
Breakpoint | Dimension |
---|---|
xs | <576px |
sm | ≥576px |
md | ≥768px |
lg | ≥992px |
xl | ≥1200px |
xxl | ≥1400px |
disabled
#
disabled: bool = False
Every control has disabled
property which is False
by default - control and all
its children are enabled.
Note
The value of this property will be propagated down to all children controls recursively.
expand
#
Specifies whether/how this control should expand to fill available space in its parent layout.
More information here.
Note
Has effect only if the direct parent of this control is one of the following
controls, or their subclasses: [Column
][flet.], [Row
][flet.],
[View
][flet.], [Page
][flet.].
expand_loose
#
expand_loose: bool = False
Allows the control to expand along the main axis if space is available, but does not require it to fill all available space.
More information here.
Note
If expand_loose
is True
, it will have effect only if:
expand
is notNone
and- the direct parent of this control is one of the following controls, or their
subclasses: [
Column
][flet.], [Row
][flet.], [View
][flet.], [Page
][flet.].
on_key_down
#
on_key_down: EventHandler[KeyDownEvent] | None = None
Fires when a keyboard key is pressed.
on_key_repeat
#
on_key_repeat: EventHandler[KeyRepeatEvent] | None = None
Fires when a keyboard key is being hold, causing repeated events.
on_key_up
#
on_key_up: EventHandler[KeyUpEvent] | None = None
Fires when a keyboard key is released.
opacity
#
opacity: Number = 1.0
Defines the transparency of the control.
Value ranges from 0.0
(completely transparent) to 1.0
(completely opaque
without any transparency).
parent
#
parent: BaseControl | None
The direct ancestor(parent) of this control.
It defaults to None
and will only have a value when this control is mounted
(added to the page tree).
The Page
control (which is the root of the tree) is an exception - it always
has parent=None
.
tooltip
#
tooltip: TooltipValue | None = None
The tooltip ot show when this control is hovered over.
visible
#
visible: bool = True
Every control has visible
property which is True
by default - control is
rendered on the page. Setting visible
to False
completely prevents control (and
all its children if any) from rendering on a page canvas. Hidden controls cannot be
focused or selected with a keyboard or mouse and they do not emit any events.
build
#
Called once during control initialization to define its child controls. self.page is available in this method.