Skip to content

Tutorial Callback Custom Key Binds

ddbnl edited this page Sep 2, 2022 · 1 revision

Custom keymaps allow you to bind keyboard keys to a widget with a callback. Keep in mind that for this to work, a widget must already be selected; only then will it receive the keyboard event. If you want a keybind to work in all cases, regardless of selection, use Global key bind instead.

To bind a key, we will use the CallbackConfig again, and use its 'bind_key' method. The parameters are:

  • The KeyCode we want to bind;
  • Modifiers, which can be None, or a Vec of KeyModifiers (CTRL/SHIFT/ALT),
  • The callback.

We can call this method as many times as we like to bind different keys.

Let's bind a couple of different keys to a callback closure to demonstrate simple keys and modified keys:

use ez_term::*;
let (root_widget, mut state_tree, mut scheduler) = load_ui();

let my_callback = move |context: Context, keycode: KeyCode| {

    true
};

let mut new_callback_config = CallbackConfig::default();

// Bind a simple character
new_callback_config.bind_key(KeyCode::Char('a'), None, Box::new(my_callback));

// Bind a functional key like the Tab key
new_callback_config.bind_key(KeyCode::Tab, None, Box::new(my_callback));

// Bind a modified character
new_callback_config.bind_key(KeyCode::Char('a'), Some(vec!(KeyModifiers::CONTROL)),
                             Box::new(my_callback));

// Bind a double modifier
new_callback_config.bind_key(KeyCode::Char('a'),
                             Some(vec!(KeyModifiers::CONTROL, KeyModifiers::ALT)),
                             Box::new(my_callback));

scheduler.update_callback_config("my_checkbox", new_callback_config);

To do the same with a function:

use ez_term::*;
let (root_widget, mut state_tree, mut scheduler) = load_ui();

fn my_callback(context: Context, keycode: KeyCode) -> bool {

    true
};

let mut new_callback_config = CallbackConfig::default();
new_callback_config.bind_key(KeyCode::Char('a'), None, Box::new(my_callback));
scheduler.update_callback_config("my_checkbox", new_callback_config);

Continue

The general tutorial continues with: Callback: Global key binds.

Tutorial Tutorial-Project-Structure
  Minimal example
EzLang
  EzLang basics
  EzLang Templates
  Ezlang Layout modes
   EzLang Box mode layouts
   EzLang Stack mode layouts
   EzLang Table mode layouts
   EzLang Float mode layouts
   EzLang Tab mode layouts
   EzLang Screen mode layouts
   EzLang Layout Scrolling
   EzLang Layout Views
  EzLang Widget overview
   EzLang Label
   EzLang Text Input
   EzLang Button
   EzLang Checkbox
   EzLang Radio button
   EzLang Dropdown
   EzLang Slider
   EzLang Canvas
  EzLang Property Binding
  EzLang Sizing
   EzLang Size hints
   EzLang Auto scaling
   EzLang Maths Sizing
   EzLang Manual Sizing
  EzLang Positioning
   EzLang Layout Mode Positioning
   EzLang Position Hints
   EzLang Position Maths
   EzLang Manual Position
   EzLang Adjusting Position
  EzLang Keyboard Selection
Scheduler
  Widget States and the State Tree
  The Scheduler Object
  Managing callbacks
   Callback Structure
   Callback Configs
   Callback: On keyboard enter
   Callback: On Left Mouse Click
   Callback: On Press
   Callback: On Select
   Callback: On Deselect
   Callback: On Right Mouse Click
   Callback: On Hover
   Callback: On Drag
   Callback: On Scroll Up
   Callback: On Scroll Down
   Callback: On Value Change
   Callback: Custom Key Binds
   Callback: Global Key Binds
   Callback: Property Binds
  Tasks
   Scheduled Single Exectution Tasks
   Scheduled Recurring Tasks
   Threaded Tasks
  Custom Properties
  Modals
  Programmatic Widgets
  Updating widgets
  Managing selection
Default global (key)binds
Performance
ExamplesLayout: Box Mode Nested
Layout: Box Mode Size Hints
Layout: Stack Mode
Layout: Table Mode Dynamic
Layout: Table Mode Static
Layout: Float Mode Manual
Layout: Float Mode Position hints
Layout: Screen Mode
Layout: Tab Mode
Layout: Scrolling
Layout: Views
Widget: Label
Widget: Text input
Widget: Button
Widget: Checkbox
Widget: Radio Button
Widget: Dropdown
Widget: Slider
Widget: Progress Bar
Widget: Canvas
Scheduler: Schedule Once
Scheduler: Schedule Once Callback
Scheduler: Schedule Recurring
Scheduler: Schedule Recurring Callback
Scheduler: Threaded Task State Tree
Scheduler: Threaded Task Custom Property
Scheduler: Create Widgets
Scheduler: Modal Popup
Reference Widgets
  Common Properties
  Label
  Text Input
  Button
  Checkbox
  Radio button
  Dropdown
  Slider
  Canvas
Scheduler
  Schedule once
  Schedule Recurring
  Schedule Threaded
  Cancel Task
  Cancel Recurring Task
  Create Widget
  Remove Widget
  Select Widget
  Deselect Widget
  Update Widget
  Force Redraw
  Open Modal
  Dismiss Modal
  Bind Global Key
  Remove Global Key
  Clear Global Keys
  Bind Property
  Create Custom Properties
  Get Property
  Get Property Mut
  Overwrite Callback Config
  Update Callback Config
  Exit
Clone this wiki locally