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,
impl<L> EventDecoder<L>where
L: KeyboardLayout,
Sourcepub const fn new(layout: L, handle_ctrl: HandleControl) -> EventDecoder<L>
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}
Sourcepub fn set_ctrl_handling(&mut self, new_value: HandleControl)
pub fn set_ctrl_handling(&mut self, new_value: HandleControl)
Change the Ctrl key mapping.
Sourcepub const fn get_ctrl_handling(&self) -> HandleControl
pub const fn get_ctrl_handling(&self) -> HandleControl
Get the current Ctrl key mapping.
Sourcepub fn process_keyevent(&mut self, ev: KeyEvent) -> Option<DecodedKey>
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}
Sourcepub fn change_layout(&mut self, new_layout: L)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more