Merge pull request #134 from malikolivier/105-get-keyboard-input

[ImGui] Add helper functions for keyboard keys

User still needs to use get_key_index to get the value of user_key_index for each key, similar to what is done in the original imgui.
This is not very convenient but it allows to be back-end independent.

Fix #105
This commit is contained in:
Malik Olivier Boussejra 2018-06-02 19:50:27 +09:00 committed by GitHub
commit d42f789fef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -304,6 +304,35 @@ impl ImGui {
let io = self.io_mut();
io.key_map[key as usize] = mapping as i32;
}
/// Map [`ImGuiKey`] values into user's key index
pub fn get_key_index(&self, key: ImGuiKey) -> usize {
unsafe { sys::igGetKeyIndex(key) as usize }
}
/// Return whether specific key is being held
///
/// # Example
///
/// ```rust
/// use imgui::{ImGuiKey, Ui};
///
/// fn test(ui: &Ui) {
/// let delete_key_index = ui.imgui().get_key_index(ImGuiKey::Delete);
/// if ui.imgui().is_key_down(delete_key_index) {
/// println!("Delete is being held!");
/// }
/// }
/// ```
pub fn is_key_down(&self, user_key_index: usize) -> bool {
unsafe { sys::igIsKeyDown(user_key_index as c_int) }
}
/// Return whether specific key was pressed
pub fn is_key_pressed(&self, user_key_index: usize) -> bool {
unsafe { sys::igIsKeyPressed(user_key_index as c_int, true) }
}
/// Return whether specific key was released
pub fn is_key_released(&self, user_key_index: usize) -> bool {
unsafe { sys::igIsKeyReleased(user_key_index as c_int) }
}
pub fn add_input_character(&mut self, character: char) {
let mut buf = [0; 5];
character.encode_utf8(&mut buf);