Skip to content

Commit

Permalink
Apply virtual key map to IO
Browse files Browse the repository at this point in the history
  • Loading branch information
veeenu committed Jan 28, 2024
1 parent 1dd0222 commit 7791c49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
44 changes: 1 addition & 43 deletions src/hooks/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ffi::c_void;
use std::mem::size_of;

use imgui::{Io, Key};
use imgui::Io;
use parking_lot::MutexGuard;
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
use windows::Win32::UI::Input::KeyboardAndMouse::*;
Expand All @@ -26,48 +26,6 @@ fn hiword(l: u32) -> u16 {
((l >> 16) & 0xffff) as u16
}

/// Implement this if you are building a custom renderer.
///
/// Check out first party implementations ([`crate::hooks::dx9`],
/// [`crate::hooks::dx11`], [`crate::hooks::dx12`], [`crate::hooks::opengl3`])
/// for guidance on how to implement the methods.
pub trait ImguiWindowsEventHandler {
fn io(&self) -> &imgui::Io;
fn io_mut(&mut self) -> &mut imgui::Io;

fn wnd_proc(&self) -> WndProcType;

fn setup_io(&mut self) {
let io = ImguiWindowsEventHandler::io_mut(self);

io.nav_active = true;
io.nav_visible = true;

// Initialize keys
io[Key::Tab] = VK_TAB.0 as _;
io[Key::LeftArrow] = VK_LEFT.0 as _;
io[Key::RightArrow] = VK_RIGHT.0 as _;
io[Key::UpArrow] = VK_UP.0 as _;
io[Key::DownArrow] = VK_DOWN.0 as _;
io[Key::PageUp] = VK_PRIOR.0 as _;
io[Key::PageDown] = VK_NEXT.0 as _;
io[Key::Home] = VK_HOME.0 as _;
io[Key::End] = VK_END.0 as _;
io[Key::Insert] = VK_INSERT.0 as _;
io[Key::Delete] = VK_DELETE.0 as _;
io[Key::Backspace] = VK_BACK.0 as _;
io[Key::Space] = VK_SPACE.0 as _;
io[Key::Enter] = VK_RETURN.0 as _;
io[Key::Escape] = VK_ESCAPE.0 as _;
io[Key::A] = VK_A.0 as _;
io[Key::C] = VK_C.0 as _;
io[Key::V] = VK_V.0 as _;
io[Key::X] = VK_X.0 as _;
io[Key::Y] = VK_Y.0 as _;
io[Key::Z] = VK_Z.0 as _;
}
}

////////////////////////////////////////////////////////////////////////////////
// Raw input
////////////////////////////////////////////////////////////////////////////////
Expand Down
32 changes: 31 additions & 1 deletion src/renderer/dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::atomic::{AtomicU64, Ordering};

pub use imgui;
use imgui::internal::RawWrapper;
use imgui::{BackendFlags, Context, DrawCmd, DrawData, DrawIdx, DrawVert, TextureId, Ui};
use imgui::{BackendFlags, Context, DrawCmd, DrawData, DrawIdx, DrawVert, Key, TextureId, Ui};
use memoffset::offset_of;
use tracing::{error, trace};
use windows::core::{s, w, ComInterface, Result, PCSTR, PCWSTR};
Expand All @@ -30,6 +30,10 @@ use windows::Win32::System::Threading::{
CreateEventA, CreateEventExW, WaitForSingleObject, WaitForSingleObjectEx, CREATE_EVENT,
INFINITE,
};
use windows::Win32::UI::Input::KeyboardAndMouse::{
VK_A, VK_BACK, VK_C, VK_DELETE, VK_DOWN, VK_END, VK_ESCAPE, VK_HOME, VK_INSERT, VK_LEFT,
VK_NEXT, VK_PRIOR, VK_RETURN, VK_RIGHT, VK_SPACE, VK_TAB, VK_UP, VK_V, VK_X, VK_Y, VK_Z,
};
use windows::Win32::UI::WindowsAndMessaging::{GetCursorPos, GetForegroundWindow, IsChild};

use crate::util::{try_out_param, try_out_ptr};
Expand Down Expand Up @@ -585,6 +589,32 @@ impl RenderEngine {
}
}

io.nav_active = true;
io.nav_visible = true;

// Map key indices to the virtual key codes
io[Key::Tab] = VK_TAB.0 as _;
io[Key::LeftArrow] = VK_LEFT.0 as _;
io[Key::RightArrow] = VK_RIGHT.0 as _;
io[Key::UpArrow] = VK_UP.0 as _;
io[Key::DownArrow] = VK_DOWN.0 as _;
io[Key::PageUp] = VK_PRIOR.0 as _;
io[Key::PageDown] = VK_NEXT.0 as _;
io[Key::Home] = VK_HOME.0 as _;
io[Key::End] = VK_END.0 as _;
io[Key::Insert] = VK_INSERT.0 as _;
io[Key::Delete] = VK_DELETE.0 as _;
io[Key::Backspace] = VK_BACK.0 as _;
io[Key::Space] = VK_SPACE.0 as _;
io[Key::Enter] = VK_RETURN.0 as _;
io[Key::Escape] = VK_ESCAPE.0 as _;
io[Key::A] = VK_A.0 as _;
io[Key::C] = VK_C.0 as _;
io[Key::V] = VK_V.0 as _;
io[Key::X] = VK_X.0 as _;
io[Key::Y] = VK_Y.0 as _;
io[Key::Z] = VK_Z.0 as _;

Ok(())
}

Expand Down

0 comments on commit 7791c49

Please sign in to comment.