File tree 2 files changed +354
-10
lines changed
2 files changed +354
-10
lines changed Original file line number Diff line number Diff line change
1
+ //! Demonstrates the display format of key events.
2
+ //!
3
+ //! This example demonstrates the display format of key events, which is useful for displaying in
4
+ //! the help section of a terminal application.
5
+ //!
6
+ //! cargo run --example key-display
7
+
8
+ use std:: io;
9
+
10
+ use crossterm:: event:: { KeyEventKind , KeyModifiers } ;
11
+ use crossterm:: {
12
+ event:: { read, Event , KeyCode } ,
13
+ terminal:: { disable_raw_mode, enable_raw_mode} ,
14
+ } ;
15
+
16
+ const HELP : & str = r#"Key display
17
+ - Press any key to see its display format
18
+ - Use Esc to quit
19
+ "# ;
20
+
21
+ fn main ( ) -> io:: Result < ( ) > {
22
+ println ! ( "{}" , HELP ) ;
23
+ enable_raw_mode ( ) ?;
24
+ if let Err ( e) = print_events ( ) {
25
+ println ! ( "Error: {:?}\r " , e) ;
26
+ }
27
+ disable_raw_mode ( ) ?;
28
+ Ok ( ( ) )
29
+ }
30
+
31
+ fn print_events ( ) -> io:: Result < ( ) > {
32
+ loop {
33
+ let event = read ( ) ?;
34
+ match event {
35
+ Event :: Key ( event) if event. kind == KeyEventKind :: Press => {
36
+ print ! ( "Key pressed: " ) ;
37
+ if event. modifiers != KeyModifiers :: NONE {
38
+ print ! ( "{}+" , event. modifiers) ;
39
+ }
40
+ println ! ( "{}\r " , event. code) ;
41
+ if event. code == KeyCode :: Esc {
42
+ break ;
43
+ }
44
+ }
45
+ _ => { }
46
+ }
47
+ }
48
+ Ok ( ( ) )
49
+ }
You can’t perform that action at this time.
0 commit comments