Struct EventDecoder

Source
pub struct EventDecoder<L>
where L: KeyboardLayout,
{ /* private fields */ }
Expand description

Converts KeyEvents into Unicode, according to the current Keyboard Layout

Implementations§

Source§

impl<L> EventDecoder<L>
where L: KeyboardLayout,

Source

pub const fn new(layout: L, handle_ctrl: HandleControl) -> EventDecoder<L>

Construct a new event decoder.

Examples found in repository?
examples/layout.rs (lines 7-10)
6fn main() {
7    let mut decoder = EventDecoder::new(
8        AnyLayout::Uk105Key(Uk105Key),
9        pc_keyboard::HandleControl::Ignore,
10    );
11
12    // User presses 'A' on their UK keyboard, gets a lower-case 'a'.
13    let decoded_key = decoder.process_keyevent(KeyEvent {
14        code: KeyCode::A,
15        state: KeyState::Down,
16    });
17    assert_eq!(Some(DecodedKey::Unicode('a')), decoded_key);
18    println!("Got {:?}", decoded_key);
19
20    // User releases 'A' on their UK keyboard
21    let decoded_key = decoder.process_keyevent(KeyEvent {
22        code: KeyCode::A,
23        state: KeyState::Up,
24    });
25    assert_eq!(None, decoded_key);
26
27    // User presses 'Shift' on their UK keyboard
28    let decoded_key = decoder.process_keyevent(KeyEvent {
29        code: KeyCode::LShift,
30        state: KeyState::Down,
31    });
32    assert_eq!(None, decoded_key);
33
34    // User presses 'A' on their UK keyboard, now gets a Capital A
35    let decoded_key = decoder.process_keyevent(KeyEvent {
36        code: KeyCode::A,
37        state: KeyState::Down,
38    });
39    assert_eq!(Some(DecodedKey::Unicode('A')), decoded_key);
40    println!("Got {:?}", decoded_key);
41
42    // User releases 'A' on their UK keyboard
43    let decoded_key = decoder.process_keyevent(KeyEvent {
44        code: KeyCode::A,
45        state: KeyState::Up,
46    });
47    assert_eq!(None, decoded_key);
48
49    // User releases 'Shift' on their UK keyboard
50    let decoded_key = decoder.process_keyevent(KeyEvent {
51        code: KeyCode::LShift,
52        state: KeyState::Up,
53    });
54    assert_eq!(None, decoded_key);
55}
Source

pub fn set_ctrl_handling(&mut self, new_value: HandleControl)

Change the Ctrl key mapping.

Source

pub const fn get_ctrl_handling(&self) -> HandleControl

Get the current Ctrl key mapping.

Source

pub fn process_keyevent(&mut self, ev: KeyEvent) -> Option<DecodedKey>

Processes a KeyEvent returned from add_bit, add_byte or add_word and produces a decoded key.

For example, the KeyEvent for pressing the ‘5’ key on your keyboard gives a DecodedKey of unicode character ‘5’, unless the shift key is held in which case you get the unicode character ‘%’.

Examples found in repository?
examples/layout.rs (lines 13-16)
6fn main() {
7    let mut decoder = EventDecoder::new(
8        AnyLayout::Uk105Key(Uk105Key),
9        pc_keyboard::HandleControl::Ignore,
10    );
11
12    // User presses 'A' on their UK keyboard, gets a lower-case 'a'.
13    let decoded_key = decoder.process_keyevent(KeyEvent {
14        code: KeyCode::A,
15        state: KeyState::Down,
16    });
17    assert_eq!(Some(DecodedKey::Unicode('a')), decoded_key);
18    println!("Got {:?}", decoded_key);
19
20    // User releases 'A' on their UK keyboard
21    let decoded_key = decoder.process_keyevent(KeyEvent {
22        code: KeyCode::A,
23        state: KeyState::Up,
24    });
25    assert_eq!(None, decoded_key);
26
27    // User presses 'Shift' on their UK keyboard
28    let decoded_key = decoder.process_keyevent(KeyEvent {
29        code: KeyCode::LShift,
30        state: KeyState::Down,
31    });
32    assert_eq!(None, decoded_key);
33
34    // User presses 'A' on their UK keyboard, now gets a Capital A
35    let decoded_key = decoder.process_keyevent(KeyEvent {
36        code: KeyCode::A,
37        state: KeyState::Down,
38    });
39    assert_eq!(Some(DecodedKey::Unicode('A')), decoded_key);
40    println!("Got {:?}", decoded_key);
41
42    // User releases 'A' on their UK keyboard
43    let decoded_key = decoder.process_keyevent(KeyEvent {
44        code: KeyCode::A,
45        state: KeyState::Up,
46    });
47    assert_eq!(None, decoded_key);
48
49    // User releases 'Shift' on their UK keyboard
50    let decoded_key = decoder.process_keyevent(KeyEvent {
51        code: KeyCode::LShift,
52        state: KeyState::Up,
53    });
54    assert_eq!(None, decoded_key);
55}
Source

pub fn change_layout(&mut self, new_layout: L)

Change the keyboard layout.

Only useful with layouts::AnyLayout, otherwise you can only change a layout for exactly the same layout.

Trait Implementations§

Source§

impl<L> Debug for EventDecoder<L>
where L: KeyboardLayout + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<L> Freeze for EventDecoder<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for EventDecoder<L>
where L: RefUnwindSafe,

§

impl<L> Send for EventDecoder<L>
where L: Send,

§

impl<L> Sync for EventDecoder<L>
where L: Sync,

§

impl<L> Unpin for EventDecoder<L>
where L: Unpin,

§

impl<L> UnwindSafe for EventDecoder<L>
where L: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.