1
2
3
4
5
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
use reaction::prelude::*;
use reaction::state::keyboard::KeyboardState;
#[test]
fn test_keyboard_press_release() {
let mut kb = KeyboardState::new();
let frame = 1;
let time = 100.0;
// Press
kb.press(KeyCode::KeyA, time, frame);
assert!(kb.is_down(KeyCode::KeyA));
assert!(kb.was_just_pressed(KeyCode::KeyA));
assert!(!kb.was_just_released(KeyCode::KeyA));
// End frame
kb.end_frame(16.0);
assert!(kb.is_down(KeyCode::KeyA));
assert!(!kb.was_just_pressed(KeyCode::KeyA)); // Should be false next frame
// Release
kb.release(KeyCode::KeyA, frame + 1);
assert!(!kb.is_down(KeyCode::KeyA));
assert!(kb.was_just_released(KeyCode::KeyA));
// End frame
kb.end_frame(16.0);
assert!(!kb.was_just_released(KeyCode::KeyA));
}
#[test]
fn test_held_duration() {
let mut kb = KeyboardState::new();
kb.press(KeyCode::Space, 0.0, 1);
kb.end_frame(100.0);
// Internally we access via public API, but check logic
// We can't easily check private duration field without accessor or just relying on internal logic correctness
// But we know end_frame accumulates it.
// Check re-press logic
kb.press(KeyCode::Space, 200.0, 2);
// If already down, held duration continues (usually depending on raw input behavior, but our press() resets if not down)
// Actually our press() implementation checks `if !state.is_down` before resetting.
// So repeated press calls while down are ignored, which is correct for repeated events from OS.
}