Skip to content

Commit b59ec4e

Browse files
uefi: Improve Input::read_key docstring
Add intra-doc links and an example showing how to wait for an event and match both printable and special keys.
1 parent 1e8df42 commit b59ec4e

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

uefi/src/proto/console/text/input.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,52 @@ impl Input {
2626

2727
/// Reads the next keystroke from the input device, if any.
2828
///
29-
/// Use `wait_for_key_event()` with the `BootServices::wait_for_event()`
29+
/// Use [`wait_for_key_event`] with the [`BootServices::wait_for_event`]
3030
/// interface in order to wait for a key to be pressed.
3131
///
32+
/// [`BootServices::wait_for_event`]: uefi::table::boot::BootServices::wait_for_event
33+
/// [`wait_for_key_event`]: Self::wait_for_key_event
34+
///
3235
/// # Errors
3336
///
34-
/// - `DeviceError` if there was an issue with the input device
37+
/// - [`Status::DEVICE_ERROR`] if there was an issue with the input device
38+
///
39+
/// # Examples
40+
///
41+
/// ```
42+
/// use log::info;
43+
/// use uefi::proto::console::text::{Input, Key, ScanCode};
44+
/// use uefi::table::boot::BootServices;
45+
/// use uefi::{Char16, Result, ResultExt};
46+
///
47+
/// fn read_keyboard_events(boot_services: &BootServices, input: &mut Input) -> Result {
48+
/// loop {
49+
/// // Pause until a keyboard event occurs.
50+
/// let mut events = unsafe { [input.wait_for_key_event().unsafe_clone()] };
51+
/// boot_services
52+
/// .wait_for_event(&mut events)
53+
/// .discard_errdata()?;
54+
///
55+
/// let u_key = Char16::try_from('u').unwrap();
56+
/// match input.read_key()? {
57+
/// // Example of handling a printable key: print a message when
58+
/// // the 'u' key is pressed.
59+
/// Some(Key::Printable(key)) if key == u_key => {
60+
/// info!("the 'u' key was pressed");
61+
/// }
62+
///
63+
/// // Example of handling a special key: exit the loop when the
64+
/// // escape key is pressed.
65+
/// Some(Key::Special(ScanCode::ESCAPE)) => {
66+
/// break;
67+
/// }
68+
/// _ => {}
69+
/// }
70+
/// }
71+
///
72+
/// Ok(())
73+
/// }
74+
/// ```
3575
pub fn read_key(&mut self) -> Result<Option<Key>> {
3676
let mut key = MaybeUninit::<RawKey>::uninit();
3777

0 commit comments

Comments
 (0)