nezha/src/joystick/joystick.rs

58 lines
1.6 KiB
Rust

#[cfg(feature = "joystick")]
pub(crate) mod joystick{
use std::thread;
use std::time::Duration;
use enigo::{Enigo, Key, KeyboardControllable};
use rand::Rng;
pub enum MoveMode{
// 横向
HORIZONTAL,
// 纵向
PORTRAIT,
// 随机
RANDOM,
}
impl MoveMode {
pub fn to_key_list(&self) -> Vec<Key>{
return match self {
MoveMode::HORIZONTAL => vec![Key::Layout('j'),Key::Layout('l')],
MoveMode::PORTRAIT => vec![Key::UpArrow,Key::DownArrow],
MoveMode:: RANDOM => vec![Key::LeftArrow,Key::RightArrow,Key::UpArrow,Key::DownArrow],
}
}
}
pub(crate) fn move_once_by_mode(road : &MoveMode, index : usize){
let mut enigo = Enigo::new();
let moves = road.to_key_list();
let move_index = index % moves.len();
let move_key = moves[move_index];
long_press(enigo,move_key,1000,500);
}
pub(crate) fn once_press(mut enigo:Enigo, key: Key){
enigo.key_click(key);
}
pub(crate) fn quick_press(key: Key){
let mut enigo = Enigo::new();
let mut rng = rand::thread_rng();
enigo.key_down(key);
thread::sleep(Duration::from_millis(rng.gen_range(0 .. 20)));
enigo.key_up(key);
}
pub(crate) fn long_press(mut enigo:Enigo, key: Key, est_time: isize, random_time: isize){
let mut rng = rand::thread_rng();
enigo.key_down(key);
let act = est_time + rng.gen_range((-random_time .. random_time));
thread::sleep(Duration::from_millis(act as u64));
enigo.key_up(key);
}
}