simulate 0.3.0

This crate allows you to simulate keystrokes.
Documentation
use super::*;

/// The event buffer is a simple wrapper around a `Vec<simulate::Event>`.
#[derive(Clone, Default)]
pub struct EventBuffer(pub Vec<Event>);

impl From<Vec<Event>> for EventBuffer {
    #[inline(always)]
    fn from(v: Vec<Event>) -> Self {
        Self(v)
    }
}

impl From<EventBuffer> for Vec<Event> {
    #[inline(always)]
    fn from(b: EventBuffer) -> Self {
        b.0
    }
}

impl IntoIterator for EventBuffer {
    type Item = Event;
    type IntoIter = std::vec::IntoIter<Event>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl AsRef<Vec<Event>> for EventBuffer {
    fn as_ref(&self) -> &Vec<Event> {
        &self.0
    }
}

impl AsMut<Vec<Event>> for EventBuffer {
    fn as_mut(&mut self) -> &mut Vec<Event> {
        &mut self.0
    }
}

impl EventBuffer {
    /// Creates a new empty `EventBuffer`. (see `Vec::new`)
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new `EventBuffer` with the given capacity. (see `Vec::with_capacity`)
    #[inline(always)]
    pub fn with_capacity(cap: usize) -> Self {
        Self(Vec::with_capacity(cap))
    }

    /// Pushes a new event into the buffer. (see `Vec::push`)
    #[inline(always)]
    pub fn add_event(&mut self, event: Event) {
        self.0.push(event)
    }

    /// Inserts multiple events into the buffer. (see `Vec::extend`)
    #[inline(always)]
    pub fn extend(&mut self, events: impl IntoIterator<Item = Event>) {
        self.0.extend(events);
    }

    /// Extends the buffer from a slice of events. (see `Vec::extend_from_slice`)
    #[inline(always)]
    pub fn extend_from_slice(&mut self, events: &[Event]) {
        self.0.extend_from_slice(events);
    }

    /// Appends the elements of another buffer to this one, living the other
    /// one empty. (see `Vec::append`)
    #[inline(always)]
    pub fn append(&mut self, events: &mut EventBuffer) {
        self.0.append(&mut events.0)
    }

    /// Adds `Press` and `Release` events for multiple keylikes into the buffer.
    #[inline]
    pub fn add_multiple<K: Keylike>(&mut self, keys: impl IntoIterator<Item = K>) {
        self.extend(keys.into_iter().flat_map(Keylike::press_release));
    }

    /// Adds a `Press` and a `Release` event for the given keylike.
    #[inline(always)]
    pub fn send(&mut self, key: impl Keylike) {
        self.extend(key.press_release());
    }

    /// Adds a `Press` event for the given keylike.
    #[inline(always)]
    pub fn press(&mut self, key: impl Keylike) {
        self.add_event(key.press())
    }

    /// Adds a `Release` event for the given keylike.
    #[inline(always)]
    pub fn release(&mut self, key: impl Keylike) {
        self.add_event(key.release())
    }

    /// Adds `Press` and `Release` events for each character of the string.
    #[inline(always)]
    pub fn type_str(&mut self, s: &str) {
        self.add_multiple(s.chars())
    }

    /// Adds an event that causes the mouse to move `dx` pixels to the right
    /// and `dy` pixels down.
    #[inline(always)]
    pub fn move_mouse_relative(&mut self, dx: i32, dy: i32) {
        self.add_event(Event::MoveMouseRelative { dx, dy });
    }

    /// Adds an event that causes the mouse to move, coordinates are normalized to the
    /// desktop: (0, 0) map to the top-left of the main monitor and (1, 1) maps to the
    /// bottom-right of the main monitor.
    #[inline(always)]
    pub fn move_mouse_absolute(&mut self, x: f32, y: f32) {
        self.add_event(Event::MoveMouseAbsolute {
            x,
            y,
            map_to_virtual_desktop: false,
        });
    }

    /// Adds a scroll event to the buffer.
    #[inline(always)]
    pub fn add_scroll_event(&mut self, delta: f32, direction: ScrollDirection) {
        self.add_event(Event::Scroll { delta, direction });
    }

    /// Uses the mouse wheel vertically.
    #[inline(always)]
    pub fn scroll(&mut self, delta: f32) {
        self.add_scroll_event(delta, ScrollDirection::Vertical);
    }

    /// Uses the mouse wheel horizontally.
    #[inline(always)]
    pub fn scroll_horizontal(&mut self, delta: f32) {
        self.add_scroll_event(delta, ScrollDirection::Horizontal);
    }
    
    /// Simulates the events using the default simulator.
    #[inline(always)]
    pub fn simulate(self) -> Result<()> {
        SIM.send_events(self)
    }
}