Custom control that allows you to detect the state of modifier keys (Shift, Ctrl, Alt, Meta) at any time.
Add dependency to pyproject.toml of your Flet app:
- Git dependency
Link to git repository:
dependencies = [
"flet==0.28.3",
"flet-modifier-key @ git+https://github.com/masayukid/flet-modifier-key",
]
- PyPi dependency
If the package is published on pypi.org:
dependencies = [
"flet==0.28.3",
"flet-modifier-key",
]
import flet as ft
from flet_modifier_key import ModifierKey
def main(page: ft.Page):
# Create a ModifierKey control
mk = ModifierKey()
def on_click(e):
if mk.shift:
print("Shift+Click detected!")
elif mk.control:
print("Ctrl+Click detected!")
elif mk.alt:
print("Alt+Click detected!")
else:
print("Normal click")
page.add(
mk, # Add the ModifierKey control (it's invisible)
ft.ElevatedButton("Click me", on_click=on_click)
)
ft.app(target=main)shift: bool- ReturnsTrueif the Shift key is currently pressedcontrol: bool- ReturnsTrueif the Control (Ctrl) key is currently pressedalt: bool- ReturnsTrueif the Alt key is currently pressedmeta: bool- ReturnsTrueif the Meta key (Windows/Command) is currently pressed
on_change- Triggered when any modifier key state changes
- Multi-selection: Implement Shift+Click for range selection, Ctrl+Click for toggle
- Custom scroll behavior: Zoom with Ctrl+Scroll, horizontal scroll with Shift+Scroll
- Enhanced interactions: Different drag behaviors based on modifier keys
- Keyboard shortcuts: Detect modifier keys for custom shortcuts
- Canvas/Drawing apps: Different tools or behaviors when keys are held