|
| 1 | +use crossterm::event::KeyCode; |
| 2 | +use rustboyadvance_core::keypad as gba_keypad; |
| 3 | + |
| 4 | +use bit; |
| 5 | +use bit::BitIndex; |
| 6 | + |
| 7 | +pub fn on_keyboard_key_down(key_state: &mut u16, scancode: KeyCode) { |
| 8 | + if let Some(key) = scancode_to_keypad(scancode) { |
| 9 | + key_state.set_bit(key as usize, false); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +pub fn on_keyboard_key_up(key_state: &mut u16, scancode: KeyCode) { |
| 14 | + if let Some(key) = scancode_to_keypad(scancode) { |
| 15 | + key_state.set_bit(key as usize, true); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +fn scancode_to_keypad(scancode: KeyCode) -> Option<gba_keypad::Keys> { |
| 20 | + match scancode { |
| 21 | + KeyCode::Up => Some(gba_keypad::Keys::Up), |
| 22 | + KeyCode::Down => Some(gba_keypad::Keys::Down), |
| 23 | + KeyCode::Left => Some(gba_keypad::Keys::Left), |
| 24 | + KeyCode::Right => Some(gba_keypad::Keys::Right), |
| 25 | + KeyCode::Char('z') => Some(gba_keypad::Keys::ButtonB), |
| 26 | + KeyCode::Char('x') => Some(gba_keypad::Keys::ButtonA), |
| 27 | + KeyCode::Enter => Some(gba_keypad::Keys::Start), |
| 28 | + KeyCode::Backspace => Some(gba_keypad::Keys::Select), |
| 29 | + KeyCode::Char('a') => Some(gba_keypad::Keys::ButtonL), |
| 30 | + KeyCode::Char('s') => Some(gba_keypad::Keys::ButtonR), |
| 31 | + _ => None, |
| 32 | + } |
| 33 | +} |
0 commit comments