pub enum Command {
Show 18 variants
Delay(u32),
KeyDown(Key),
KeyUp(Key),
KeyClick(Key),
MouseMoveRel(i32, i32),
MouseMoveAbs(i32, i32),
MouseScroll(i32, i32),
MouseDown(MouseButton),
MouseUp(MouseButton),
MouseClick(MouseButton),
AsciiCharDown(u8),
AsciiCharUp(u8),
AsciiChar(u8),
AsciiString(Vec<u8>),
UnicodeCharDown(char),
UnicodeCharUp(char),
UnicodeChar(char),
UnicodeString(String),
}
Expand description
Variants§
Delay(u32)
Creates a delay for a number of milliseconds.
KeyDown(Key)
Corresponds to key_down
.
KeyUp(Key)
Corresponds to key_up
.
KeyClick(Key)
Corresponds to key_click
.
MouseMoveRel(i32, i32)
Corresponds to mouse_move_rel
.
MouseMoveAbs(i32, i32)
Corresponds to mouse_move_abs
.
MouseScroll(i32, i32)
Corresponds to mouse_scroll
.
MouseDown(MouseButton)
Corresponds to mouse_down
.
MouseUp(MouseButton)
Corresponds to mouse_up
.
MouseClick(MouseButton)
Corresponds to mouse_click
.
AsciiCharDown(u8)
Corresponds to ascii_char_down
.
AsciiCharUp(u8)
Corresponds to ascii_char_up
.
AsciiChar(u8)
Corresponds to ascii_char
.
AsciiString(Vec<u8>)
Corresponds to ascii_string
.
UnicodeCharDown(char)
Corresponds to unicode_char_down
.
UnicodeCharUp(char)
Corresponds to unicode_char_up
.
UnicodeChar(char)
Corresponds to unicode_char
.
UnicodeString(String)
Corresponds to unicode_string
.
Implementations§
source§impl Command
impl Command
sourcepub fn execute<C>(
&self,
ctx: &mut C
) -> Result<(), GenericError<C::PlatformError>>where
C: FallibleContext + KeyboardContext + MouseContext + AsciiKeyboardContext + UnicodeKeyboardContext,
pub fn execute<C>(
&self,
ctx: &mut C
) -> Result<(), GenericError<C::PlatformError>>where
C: FallibleContext + KeyboardContext + MouseContext + AsciiKeyboardContext + UnicodeKeyboardContext,
Execute a Command
by calling the corresponding method on
one of the traits
.
Delay
is treated as being synchronous and is
executed using std::thread::sleep
.
source§impl Command
impl Command
sourcepub fn from_bytes(buf: &[u8]) -> Result<(Command, usize), CommandBytesError>
pub fn from_bytes(buf: &[u8]) -> Result<(Command, usize), CommandBytesError>
Construct a Command
from a sequence of bytes.
The first byte in the buffer must be a CommandCode
. This identifies
the command and its arguments. Following the command identifier is a
sequence of bytes that encode the arguments of the command. Key
and
MouseButton
are single bytes. For integer arguments (used for moving
the mouse and scrolling), signed 16-bit big-endian integers are used.
Since a negative delay is impossible, the millisecond parameter for the
Delay
command is unsigned.
An ASCII character is a single byte. An ASCII string is a length followed by a sequence of bytes. The length is an unsigned 16-bit big-endian integer.
A Unicode character (or more specifically, a Unicode scalar value) is 32-bits. A Unicode string is similar to an ASCII string in that it has a length followed by a sequence of bytes, however the sequence of bytes are a UTF-8 encoded string.
The function returns the command and the number of bytes that were read from the buffer.
Examples
use tfc::{Command, CommandCode, Key};
let bytes = &[
CommandCode::MouseMoveRel as u8, 255, 214, 0, 64,
CommandCode::KeyClick as u8, Key::K as u8,
CommandCode::UnicodeString as u8, 0, 4, 0xF0, 0x9F, 0xA4, 0xAA,
];
let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 5);
assert_eq!(command, Command::MouseMoveRel(-42, 64));
let bytes = &bytes[len..];
let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 2);
assert_eq!(command, Command::KeyClick(Key::K));
let bytes = &bytes[len..];
let (command, len) = Command::from_bytes(bytes).unwrap();
assert_eq!(len, 7);
assert_eq!(command, Command::UnicodeString("🤪".to_owned()));
source§impl Command
impl Command
sourcepub fn to_bytes(&self, buf: &mut [u8]) -> Result<usize, usize>
pub fn to_bytes(&self, buf: &mut [u8]) -> Result<usize, usize>
Fill a byte array with the command.
See from_bytes
for a description of the byte
format. On success, this will return Ok
with the number of bytes
written. If the given slice is too small, this will return Err
with
the number of bytes necessary.
Examples
use tfc::{Command, CommandCode, Key};
// Write some commands to a byte array
let commands = [
Command::MouseMoveRel(-42, 64),
Command::KeyClick(Key::K),
Command::UnicodeString("🤪".to_owned()),
];
let total_size = commands.iter().map(|c| c.bytes_len()).sum();
let mut byte_vec = vec![0; total_size];
let mut byte_slice = byte_vec.as_mut_slice();
for command in commands.iter() {
let size = command.to_bytes(byte_slice).unwrap();
byte_slice = &mut byte_slice[size..];
}
assert_eq!(byte_slice.len(), 0);
// Read the commands back from the byte array and make sure they match.
let mut byte_slice = byte_vec.as_slice();
let mut command_index = 0;
while !byte_slice.is_empty() {
let (command, size) = Command::from_bytes(byte_slice).unwrap();
assert_eq!(command, commands[command_index]);
byte_slice = &byte_slice[size..];
command_index += 1;
}