this repo has no description
at main 1.7 kB view raw
1use std::sync::{Arc, Mutex}; 2 3use enigo::{Direction, Enigo, Key, Keyboard}; 4 5use crate::{ 6 runtime::nodes::RuntimeNode, 7 structs::{nodes::Node, parameter_types::ParameterType}, 8}; 9 10pub struct PressKey { 11 outputs: Vec<Vec<(String, isize, isize)>>, 12 inputs: Vec<Option<(String, isize, isize)>>, 13 14 key: Option<char>, 15 enigo: Arc<Mutex<Enigo>>, 16} 17 18impl PressKey { 19 pub fn new(node: Node, enigo: Arc<Mutex<Enigo>>) -> Box<Self> { 20 let value = &node.statics[0].value; 21 22 Box::new(Self { 23 outputs: node.outputs.iter().map(|x| { 24 x.connections.iter() 25 .map(|x| (x.node.clone(), x.index, x.value_type)).collect()}).collect(), 26 27 inputs: node.inputs.iter().map(|x| { 28 let y = x.connections.get(0); 29 if let Some(y) = y{ 30 Some((y.node.clone(), y.index, y.value_type)) 31 } else{ 32 None 33 } 34 }).collect(), 35 36 enigo, 37 key: if value.is_null() { 38 None 39 } else { 40 let string = value.as_str().unwrap().to_owned(); 41 42 if string.len() == 1 { 43 Some(string.chars().nth(0).unwrap()) 44 } else { 45 None 46 } 47 }, 48 }) 49 } 50} 51 52impl RuntimeNode for PressKey { 53 fn outputs(&self) -> Vec<Vec<(String, isize, isize)>> { 54 self.outputs.clone() 55 } 56 57 fn inputs(&self) -> Vec<Option<(String, isize, isize)>> { 58 self.inputs.clone() 59 } 60 61 fn execute(&mut self, _: Vec<ParameterType>) -> Vec<ParameterType> { 62 if self.key.is_some() { 63 let mut enigo = self.enigo.lock().unwrap(); 64 enigo.key(Key::MediaPlayPause, Direction::Click).unwrap(); 65 } 66 67 vec![] 68 } 69 70 fn is_entrypoint(&self) -> bool { 71 false 72 } 73}