Make a rust input reader thing

This commit is contained in:
CenTdemeern1 2025-04-29 20:53:34 +02:00
parent e78cfa0a31
commit b1b2b6d803
10 changed files with 604 additions and 19 deletions

View file

@ -0,0 +1,11 @@
[package]
name = "global-input-reading"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
[dependencies]
device_query = "3.0.1"
godot = "0.2.4"

View file

@ -0,0 +1,144 @@
use device_query::{DeviceQuery, DeviceState, Keycode};
use godot::{global::Key, prelude::*};
struct MyExtension;
#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {}
#[derive(GodotClass)]
#[class(init, base = Node)]
pub struct BackgroundInputCapture {
device_state: DeviceState,
base: Base<Node>,
}
#[godot_api]
impl BackgroundInputCapture {
#[func]
pub fn get_keys(&mut self) -> Vec<i32> {
self.device_state
.get_keys()
.into_iter()
.filter_map(|k| {
Some(
match k {
Keycode::Key0 => Key::KEY_0,
Keycode::Key1 => Key::KEY_1,
Keycode::Key2 => Key::KEY_2,
Keycode::Key3 => Key::KEY_3,
Keycode::Key4 => Key::KEY_4,
Keycode::Key5 => Key::KEY_5,
Keycode::Key6 => Key::KEY_6,
Keycode::Key7 => Key::KEY_7,
Keycode::Key8 => Key::KEY_8,
Keycode::Key9 => Key::KEY_9,
Keycode::A => Key::A,
Keycode::B => Key::B,
Keycode::C => Key::C,
Keycode::D => Key::D,
Keycode::E => Key::E,
Keycode::F => Key::F,
Keycode::G => Key::G,
Keycode::H => Key::H,
Keycode::I => Key::I,
Keycode::J => Key::J,
Keycode::K => Key::K,
Keycode::L => Key::L,
Keycode::M => Key::M,
Keycode::N => Key::N,
Keycode::O => Key::O,
Keycode::P => Key::P,
Keycode::Q => Key::Q,
Keycode::R => Key::R,
Keycode::S => Key::S,
Keycode::T => Key::T,
Keycode::U => Key::U,
Keycode::V => Key::V,
Keycode::W => Key::W,
Keycode::X => Key::X,
Keycode::Y => Key::Y,
Keycode::Z => Key::Z,
Keycode::F1 => Key::F1,
Keycode::F2 => Key::F2,
Keycode::F3 => Key::F3,
Keycode::F4 => Key::F4,
Keycode::F5 => Key::F5,
Keycode::F6 => Key::F6,
Keycode::F7 => Key::F7,
Keycode::F8 => Key::F8,
Keycode::F9 => Key::F9,
Keycode::F10 => Key::F10,
Keycode::F11 => Key::F11,
Keycode::F12 => Key::F12,
Keycode::F13 => Key::F13,
Keycode::F14 => Key::F14,
Keycode::F15 => Key::F15,
Keycode::F16 => Key::F16,
Keycode::F17 => Key::F17,
Keycode::F18 => Key::F18,
Keycode::F19 => Key::F19,
Keycode::F20 => Key::F20,
Keycode::Escape => Key::ESCAPE,
Keycode::Space => Key::SPACE,
Keycode::LControl => return None,
Keycode::RControl => return None,
Keycode::LShift => return None,
Keycode::RShift => return None,
Keycode::LAlt => return None,
Keycode::RAlt => return None,
Keycode::Command => return None,
Keycode::RCommand => return None,
Keycode::LOption => return None,
Keycode::ROption => return None,
Keycode::LMeta => return None,
Keycode::RMeta => return None,
Keycode::Enter => Key::ENTER,
Keycode::Up => Key::UP,
Keycode::Down => Key::DOWN,
Keycode::Left => Key::LEFT,
Keycode::Right => Key::RIGHT,
Keycode::Backspace => Key::BACKSPACE,
Keycode::CapsLock => Key::CAPSLOCK,
Keycode::Tab => Key::TAB,
Keycode::Home => Key::HOME,
Keycode::End => Key::END,
Keycode::PageUp => Key::PAGEUP,
Keycode::PageDown => Key::PAGEDOWN,
Keycode::Insert => Key::INSERT,
Keycode::Delete => Key::DELETE,
Keycode::Numpad0 => Key::KP_0,
Keycode::Numpad1 => Key::KP_1,
Keycode::Numpad2 => Key::KP_2,
Keycode::Numpad3 => Key::KP_3,
Keycode::Numpad4 => Key::KP_4,
Keycode::Numpad5 => Key::KP_5,
Keycode::Numpad6 => Key::KP_6,
Keycode::Numpad7 => Key::KP_7,
Keycode::Numpad8 => Key::KP_8,
Keycode::Numpad9 => Key::KP_9,
Keycode::NumpadSubtract => Key::KP_SUBTRACT,
Keycode::NumpadAdd => Key::KP_ADD,
Keycode::NumpadDivide => Key::KP_DIVIDE,
Keycode::NumpadMultiply => Key::KP_MULTIPLY,
Keycode::NumpadEquals => Key::EQUAL,
Keycode::NumpadEnter => Key::KP_ENTER,
Keycode::NumpadDecimal => Key::KP_PERIOD,
Keycode::Grave => Key::QUOTELEFT,
Keycode::Minus => Key::MINUS,
Keycode::Equal => Key::EQUAL,
Keycode::LeftBracket => Key::BRACKETLEFT,
Keycode::RightBracket => Key::BRACKETRIGHT,
Keycode::BackSlash => Key::BACKSLASH,
Keycode::Semicolon => Key::SEMICOLON,
Keycode::Apostrophe => Key::APOSTROPHE,
Keycode::Comma => Key::COMMA,
Keycode::Dot => Key::PERIOD,
Keycode::Slash => Key::SLASH,
}
.to_godot(),
)
})
.collect()
}
}