如何监听系统事件,如睡眠、锁定、唤醒、关机、休眠

问题描述 投票:0回答:1

我有一个 tauri 应用程序,我想在其中监听上述事件,是否有我可以使用的库或代码,谢谢

linux windows macos rust tauri
1个回答
1
投票

这是使用

sys_info
的幼稚做法:

use sys_info::{PowerStatus, SystemExt};

fn main() {
    let power_status = PowerStatus::new().unwrap();
    if power_status.is_sleeping() {
        println!("Sleeping");
    } else {
        println!("Not sleeping");
    }
}

仅限 Windows 版本:

use winapi::um::winbase::GetSystemPowerStatus;
use winapi::um::winnt::SYSTEM_POWER_STATUS;

fn main() {
    let mut status = SYSTEM_POWER_STATUS::default();
    unsafe { GetSystemPowerStatus(&mut status) };
    if status.BatteryFlag & 0x80 != 0 {
        println!("Sleeping");
    } else {
        println!("Not sleeping");
    }
}

还有一个使用PSP的解决方案:

use psp::monitor::PowerMonitor;

let power_monitor = PowerMonitor::new();
  let power_event_channel = power_monitor.event_receiver();
  if let Err(msg) = power_monitor.start_listening() {
    println!("Failed to start listening to power events: {}", msg);
    return;
  }

完整示例此处

© www.soinside.com 2019 - 2024. All rights reserved.