Skip to main content

keycode_to_char

Function keycode_to_char 

Source
pub fn keycode_to_char(code: KeyCode) -> Option<char>
Expand description

Converts a KeyCode into its corresponding character, if applicable.

§Parameters

  • code: The KeyCode to convert.

§Returns

  • Some(char): If the KeyCode represents a printable character.
  • None: If the KeyCode is not a character (e.g., arrow keys, function keys).

§Example

fn main() -> FtuiResult<()> {
    // Capture user keyboard input as a KeyCode.
    // If reading fails, terminate with an error.
    let key_code = key()?;

    // If a key was pressed, attempt to convert it to a character.
    match key_code {
        Some(code) => match keycode_to_char(code) {
            // Print the character if it's a printable key.
            Some(c) => println!("Key pressed: {}", c), 
            None => println!("Unprintable KeyCode"), 
        },
        // No key was pressed, exit the function.
        None => return, 
    }

    Ok(())
}