Move Keycode and Scancode into the keyboard module, adjust spelling

* The `keycode` and `scancode` modules only contained a single enum each, so
it makes more sense to put them into the `keyboard` module.

* KeyCode -> Keycode, ScanCode -> Scancode.
A small detail, but this is consistent with SDL_Keycode and SDL_Scancode.
This commit is contained in:
Dan Spencer
2015-06-14 02:04:34 -06:00
parent ef23bab267
commit 2af5dcfac4
10 changed files with 56 additions and 55 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::keycode::KeyCode;
use sdl2::keyboard::Keycode;
pub fn main() {
let mut sdl_context = sdl2::init().video().unwrap();
@@ -26,7 +26,7 @@ pub fn main() {
use sdl2::event::Event;
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
running = false
},
_ => {}
+2 -2
View File
@@ -1,6 +1,6 @@
extern crate sdl2;
use sdl2::keycode::KeyCode;
use sdl2::keyboard::Keycode;
use std::collections::HashSet;
pub fn main() {
@@ -26,7 +26,7 @@ pub fn main() {
}
// Create a set of pressed Keys.
let keys = sdl_context.keyboard_state().pressed_scancodes().filter_map(KeyCode::from_scancode).collect();
let keys = sdl_context.keyboard_state().pressed_scancodes().filter_map(Keycode::from_scancode).collect();
// Get the difference between the new and old sets.
let new_keys = &keys - &prev_keys;
+2 -2
View File
@@ -2,7 +2,7 @@ extern crate sdl2;
use sdl2::pixels::PixelFormatEnum;
use sdl2::rect::Rect;
use sdl2::keycode::KeyCode;
use sdl2::keyboard::Keycode;
pub fn main() {
let mut sdl_context = sdl2::init().video().unwrap();
@@ -41,7 +41,7 @@ pub fn main() {
use sdl2::event::Event;
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
running = false
},
_ => {}
+2 -2
View File
@@ -2,7 +2,7 @@ extern crate sdl2;
use sdl2::pixels::PixelFormatEnum;
use sdl2::rect::Rect;
use sdl2::keycode::KeyCode;
use sdl2::keyboard::Keycode;
pub fn main() {
let mut sdl_context = sdl2::init().video().unwrap();
@@ -57,7 +57,7 @@ pub fn main() {
use sdl2::event::Event;
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
running = false
},
_ => {}
+2 -2
View File
@@ -18,10 +18,10 @@ pub fn main() {
while running {
for event in sdl_context.event_pump().poll_iter() {
use sdl2::event::Event;
use sdl2::keycode::KeyCode;
use sdl2::keyboard::Keycode;
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
running = false
},
_ => {}
+6 -6
View File
@@ -18,10 +18,10 @@ use joystick::HatState;
use keyboard;
use keyboard::Mod;
use sys::keycode::SDL_Keymod;
use keycode::KeyCode;
use keyboard::Keycode;
use mouse;
use mouse::{Mouse, MouseState};
use scancode::ScanCode;
use keyboard::Scancode;
use get_error;
use SdlResult;
use Sdl;
@@ -211,16 +211,16 @@ pub enum Event {
KeyDown {
timestamp: u32 ,
window_id: u32,
keycode: Option<KeyCode>,
scancode: Option<ScanCode>,
keycode: Option<Keycode>,
scancode: Option<Scancode>,
keymod: Mod,
repeat: bool
},
KeyUp {
timestamp: u32 ,
window_id: u32,
keycode: Option<KeyCode>,
scancode: Option<ScanCode>,
keycode: Option<Keycode>,
scancode: Option<Scancode>,
keymod: Mod,
repeat: bool
},
@@ -4,7 +4,7 @@ use std::ffi::{CString, CStr};
use sys::keycode as ll;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum KeyCode {
pub enum Keycode {
Backspace = ll::SDLK_BACKSPACE as isize,
Tab = ll::SDLK_TAB as isize,
Return = ll::SDLK_RETURN as isize,
@@ -242,7 +242,7 @@ pub enum KeyCode {
Sleep = ll::SDLK_SLEEP as isize,
}
impl ToPrimitive for KeyCode {
impl ToPrimitive for Keycode {
#[inline]
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
@@ -259,9 +259,9 @@ impl ToPrimitive for KeyCode {
}
}
impl FromPrimitive for KeyCode {
fn from_i64(n: i64) -> Option<KeyCode> {
use self::KeyCode::*;
impl FromPrimitive for Keycode {
fn from_i64(n: i64) -> Option<Keycode> {
use self::Keycode::*;
Some( match n as ll::SDL_Keycode {
ll::SDLK_UNKNOWN => return None,
@@ -504,24 +504,24 @@ impl FromPrimitive for KeyCode {
})
}
fn from_u64(n: u64) -> Option<KeyCode> {
fn from_u64(n: u64) -> Option<Keycode> {
FromPrimitive::from_i64(n as i64)
}
}
use std::fmt;
impl fmt::Display for KeyCode {
impl fmt::Display for Keycode {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.name())
}
}
use scancode::ScanCode;
use keyboard::Scancode;
impl KeyCode {
impl Keycode {
/// Gets the virtual key from a scancode. Returns None if there is no corresponding virtual key.
pub fn from_scancode(scancode: ScanCode) -> Option<KeyCode> {
pub fn from_scancode(scancode: Scancode) -> Option<Keycode> {
unsafe {
match ::sys::keyboard::SDL_GetKeyFromScancode(scancode as u32) {
ll::SDLK_UNKNOWN => None,
@@ -530,7 +530,7 @@ impl KeyCode {
}
}
pub fn from_name(name: &str) -> Option<KeyCode> {
pub fn from_name(name: &str) -> Option<Keycode> {
unsafe {
match CString::new(name) {
Ok(name) => match ::sys::keyboard::SDL_GetKeyFromName(name.as_ptr()) {
@@ -3,11 +3,15 @@ use std::ptr;
use Sdl;
use rect::Rect;
use scancode::ScanCode;
use video::Window;
use sys::keyboard as ll;
mod keycode;
mod scancode;
pub use self::keycode::Keycode;
pub use self::scancode::Scancode;
bitflags! {
flags Mod: u32 {
const NOMOD = 0x0000,
@@ -57,13 +61,13 @@ impl<'sdl> KeyboardState<'sdl> {
///
/// # Example
/// ```no_run
/// use sdl2::scancode::ScanCode;
/// use sdl2::keyboard::Scancode;
///
/// fn is_a_pressed(sdl_context: &mut sdl2::Sdl) -> bool {
/// sdl_context.keyboard_state().is_scancode_pressed(ScanCode::A)
/// sdl_context.keyboard_state().is_scancode_pressed(Scancode::A)
/// }
/// ```
pub fn is_scancode_pressed(&self, scancode: ScanCode) -> bool {
pub fn is_scancode_pressed(&self, scancode: Scancode) -> bool {
self.keyboard_state[ToPrimitive::to_isize(&scancode).unwrap() as usize] != 0
}
@@ -79,21 +83,21 @@ impl<'sdl> KeyboardState<'sdl> {
///
/// # Example
/// ```no_run
/// use sdl2::keycode::KeyCode;
/// use sdl2::scancode::ScanCode;
/// use sdl2::keyboard::Keycode;
/// use sdl2::keyboard::Scancode;
/// use std::collections::HashSet;
///
/// fn pressed_scancode_set(sdl_context: &sdl2::Sdl) -> HashSet<ScanCode> {
/// fn pressed_scancode_set(sdl_context: &sdl2::Sdl) -> HashSet<Scancode> {
/// sdl_context.keyboard_state().pressed_scancodes().collect()
/// }
///
/// fn pressed_keycode_set(sdl_context: &sdl2::Sdl) -> HashSet<KeyCode> {
/// fn pressed_keycode_set(sdl_context: &sdl2::Sdl) -> HashSet<Keycode> {
/// sdl_context.keyboard_state().pressed_scancodes()
/// .filter_map(KeyCode::from_scancode)
/// .filter_map(Keycode::from_scancode)
/// .collect()
/// }
///
/// fn newly_pressed(old: &HashSet<ScanCode>, new: &HashSet<ScanCode>) -> HashSet<ScanCode> {
/// fn newly_pressed(old: &HashSet<Scancode>, new: &HashSet<Scancode>) -> HashSet<Scancode> {
/// new - old
/// // sugar for: new.difference(old).collect()
/// }
@@ -111,9 +115,9 @@ pub struct ScancodeIterator<'a> {
}
impl<'a> Iterator for ScancodeIterator<'a> {
type Item = (ScanCode, bool);
type Item = (Scancode, bool);
fn next(&mut self) -> Option<(ScanCode, bool)> {
fn next(&mut self) -> Option<(Scancode, bool)> {
if self.index < self.keyboard_state.len() {
let index = self.index;
self.index += 1;
@@ -136,9 +140,9 @@ pub struct PressedScancodeIterator<'a> {
}
impl<'a> Iterator for PressedScancodeIterator<'a> {
type Item = ScanCode;
type Item = Scancode;
fn next(&mut self) -> Option<ScanCode> {
fn next(&mut self) -> Option<Scancode> {
while let Some((scancode, pressed)) = self.iter.next() {
if pressed { return Some(scancode) }
}
@@ -4,7 +4,7 @@ use std::ffi::{CString, CStr};
use sys::scancode as ll;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum ScanCode {
pub enum Scancode {
A = ll::SDL_SCANCODE_A as isize,
B = ll::SDL_SCANCODE_B as isize,
C = ll::SDL_SCANCODE_C as isize,
@@ -248,7 +248,7 @@ pub enum ScanCode {
Num = ll::SDL_NUM_SCANCODES as isize,
}
impl ToPrimitive for ScanCode {
impl ToPrimitive for Scancode {
#[inline]
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
@@ -265,9 +265,9 @@ impl ToPrimitive for ScanCode {
}
}
impl FromPrimitive for ScanCode {
fn from_i64(n: i64) -> Option<ScanCode> {
use self::ScanCode::*;
impl FromPrimitive for Scancode {
fn from_i64(n: i64) -> Option<Scancode> {
use self::Scancode::*;
Some( match n as ll::SDL_Scancode {
ll::SDL_SCANCODE_UNKNOWN => return None,
@@ -516,22 +516,22 @@ impl FromPrimitive for ScanCode {
})
}
fn from_u64(n: u64) -> Option<ScanCode> { FromPrimitive::from_i64(n as i64) }
fn from_u64(n: u64) -> Option<Scancode> { FromPrimitive::from_i64(n as i64) }
}
use std::fmt;
impl fmt::Display for ScanCode {
impl fmt::Display for Scancode {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.name())
}
}
use keycode::KeyCode;
use keyboard::Keycode;
impl ScanCode {
impl Scancode {
/// Gets the scancode from a virtual key. Returns None if there is no corresponding scancode.
pub fn from_keycode(keycode: KeyCode) -> Option<ScanCode> {
pub fn from_keycode(keycode: Keycode) -> Option<Scancode> {
unsafe {
match ::sys::keyboard::SDL_GetScancodeFromKey(keycode as i32) {
ll::SDL_SCANCODE_UNKNOWN => None,
@@ -540,7 +540,7 @@ impl ScanCode {
}
}
pub fn from_name(name: &str) -> Option<ScanCode> {
pub fn from_name(name: &str) -> Option<Scancode> {
unsafe {
match CString::new(name) {
Ok(name) => match ::sys::keyboard::SDL_GetScancodeFromName(name.as_ptr()) {
-3
View File
@@ -9,9 +9,6 @@ extern crate sdl2_sys as sys;
pub use sdl::*;
pub mod keycode;
pub mod scancode;
pub mod clipboard;
pub mod cpuinfo;
#[macro_use] pub mod macros;