Skip to content

Commit 4aa5ef6

Browse files
authored
Add getter for the Modifiers struct to Keyboard (#40)
* Add getter for the Modifiers struct to Keyboard
1 parent 327a855 commit 4aa5ef6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
.idea/
22
/target
33
**/*.rs.bk
44
Cargo.lock

src/lib.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ pub trait ScancodeSet {
420420
}
421421

422422
/// The set of modifier keys you have on a keyboard.
423-
#[derive(Debug, Default)]
423+
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
424424
pub struct Modifiers {
425425
/// The left shift key is down
426426
pub lshift: bool,
@@ -505,6 +505,11 @@ where
505505
}
506506
}
507507

508+
/// Get the current key modifier states.
509+
pub const fn get_modifiers(&self) -> &Modifiers {
510+
&self.event_decoder.modifiers
511+
}
512+
508513
/// Change the Ctrl key mapping.
509514
pub fn set_ctrl_handling(&mut self, new_value: HandleControl) {
510515
self.event_decoder.set_ctrl_handling(new_value);
@@ -1594,6 +1599,28 @@ mod test {
15941599

15951600
process_keyevents(&mut k, &test_sequence);
15961601
}
1602+
1603+
#[test]
1604+
fn test_modifier_state_shift() {
1605+
let mut k = Keyboard::new(
1606+
ScancodeSet2::new(),
1607+
layouts::Uk105Key,
1608+
HandleControl::MapLettersToUnicode,
1609+
);
1610+
assert!(!k.get_modifiers().lshift);
1611+
1612+
k.process_keyevent(KeyEvent {
1613+
code: KeyCode::LShift,
1614+
state: KeyState::Down,
1615+
});
1616+
assert!(k.get_modifiers().lshift);
1617+
1618+
k.process_keyevent(KeyEvent {
1619+
code: KeyCode::LShift,
1620+
state: KeyState::Up,
1621+
});
1622+
assert!(!k.get_modifiers().lshift);
1623+
}
15971624
}
15981625

15991626
// ****************************************************************************

0 commit comments

Comments
 (0)