From 903b2d7eadd7fe3f82ffd79b6b80f1b07590bace Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 2 Mar 2025 10:23:37 -0800 Subject: [PATCH] Fix arrow keys --- tetros/src/lib.rs | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/tetros/src/lib.rs b/tetros/src/lib.rs index 8b5e649..5e48eb9 100644 --- a/tetros/src/lib.rs +++ b/tetros/src/lib.rs @@ -89,23 +89,37 @@ enum InputKey { } extern "x86-interrupt" fn keyboard_handler(_stack_frame: InterruptStackFrame) { - let scancode = unsafe { inb(0x60) }; - println!("{:x?}", scancode); + 'key_block: { + let scancode = unsafe { inb(0x60) }; + let key = match scancode { + 0x11 => Some(InputKey::Up), // W + 0x1E => Some(InputKey::Left), // A + 0x1F => Some(InputKey::Down), // S + 0x20 => Some(InputKey::Right), // D - let key = match scancode { - 0x11 => Some(InputKey::Up), - 0x1E => Some(InputKey::Left), - 0x1F => Some(InputKey::Down), - 0x20 => Some(InputKey::Right), - 0xE0 => { - let scancode = unsafe { inb(0x60) }; - println!("e {:x?}", scancode); - None - } - _ => None, - }; + // Extended codes + 0xE0 => { + let scancode = unsafe { inb(0x60) }; + match scancode { + 0x48 => Some(InputKey::Up), // Up arrow + 0x4B => Some(InputKey::Left), // Left arrow + 0x50 => Some(InputKey::Down), // Down arrow + 0x4D => Some(InputKey::Right), // Right arrow + _ => break 'key_block, // Ignore unknown codes + } + } + + // Ignore unrecognized keycodes. + // + // Note that this does NOT return to None! + // If we do that, "release" keycodes will + // immediately clear "press" keycodes. + _ => break 'key_block, + }; + + *LAST_INPUT.lock() = key; + } - *LAST_INPUT.lock() = key; PIC.lock().send_eoi(InterruptIndex::Keyboard.as_u8()); }