mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-11 21:48:36 +00:00
Cleaner method for sending keys to ImGui across platforms
Use Virtual Key codes however glutin is missing virtual key codes for Ctrl/Shift and such on my Win10 machines
This commit is contained in:
parent
8520d0e245
commit
238f5fead1
@ -15,7 +15,7 @@ default = ["glium"]
|
||||
libc = "0.1"
|
||||
|
||||
[dependencies.glium]
|
||||
version = "0.9"
|
||||
version = "0.9.3"
|
||||
default-features = false
|
||||
optional = true
|
||||
|
||||
@ -30,6 +30,6 @@ gcc = "0.3"
|
||||
time = "0.1"
|
||||
|
||||
[dev-dependencies.glium]
|
||||
version = "0.9"
|
||||
version = "0.9.3"
|
||||
features = ["glutin"]
|
||||
default-features = false
|
||||
|
||||
@ -25,26 +25,25 @@ impl Support {
|
||||
let mut imgui = ImGui::init();
|
||||
let renderer = Renderer::init(&mut imgui, &display).unwrap();
|
||||
|
||||
// TODO: How can we get the virtual key -> scancode mapping from glium?
|
||||
imgui.set_imgui_key(ImGuiKey::Tab, 15);
|
||||
imgui.set_imgui_key(ImGuiKey::LeftArrow, 75);
|
||||
imgui.set_imgui_key(ImGuiKey::RightArrow, 77);
|
||||
imgui.set_imgui_key(ImGuiKey::UpArrow, 72);
|
||||
imgui.set_imgui_key(ImGuiKey::DownArrow, 80);
|
||||
imgui.set_imgui_key(ImGuiKey::PageUp, 73);
|
||||
imgui.set_imgui_key(ImGuiKey::PageDown, 81);
|
||||
imgui.set_imgui_key(ImGuiKey::Home, 71);
|
||||
imgui.set_imgui_key(ImGuiKey::End, 79);
|
||||
imgui.set_imgui_key(ImGuiKey::Delete, 83);
|
||||
imgui.set_imgui_key(ImGuiKey::Backspace, 14);
|
||||
imgui.set_imgui_key(ImGuiKey::Enter, 28);
|
||||
imgui.set_imgui_key(ImGuiKey::Escape, 1);
|
||||
imgui.set_imgui_key(ImGuiKey::A, 30);
|
||||
imgui.set_imgui_key(ImGuiKey::C, 46);
|
||||
imgui.set_imgui_key(ImGuiKey::V, 47);
|
||||
imgui.set_imgui_key(ImGuiKey::X, 45);
|
||||
imgui.set_imgui_key(ImGuiKey::Y, 21);
|
||||
imgui.set_imgui_key(ImGuiKey::Z, 44);
|
||||
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
||||
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
||||
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
||||
imgui.set_imgui_key(ImGuiKey::UpArrow, 3);
|
||||
imgui.set_imgui_key(ImGuiKey::DownArrow, 4);
|
||||
imgui.set_imgui_key(ImGuiKey::PageUp, 5);
|
||||
imgui.set_imgui_key(ImGuiKey::PageDown, 6);
|
||||
imgui.set_imgui_key(ImGuiKey::Home, 7);
|
||||
imgui.set_imgui_key(ImGuiKey::End, 8);
|
||||
imgui.set_imgui_key(ImGuiKey::Delete, 9);
|
||||
imgui.set_imgui_key(ImGuiKey::Backspace, 10);
|
||||
imgui.set_imgui_key(ImGuiKey::Enter, 11);
|
||||
imgui.set_imgui_key(ImGuiKey::Escape, 12);
|
||||
imgui.set_imgui_key(ImGuiKey::A, 13);
|
||||
imgui.set_imgui_key(ImGuiKey::C, 14);
|
||||
imgui.set_imgui_key(ImGuiKey::V, 15);
|
||||
imgui.set_imgui_key(ImGuiKey::X, 16);
|
||||
imgui.set_imgui_key(ImGuiKey::Y, 17);
|
||||
imgui.set_imgui_key(ImGuiKey::Z, 18);
|
||||
|
||||
Support {
|
||||
display: display,
|
||||
@ -89,18 +88,39 @@ impl Support {
|
||||
for event in self.display.poll_events() {
|
||||
match event {
|
||||
Event::Closed => return false,
|
||||
// TODO: Why does glutin not give us scancodes for left/right control?
|
||||
Event::KeyboardInput(state, _, Some(code))
|
||||
if code == VirtualKeyCode::RControl || code == VirtualKeyCode::LControl
|
||||
=> self.imgui.set_key_ctrl(state == ElementState::Pressed),
|
||||
Event::KeyboardInput(state, scancode, code) => {
|
||||
println!("KeyCode {:?} scancode {} pressed? {}", code, scancode,
|
||||
state == ElementState::Pressed);
|
||||
self.imgui.set_key(scancode, state == ElementState::Pressed);
|
||||
// TODO: This hack to do Ctrl should not be needed!
|
||||
if scancode == 29 {
|
||||
println!("Bad! Hacking in ctrl key press");
|
||||
self.imgui.set_key_ctrl(state == ElementState::Pressed);
|
||||
let pressed = state == ElementState::Pressed;
|
||||
// TODO: glutin is missing some virtual key codes on windows 10 for Ctrl and
|
||||
// Shift
|
||||
match code {
|
||||
Some(VirtualKeyCode::Tab) => self.imgui.set_key(0, pressed),
|
||||
Some(VirtualKeyCode::Left) => self.imgui.set_key(1, pressed),
|
||||
Some(VirtualKeyCode::Right) => self.imgui.set_key(2, pressed),
|
||||
Some(VirtualKeyCode::Up) => self.imgui.set_key(3, pressed),
|
||||
Some(VirtualKeyCode::Down) => self.imgui.set_key(4, pressed),
|
||||
Some(VirtualKeyCode::PageUp) => self.imgui.set_key(5, pressed),
|
||||
Some(VirtualKeyCode::PageDown) => self.imgui.set_key(6, pressed),
|
||||
Some(VirtualKeyCode::Home) => self.imgui.set_key(7, pressed),
|
||||
Some(VirtualKeyCode::End) => self.imgui.set_key(8, pressed),
|
||||
Some(VirtualKeyCode::Delete) => self.imgui.set_key(9, pressed),
|
||||
Some(VirtualKeyCode::Back) => self.imgui.set_key(10, pressed),
|
||||
Some(VirtualKeyCode::Return) => self.imgui.set_key(11, pressed),
|
||||
Some(VirtualKeyCode::Escape) => self.imgui.set_key(12, pressed),
|
||||
Some(VirtualKeyCode::A) => self.imgui.set_key(13, pressed),
|
||||
Some(VirtualKeyCode::C) => self.imgui.set_key(14, pressed),
|
||||
Some(VirtualKeyCode::V) => self.imgui.set_key(15, pressed),
|
||||
Some(VirtualKeyCode::X) => self.imgui.set_key(16, pressed),
|
||||
Some(VirtualKeyCode::Y) => self.imgui.set_key(17, pressed),
|
||||
Some(VirtualKeyCode::Z) => self.imgui.set_key(18, pressed),
|
||||
Some(VirtualKeyCode::LControl) | Some(VirtualKeyCode::RControl) =>
|
||||
self.imgui.set_key_ctrl(pressed),
|
||||
Some(VirtualKeyCode::LShift) | Some(VirtualKeyCode::RShift) =>
|
||||
self.imgui.set_key_shift(pressed),
|
||||
Some(VirtualKeyCode::LAlt) | Some(VirtualKeyCode::RAlt) =>
|
||||
self.imgui.set_key_alt(pressed),
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
Event::MouseMoved(pos) => self.mouse_pos = pos,
|
||||
@ -110,7 +130,8 @@ impl Support {
|
||||
self.mouse_pressed.1 = state == ElementState::Pressed,
|
||||
Event::MouseInput(state, MouseButton::Middle) =>
|
||||
self.mouse_pressed.2 = state == ElementState::Pressed,
|
||||
Event::MouseWheel(MouseScrollDelta::LineDelta(_, y)) => self.mouse_wheel = y,
|
||||
Event::MouseWheel(MouseScrollDelta::LineDelta(_, y)) => self.mouse_wheel = y * 5.0,
|
||||
Event::MouseWheel(MouseScrollDelta::PixelDelta(_, y)) => self.mouse_wheel = y,
|
||||
Event::ReceivedCharacter(c) => {
|
||||
println!("Got character {}", c);
|
||||
self.imgui.add_input_character(c);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user