Skip to content

Commit

Permalink
batch operations, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveway committed May 19, 2024
1 parent 0223168 commit 81d13b8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mouse-keyboard-input"
version = "0.7.3"
version = "0.8.1"
authors = ["Andrew Korovkin <[email protected]>"]
edition = "2021"

Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sudo apt install libudev-dev libevdev-dev libhidapi-dev

Add to `Cargo.toml`
```
mouse-keyboard-input = "0.7.3"
mouse-keyboard-input = "0.8.1"
```
To use the latest development version:
```
Expand Down Expand Up @@ -57,9 +57,11 @@ press(button_or_key)
release(button_or_key)
move_mouse(x, y)
move_mouse_x(value)
move_mouse_y(value)
scroll_vertical(value)
scroll_horizontal(value)
scroll_x(value) - scroll horizontally
scroll_y(value) - scroll vertically
```
### List of buttons
#### Mouse
Expand All @@ -82,21 +84,19 @@ KEY_LEFTMETA (Meta means Windows button on Linux)
### Code examples
#### Mouse
```
extern crate mouse_keyboard_input;
use mouse_keyboard_input::VirtualDevice;
use mouse_keyboard_input::key_codes::*;
use std::thread;
use std::time::Duration;
fn main() {
let mut device = VirtualDevice::new();
let mut device = VirtualDevice::default().unwrap();
for _ in 1..5 {
thread::sleep(Duration::from_secs(1));
// scroll down by 100
device.scroll_vertical(-100).unwrap();
device.scroll_y(-100).unwrap();
// move cursor 50 pixels up and 50 pixels to the right from the current position
device.move_mouse(50, 50).unwrap();
//click the left mouse button
Expand All @@ -106,15 +106,13 @@ fn main() {
```
#### Keyboard
```
extern crate mouse_keyboard_input;
use mouse_keyboard_input::VirtualDevice;
use mouse_keyboard_input::key_codes::*;
use std::thread;
use std::time::Duration;
fn main() {
let mut device = VirtualDevice::new();
let mut device = VirtualDevice::default().unwrap();
thread::sleep(Duration::from_secs(2));
Expand Down
79 changes: 59 additions & 20 deletions src/virtual_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,31 @@ impl VirtualDevice {
Ok(())
}

#[inline]
fn write_batch(&mut self, batch: Vec<EventParams>) -> EmptyResult{
let mut converted = Vec::new();

for event in batch{
let mut input_event = input_event {
time: FIXED_TIME,
kind: event.0,
code: event.1,
value: event.2,
};

unsafe {
// gettimeofday(&mut input_event.time, ptr::null_mut());

let ptr = &input_event as *const _ as *const u8;
let size = mem::size_of_val(&input_event);
let content = slice::from_raw_parts(ptr, size);
converted.extend_from_slice(content);
}
}
self.file.write_all(converted.as_slice())?;
Ok(())
}

#[inline]
fn write(&mut self, kind: u16, code: u16, value: i32) -> EmptyResult {
// let content = convert_event_for_writing(kind, code, value);
Expand Down Expand Up @@ -368,28 +393,35 @@ impl VirtualDevice {

#[inline]
pub fn move_mouse_raw(&mut self, x: Coord, y: Coord) -> EmptyResult {
self.write(EV_REL, REL_X, x)?;
self.write(EV_REL, REL_Y, -y)?;
Ok(())
self.write_batch(vec![
(EV_REL, REL_X, x),
(EV_REL, REL_Y, -y),
])
}

#[inline]
pub fn move_mouse_x(&mut self, x: Coord) -> EmptyResult {
self.write(EV_REL, REL_X, x)?;
self.synchronize()
self.write_batch(vec![
(EV_REL, REL_X, x),
SYN_PARAMS
])
}

#[inline]
pub fn move_mouse_y(&mut self, y: Coord) -> EmptyResult {
self.write(EV_REL, REL_Y, -y)?;
self.synchronize()
self.write_batch(vec![
(EV_REL, REL_Y, -y),
SYN_PARAMS
])
}

#[inline]
pub fn move_mouse(&mut self, x: Coord, y: Coord) -> EmptyResult {
self.write(EV_REL, REL_X, x)?;
self.write(EV_REL, REL_Y, -y)?;
self.synchronize()
self.write_batch(vec![
(EV_REL, REL_X, x),
(EV_REL, REL_Y, -y),
SYN_PARAMS
])
}

#[inline]
Expand All @@ -404,32 +436,39 @@ impl VirtualDevice {

#[inline]
pub fn scroll_x(&mut self, value: Coord) -> EmptyResult {
self.write(EV_REL, REL_HWHEEL, value)?;
self.synchronize()
self.write_batch(vec![
(EV_REL, REL_HWHEEL, value),
SYN_PARAMS
])
}

#[inline]
pub fn scroll_y(&mut self, value: Coord) -> EmptyResult {
self.write(EV_REL, REL_WHEEL, value)?;
self.synchronize()
self.write_batch(vec![
(EV_REL, REL_WHEEL, value),
SYN_PARAMS
])
}

#[inline]
pub fn press(&mut self, button: Button) -> EmptyResult {
self.write(EV_KEY, button, 1)?;
self.synchronize()
self.write_batch(vec![
(EV_KEY, button, 1),
SYN_PARAMS
])
}

#[inline]
pub fn release(&mut self, button: Button) -> EmptyResult {
sleep(SLEEP_BEFORE_RELEASE); // required to preserve typing order

self.write(EV_KEY, button, 0)?;
self.synchronize()
self.write_batch(vec![
(EV_KEY, button, 0),
SYN_PARAMS
])
}

pub fn click(&mut self, button: Button) -> EmptyResult {
self.press(button)?;
sleep(SLEEP_BEFORE_RELEASE); // required to preserve typing order
self.release(button)
}
}
Expand Down

0 comments on commit 81d13b8

Please sign in to comment.