Crate clipboard_win

Source
Expand description

This crate provide simple means to operate with Windows clipboard.

§Note keeping Clipboard around:

In Windows Clipboard opens globally and only one application can set data onto format at the time.

Therefore as soon as operations are finished, user is advised to close Clipboard.

§Features

  • std - Enables usage of std, including std::error::Error trait.
  • monitor - Enables code related to clipboard monitoring.

§Clipboard

All read and write access to Windows clipboard requires user to open it.

§Usage

§Getter

Library provides various extractors from clipboard to particular format using Getter:

  • RawData - Reads raw bytes from specified format.
  • Unicode - Reads unicode string from clipboard.
  • Bitmap - Reads RGB data of image on clipboard.
  • FileList - Reads list of files from clipboard.

Depending on format, getter can extract data into various data types.

§Setter

Library provides various setters onto clipboard by using Setter:

  • RawData - Writes raw bytes onto specified format.
  • Unicode - Writes unicode string onto clipboard.
  • Bitmap - Writes RGB data of image on clipboard.

Default setters are generic over type allowing anything that can be referenced as byte slice or str

§Manually lock clipboard

use clipboard_win::{Clipboard, formats, Getter, Setter};

const SAMPLE: &str = "MY loli sample ^^";

let _clip = Clipboard::new_attempts(10).expect("Open clipboard");
formats::Unicode.write_clipboard(&SAMPLE).expect("Write sample");

let mut output = String::new();

assert_eq!(formats::Unicode.read_clipboard(&mut output).expect("Read sample"), SAMPLE.len());
assert_eq!(output, SAMPLE);

//Efficiently re-use buffer ;)
output.clear();
assert_eq!(formats::Unicode.read_clipboard(&mut output).expect("Read sample"), SAMPLE.len());
assert_eq!(output, SAMPLE);

//Or take the same string twice?
assert_eq!(formats::Unicode.read_clipboard(&mut output).expect("Read sample"), SAMPLE.len());
assert_eq!(format!("{0}{0}", SAMPLE), output);

§Simplified API

use clipboard_win::{formats, get_clipboard, set_clipboard};

let text = "my sample ><";

set_clipboard(formats::Unicode, text).expect("To set clipboard");
//Type is necessary as string can be stored in various storages
let result: String = get_clipboard(formats::Unicode).expect("To set clipboard");
assert_eq!(result, text)

Re-exports§

pub use formats::Format;
pub use monitor::Monitor;
pub use raw::get_owner;
pub use raw::empty;
pub use raw::seq_num;
pub use raw::size;
pub use raw::is_format_avail;
pub use raw::register_format;
pub use raw::count_formats;
pub use raw::EnumFormats;
pub use formats::Unicode;

Modules§

formats
Standard clipboard formats.
monitor
Clipboard monitoring utility
options
Configuration options
raw
Raw bindings to Windows clipboard.
types
WINAPI related types

Structs§

Clipboard
Clipboard instance, which allows to perform clipboard ops.
ErrorCode
Describes error code of particular category.

Traits§

Getter
Describes format getter, specifying data type as type param
Setter
Describes format setter, specifying data type as type param

Functions§

get
Retrieve data from clipboard.
get_clipboard
Shortcut to retrieve data from clipboard.
get_clipboard_string
Shortcut to retrieve string from clipboard.
set
Set data onto clipboard.
set_clipboard
Shortcut to set data onto clipboard.
set_clipboard_string
Shortcut to set string onto clipboard.
with_clipboard
Runs provided callable with open clipboard, returning whether clipboard was open successfully.
with_clipboard_attempts
Runs provided callable with open clipboard, returning whether clipboard was open successfully.

Type Aliases§

SysResult
Alias to result used by this crate