Struct pc_keyboard::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)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn main() {
    let mut decoder = EventDecoder::new(
        AnyLayout::Uk105Key(Uk105Key),
        pc_keyboard::HandleControl::Ignore,
    );

    // User presses 'A' on their UK keyboard, gets a lower-case 'a'.
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Down,
    });
    assert_eq!(Some(DecodedKey::Unicode('a')), decoded_key);
    println!("Got {:?}", decoded_key);

    // User releases 'A' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);

    // User presses 'Shift' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::LShift,
        state: KeyState::Down,
    });
    assert_eq!(None, decoded_key);

    // User presses 'A' on their UK keyboard, now gets a Capital A
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Down,
    });
    assert_eq!(Some(DecodedKey::Unicode('A')), decoded_key);
    println!("Got {:?}", decoded_key);

    // User releases 'A' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);

    // User releases 'Shift' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::LShift,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);
}
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)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn main() {
    let mut decoder = EventDecoder::new(
        AnyLayout::Uk105Key(Uk105Key),
        pc_keyboard::HandleControl::Ignore,
    );

    // User presses 'A' on their UK keyboard, gets a lower-case 'a'.
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Down,
    });
    assert_eq!(Some(DecodedKey::Unicode('a')), decoded_key);
    println!("Got {:?}", decoded_key);

    // User releases 'A' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);

    // User presses 'Shift' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::LShift,
        state: KeyState::Down,
    });
    assert_eq!(None, decoded_key);

    // User presses 'A' on their UK keyboard, now gets a Capital A
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Down,
    });
    assert_eq!(Some(DecodedKey::Unicode('A')), decoded_key);
    println!("Got {:?}", decoded_key);

    // User releases 'A' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::A,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);

    // User releases 'Shift' on their UK keyboard
    let decoded_key = decoder.process_keyevent(KeyEvent {
        code: KeyCode::LShift,
        state: KeyState::Up,
    });
    assert_eq!(None, decoded_key);
}
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.