pub struct KeyRepeater { /* private fields */ }Expand description
Implementations§
Source§impl KeyRepeater
impl KeyRepeater
Sourcepub fn new(config: RepeatConfig) -> Result<Self, InputError>
pub fn new(config: RepeatConfig) -> Result<Self, InputError>
Create a repeater with its own timer.
§Errors
InputError::Config for a zero interval, which would leave the timer
firing as fast as the kernel can wake the loop. A zero delay is
allowed and means the first repeat lands on the next tick.
InputError::Timer if the timer cannot be created.
Examples found in repository?
28fn main() {
29 let mut seat = match Seat::open(&SeatOptions::default()) {
30 Ok(seat) => seat,
31 Err(error) => {
32 eprintln!("{error}");
33 std::process::exit(1);
34 }
35 };
36 let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37 let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39 let started = Instant::now();
40 let mut events = Vec::new();
41 let mut repeats: Vec<KeyEvent> = Vec::new();
42 let mut leds = keyboard.leds();
43
44 loop {
45 {
46 let seat_fd = seat.as_fd();
47 let timer_fd = repeater.fd();
48 let mut fds = [
49 PollFd::new(&seat_fd, PollFlags::IN),
50 PollFd::new(&timer_fd, PollFlags::IN),
51 ];
52 let timeout = Duration::from_secs(30).try_into().expect("timeout");
53 if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54 println!("nothing for thirty seconds");
55 return;
56 }
57 }
58
59 let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61 events.clear();
62 seat.dispatch(&mut events).expect("dispatch");
63 for event in &mut events {
64 match event {
65 InputEvent::Key(key) => {
66 keyboard.process_key(key);
67 repeater.on_key(key, keyboard.should_repeat(key.key));
68 if report(key) {
69 return;
70 }
71 }
72 other => println!("{other:?}"),
73 }
74 }
75
76 repeats.clear();
77 repeater.dispatch(now, &mut repeats).expect("repeat");
78 for key in &mut repeats {
79 // Resolved, not replayed: a Shift pressed since the key went down
80 // changes what this types.
81 keyboard.resolve(key);
82 report(key);
83 }
84
85 let current = keyboard.leds();
86 if current != leds {
87 seat.set_leds(current);
88 leds = current;
89 }
90 }
91}Sourcepub fn fd(&self) -> BorrowedFd<'_>
pub fn fd(&self) -> BorrowedFd<'_>
The descriptor to poll. Readable when a repeat is due.
Examples found in repository?
28fn main() {
29 let mut seat = match Seat::open(&SeatOptions::default()) {
30 Ok(seat) => seat,
31 Err(error) => {
32 eprintln!("{error}");
33 std::process::exit(1);
34 }
35 };
36 let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37 let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39 let started = Instant::now();
40 let mut events = Vec::new();
41 let mut repeats: Vec<KeyEvent> = Vec::new();
42 let mut leds = keyboard.leds();
43
44 loop {
45 {
46 let seat_fd = seat.as_fd();
47 let timer_fd = repeater.fd();
48 let mut fds = [
49 PollFd::new(&seat_fd, PollFlags::IN),
50 PollFd::new(&timer_fd, PollFlags::IN),
51 ];
52 let timeout = Duration::from_secs(30).try_into().expect("timeout");
53 if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54 println!("nothing for thirty seconds");
55 return;
56 }
57 }
58
59 let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61 events.clear();
62 seat.dispatch(&mut events).expect("dispatch");
63 for event in &mut events {
64 match event {
65 InputEvent::Key(key) => {
66 keyboard.process_key(key);
67 repeater.on_key(key, keyboard.should_repeat(key.key));
68 if report(key) {
69 return;
70 }
71 }
72 other => println!("{other:?}"),
73 }
74 }
75
76 repeats.clear();
77 repeater.dispatch(now, &mut repeats).expect("repeat");
78 for key in &mut repeats {
79 // Resolved, not replayed: a Shift pressed since the key went down
80 // changes what this types.
81 keyboard.resolve(key);
82 report(key);
83 }
84
85 let current = keyboard.leds();
86 if current != leds {
87 seat.set_leds(current);
88 leds = current;
89 }
90 }
91}Sourcepub fn on_key(&mut self, event: &KeyEvent, repeats: bool)
pub fn on_key(&mut self, event: &KeyEvent, repeats: bool)
Feed a real key event.
repeats is the keymap’s answer for this key; see
Repeat::on_key.
Examples found in repository?
28fn main() {
29 let mut seat = match Seat::open(&SeatOptions::default()) {
30 Ok(seat) => seat,
31 Err(error) => {
32 eprintln!("{error}");
33 std::process::exit(1);
34 }
35 };
36 let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37 let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39 let started = Instant::now();
40 let mut events = Vec::new();
41 let mut repeats: Vec<KeyEvent> = Vec::new();
42 let mut leds = keyboard.leds();
43
44 loop {
45 {
46 let seat_fd = seat.as_fd();
47 let timer_fd = repeater.fd();
48 let mut fds = [
49 PollFd::new(&seat_fd, PollFlags::IN),
50 PollFd::new(&timer_fd, PollFlags::IN),
51 ];
52 let timeout = Duration::from_secs(30).try_into().expect("timeout");
53 if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54 println!("nothing for thirty seconds");
55 return;
56 }
57 }
58
59 let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61 events.clear();
62 seat.dispatch(&mut events).expect("dispatch");
63 for event in &mut events {
64 match event {
65 InputEvent::Key(key) => {
66 keyboard.process_key(key);
67 repeater.on_key(key, keyboard.should_repeat(key.key));
68 if report(key) {
69 return;
70 }
71 }
72 other => println!("{other:?}"),
73 }
74 }
75
76 repeats.clear();
77 repeater.dispatch(now, &mut repeats).expect("repeat");
78 for key in &mut repeats {
79 // Resolved, not replayed: a Shift pressed since the key went down
80 // changes what this types.
81 keyboard.resolve(key);
82 report(key);
83 }
84
85 let current = keyboard.leds();
86 if current != leds {
87 seat.set_leds(current);
88 leds = current;
89 }
90 }
91}Sourcepub fn dispatch(
&mut self,
time_ms: u32,
out: &mut Vec<KeyEvent>,
) -> Result<(), InputError>
pub fn dispatch( &mut self, time_ms: u32, out: &mut Vec<KeyEvent>, ) -> Result<(), InputError>
Drain the timer and append whatever repeats are due.
Nothing is appended if no key is held, or if the descriptor had nothing
to say. The events carry only the key – run them through
Keyboard::resolve for sym and utf8.
§Errors
InputError::Timer if the timer cannot be read. EAGAIN is not one:
a descriptor that was not ready simply produces nothing.
Examples found in repository?
28fn main() {
29 let mut seat = match Seat::open(&SeatOptions::default()) {
30 Ok(seat) => seat,
31 Err(error) => {
32 eprintln!("{error}");
33 std::process::exit(1);
34 }
35 };
36 let mut keyboard = Keyboard::from_names(&KeymapNames::default()).expect("keymap");
37 let mut repeater = KeyRepeater::new(RepeatConfig::default()).expect("timer");
38
39 let started = Instant::now();
40 let mut events = Vec::new();
41 let mut repeats: Vec<KeyEvent> = Vec::new();
42 let mut leds = keyboard.leds();
43
44 loop {
45 {
46 let seat_fd = seat.as_fd();
47 let timer_fd = repeater.fd();
48 let mut fds = [
49 PollFd::new(&seat_fd, PollFlags::IN),
50 PollFd::new(&timer_fd, PollFlags::IN),
51 ];
52 let timeout = Duration::from_secs(30).try_into().expect("timeout");
53 if poll(&mut fds, Some(&timeout)).expect("poll") == 0 {
54 println!("nothing for thirty seconds");
55 return;
56 }
57 }
58
59 let now = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
60
61 events.clear();
62 seat.dispatch(&mut events).expect("dispatch");
63 for event in &mut events {
64 match event {
65 InputEvent::Key(key) => {
66 keyboard.process_key(key);
67 repeater.on_key(key, keyboard.should_repeat(key.key));
68 if report(key) {
69 return;
70 }
71 }
72 other => println!("{other:?}"),
73 }
74 }
75
76 repeats.clear();
77 repeater.dispatch(now, &mut repeats).expect("repeat");
78 for key in &mut repeats {
79 // Resolved, not replayed: a Shift pressed since the key went down
80 // changes what this types.
81 keyboard.resolve(key);
82 report(key);
83 }
84
85 let current = keyboard.leds();
86 if current != leds {
87 seat.set_leds(current);
88 leds = current;
89 }
90 }
91}