Skip to content

Commit 3d05219

Browse files
committed
скрытие консоли по бинду
1 parent bd5a6df commit 3d05219

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "voxelproxy"
3-
version = "3.0.4"
3+
version = "3.1.0"
44
edition = "2021"
55

66
[dependencies]
@@ -16,8 +16,11 @@ minecraft_protocol = { git = "https://github.com/kauri-off/minecraft_protocol.gi
1616
windows = { version = "0.61.3", features = [
1717
"Win32_Networking_WinSock",
1818
"Win32_NetworkManagement_IpHelper",
19-
"Win32_NetworkManagement_Ndis"
19+
"Win32_NetworkManagement_Ndis",
20+
"Win32_UI_Input_KeyboardAndMouse",
21+
"Win32_UI_WindowsAndMessaging",
22+
"Win32_System_Console"
2023
] }
2124

22-
[target.'cfg(linux)'.dependencies]
25+
[target.'cfg(not(windows))'.dependencies]
2326
get_if_addrs = "0.5.3"

build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
let target = std::env::var("TARGET").unwrap_or_default();
3+
4+
// Только для Windows + MSVC
5+
if target.contains("windows-msvc") {
6+
println!("cargo:rustc-link-arg=/ENTRY:mainCRTStartup");
7+
println!("cargo:rustc-link-arg=/SUBSYSTEM:WINDOWS");
8+
}
9+
10+
// Если хочешь также поддерживать gnu:
11+
// else if target.contains("windows-gnu") {
12+
// println!("cargo:rustc-link-arg=-Wl,--subsystem,windows");
13+
// }
14+
}

src/keybind.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::ptr::null_mut;
2+
use std::sync::mpsc::Sender;
3+
use windows::Win32::System::Console::GetConsoleWindow;
4+
use windows::Win32::UI::Input::KeyboardAndMouse::RegisterHotKey;
5+
use windows::Win32::UI::Input::KeyboardAndMouse::MOD_ALT;
6+
use windows::Win32::UI::Input::KeyboardAndMouse::{MOD_NOREPEAT, VK_F10};
7+
use windows::Win32::UI::WindowsAndMessaging::GetMessageW;
8+
use windows::Win32::UI::WindowsAndMessaging::MSG;
9+
use windows::Win32::UI::WindowsAndMessaging::SHOW_WINDOW_CMD;
10+
use windows::Win32::UI::WindowsAndMessaging::SW_SHOW;
11+
use windows::Win32::UI::WindowsAndMessaging::WM_HOTKEY;
12+
use windows::Win32::UI::WindowsAndMessaging::{ShowWindow, SW_HIDE};
13+
14+
#[derive(PartialEq, Eq, Debug)]
15+
enum State {
16+
Opened,
17+
Closed,
18+
}
19+
20+
impl State {
21+
fn swap(&mut self) {
22+
*self = match *self {
23+
State::Opened => State::Closed,
24+
State::Closed => State::Opened,
25+
}
26+
}
27+
28+
fn cmd(&self) -> SHOW_WINDOW_CMD {
29+
match self {
30+
State::Opened => SW_HIDE,
31+
State::Closed => SW_SHOW,
32+
}
33+
}
34+
}
35+
36+
pub unsafe fn setup_keybind(tx: Sender<String>) {
37+
if let Err(e) = RegisterHotKey(None, 1, MOD_ALT | MOD_NOREPEAT, VK_F10.0 as u32) {
38+
tx.send(format!("Ошибка при установке горячей клавиши: {}", e))
39+
.unwrap();
40+
return;
41+
}
42+
43+
tx.send(format!(
44+
"Установлена горячая клавиша ALT+F10 для скрытия/показа окна консоли"
45+
))
46+
.unwrap();
47+
let mut state = State::Opened;
48+
let hwnd = GetConsoleWindow();
49+
50+
let mut msg: MSG = std::mem::zeroed();
51+
while GetMessageW(&mut msg as *mut MSG, None, 0, 0).as_bool() {
52+
if msg.message == WM_HOTKEY {
53+
if hwnd.0 != null_mut() {
54+
let _ = ShowWindow(hwnd, state.cmd());
55+
state.swap();
56+
}
57+
}
58+
}
59+
}

src/local_ip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn get_local_ip() -> Option<Ipv4Addr> {
6666
return preferred_ip;
6767
}
6868

69-
#[cfg(target_os = "linux")]
69+
#[cfg(not(target_os = "windows"))]
7070
pub fn get_local_ip() -> Option<Ipv4Addr> {
7171
use get_if_addrs::get_if_addrs;
7272
get_if_addrs()

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::{
2323
};
2424

2525
mod controller;
26+
#[cfg(target_os = "windows")]
27+
mod keybind;
2628
mod local_ip;
2729
#[allow(dead_code)]
2830
mod packets;
@@ -31,6 +33,13 @@ mod updater;
3133

3234
#[tokio::main]
3335
async fn main() {
36+
#[cfg(target_os = "windows")]
37+
unsafe {
38+
use windows::Win32::System::Console::AllocConsole;
39+
40+
AllocConsole().unwrap(); // создаёт новое окно консоли (conhost.exe)
41+
}
42+
3443
println!(
3544
r#"
3645
__ __ _ ____
@@ -75,6 +84,17 @@ __ __ _ ____
7584
}
7685
}
7786

87+
#[cfg(target_os = "windows")]
88+
{
89+
use std::sync::mpsc;
90+
91+
use crate::keybind::setup_keybind;
92+
let (tx, rx) = mpsc::channel();
93+
94+
std::thread::spawn(|| unsafe { setup_keybind(tx) });
95+
println!("{}", rx.recv().unwrap());
96+
}
97+
7898
let (remote_addr, remote_dns) = {
7999
loop {
80100
let input: String = dialoguer::Input::with_theme(&ColorfulTheme::default())

0 commit comments

Comments
 (0)