diff --git a/src/features/anti_afk.rs b/src/features/anti_afk.rs index a957eb3..1f08b3a 100644 --- a/src/features/anti_afk.rs +++ b/src/features/anti_afk.rs @@ -1,10 +1,12 @@ +use crate::util::{self, consts::GTA_WINDOW_TITLE}; use std::time::{Duration, Instant}; use windows::Win32::UI::Input::KeyboardAndMouse::{ KEYBD_EVENT_FLAGS, MAP_VIRTUAL_KEY_TYPE, MapVirtualKeyW, keybd_event, }; pub const INTERVAL: Duration = Duration::from_secs(60); -const VK_SHIFT: u8 = 16; +const VK_NUMPAD4: u8 = 0x64; +const VK_NUMPAD6: u8 = 0x66; #[derive(Debug)] pub struct AntiAfk { @@ -21,6 +23,19 @@ impl Default for AntiAfk { } } +impl AntiAfk { + pub fn activate(&mut self) { + if util::is_window_focused(GTA_WINDOW_TITLE) + && !util::is_key_pressed(VK_NUMPAD4 as i32) + && !util::is_key_pressed(VK_NUMPAD6 as i32) + { + send(VK_NUMPAD4); + send(VK_NUMPAD6); + } + self.interval = Instant::now(); + } +} + pub fn send(vk_code: u8) { unsafe { keybd_event( @@ -37,7 +52,3 @@ pub fn send(vk_code: u8) { ); } } - -pub fn activate() { - send(VK_SHIFT); -} diff --git a/src/gui.rs b/src/gui.rs index 8f1f54c..53b773a 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -7,7 +7,8 @@ use crate::{ launch::{Launch, Platform}, }, util::{ - consts::{ENHANCED, LEGACY}, + self, + consts::{ENHANCED, GTA_WINDOW_TITLE, LEGACY}, elevation, }, }; @@ -235,12 +236,23 @@ impl App { ui.label(&self.empty_session.countdown.i_string); }); }); - ui.checkbox(&mut self.anti_afk.enabled, "Anti AFK") - .on_hover_text("You should be tabbed in\nfor this to work."); + ui.horizontal(|ui| { + ui.checkbox(&mut self.anti_afk.enabled, "Anti AFK") + .on_hover_text("You should be tabbed in\nfor this to work."); + if self.anti_afk.enabled { + ui.add_space(8.0); + ui.add_enabled_ui(false, |ui| { + ui.label(if util::is_window_focused(GTA_WINDOW_TITLE) { + "GTA is focused." + } else { + "GTA is not focused!" + }) + }); + } + }); if self.anti_afk.enabled && self.anti_afk.interval.elapsed() >= features::anti_afk::INTERVAL { - features::anti_afk::activate(); - self.anti_afk.interval = Instant::now(); + self.anti_afk.activate(); } } @@ -297,6 +309,12 @@ impl App { } fn show_debug(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui) { + ui.collapsing("times", |ui| { + ui.label(format!( + "anti afk timer: {}", + self.anti_afk.interval.elapsed().as_secs() + )) + }); ui.collapsing("sysinfo", |ui| { if ui.button("refresh all").clicked() { self.sysinfo.refresh_all(); diff --git a/src/util/consts.rs b/src/util/consts.rs index 327faf8..f221d1b 100644 --- a/src/util/consts.rs +++ b/src/util/consts.rs @@ -1,2 +1,4 @@ pub const ENHANCED: &str = "GTA5_Enhanced.exe"; pub const LEGACY: &str = "GTA5.exe"; + +pub const GTA_WINDOW_TITLE: &str = "Grand Theft Auto V"; diff --git a/src/util/mod.rs b/src/util/mod.rs index 69cbec7..8adc470 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,22 @@ pub mod consts; pub mod countdown; pub mod elevation; + +use windows::Win32::UI::{ + Input::KeyboardAndMouse::GetAsyncKeyState, + WindowsAndMessaging::{GetForegroundWindow, GetWindowTextW}, +}; + +pub fn is_window_focused(target_title: &str) -> bool { + unsafe { + let hwnd = GetForegroundWindow(); + let mut buffer: [u16; 512] = [0; 512]; + let length = GetWindowTextW(hwnd, &mut buffer); + let current_title = String::from_utf16_lossy(&buffer[..length as usize]); + current_title == target_title + } +} + +pub fn is_key_pressed(key: i32) -> bool { + unsafe { (GetAsyncKeyState(key) as i32 & 0x8000) != 0 } +}