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,
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)
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);
}
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)
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);
}
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