Skip to content

Instantly share code, notes, and snippets.

@fkaa
Created April 11, 2017 07:42
Show Gist options
  • Save fkaa/3216d6a4e6be47ce0cd4a98d5f6b1139 to your computer and use it in GitHub Desktop.
Save fkaa/3216d6a4e6be47ce0cd4a98d5f6b1139 to your computer and use it in GitHub Desktop.
use winit::{WindowEvent, ElementState};
static mut KEYS: [bool; 256] = [false; 256];
static mut KEYS_PRESSED: [bool; 256] = [false; 256];
static mut KEYS_RELEASED: [bool; 256] = [false; 256];
pub fn key_poll(evt: WindowEvent) {
match evt {
WindowEvent::KeyboardInput(ElementState::Pressed, scan, _, _) => {
unsafe {
KEYS[scan as usize] = true;
KEYS_PRESSED[scan as usize] = true;
}
},
WindowEvent::KeyboardInput(ElementState::Released, scan, _, _) => {
unsafe {
KEYS[scan as usize] = false;
KEYS_RELEASED[scan as usize] = true;
}
},
_ => {}
}
}
pub fn key_down(code: u8) -> bool {
unsafe { KEYS[code as usize] }
}
pub fn key_pressed(code: u8) -> bool {
unsafe { KEYS[code as usize] }
}
pub fn key_released(code: u8) -> bool {
unsafe { KEYS[code as usize] }
}
pub fn frame() {
unsafe {
KEYS_PRESSED = [false; 256];
KEYS_RELEASED = [false; 256];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment