Skip to content

Commit d7b1443

Browse files
committed
Add an asynchronous print_keypresses task
1 parent d63ddde commit d7b1443

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
extern crate alloc;
88

99
use blog_os::println;
10-
use blog_os::task::{simple_executor::SimpleExecutor, Task};
10+
use blog_os::task::{keyboard, simple_executor::SimpleExecutor, Task};
1111
use bootloader::{entry_point, BootInfo};
1212
use core::panic::PanicInfo;
1313

@@ -29,6 +29,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
2929

3030
let mut executor = SimpleExecutor::new();
3131
executor.spawn(Task::new(example_task()));
32+
executor.spawn(Task::new(keyboard::print_keypresses()));
3233
executor.run();
3334

3435
#[cfg(test)]

src/task/keyboard.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
use crate::println;
1+
use crate::{print, println};
22
use conquer_once::spin::OnceCell;
33
use core::{
44
pin::Pin,
55
task::{Context, Poll},
66
};
77
use crossbeam_queue::ArrayQueue;
8-
use futures_util::{stream::Stream, task::AtomicWaker};
8+
use futures_util::{
9+
stream::{Stream, StreamExt},
10+
task::AtomicWaker,
11+
};
12+
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
913

1014
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
1115
static WAKER: AtomicWaker = AtomicWaker::new();
@@ -61,3 +65,19 @@ impl Stream for ScancodeStream {
6165
}
6266
}
6367
}
68+
69+
pub async fn print_keypresses() {
70+
let mut scancodes = ScancodeStream::new();
71+
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore);
72+
73+
while let Some(scancode) = scancodes.next().await {
74+
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
75+
if let Some(key) = keyboard.process_keyevent(key_event) {
76+
match key {
77+
DecodedKey::Unicode(character) => print!("{}", character),
78+
DecodedKey::RawKey(key) => print!("{:?}", key),
79+
}
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)