Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sgoudham/myclipboard
Utility Library to Access and Manipulate the Default System Clipboard
https://github.com/sgoudham/myclipboard
clipboard clipboard-history cross-platform java macos unix windows
Last synced: 3 months ago
JSON representation
Utility Library to Access and Manipulate the Default System Clipboard
- Host: GitHub
- URL: https://github.com/sgoudham/myclipboard
- Owner: sgoudham
- License: mit
- Created: 2021-07-25T19:31:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T03:45:55.000Z (about 2 years ago)
- Last Synced: 2024-10-14T23:07:42.992Z (4 months ago)
- Topics: clipboard, clipboard-history, cross-platform, java, macos, unix, windows
- Language: Java
- Homepage:
- Size: 121 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[maven-central]: https://img.shields.io/maven-central/v/me.goudham/MyClipboard.svg?label=Maven%20Central
[build-status]: https://img.shields.io/github/checks-status/sgoudham/MyClipboard/main
[codecov]: https://codecov.io/gh/sgoudham/MyClipboard/branch/main/graph/badge.svg?token=F4LKql7rIq![maven-central]
![build-status]
![codecov]# MyClipboard
Utility Library to Access and Manipulate the Default System Clipboard# About
The inspiration for this project came from my frustration of macOS not having clipboard history
built-in unlike Windows. This library will allow you to access the system clipboard and manipulate it.# Configuration
Follow the steps below for a basic implementation:
```java
public class Main {
public static void main(String[] args) {
// Retrieve an instance of MyClipboard
MyClipboard myClipboard = MyClipboard.getSystemClipboard();// Start listening for clipboard notifications
myClipboard.startListening();// Add event listeners for different types of events (Text, Image & File)
myClipboard.addEventListener((TextEvent) (oldContent, newContent) -> {
String oldText = oldContent.getText();
BufferedImage oldImage = oldContent.getImage();
List oldFiles = oldContent.getFiles();
System.out.println("Old Text: " + oldText);
System.out.println("Old Image: " + oldImage);
System.out.println("Old File: " + oldFiles);
System.out.println("New String Content: " + newContent);
});myClipboard.addEventListener((ImageEvent) (oldContent, newContent) -> {
String oldText = oldContent.getText();
BufferedImage oldImage = oldContent.getImage();
List oldFiles = oldContent.getFiles();
System.out.println("Old Text: " + oldText);
System.out.println("Old Image: " + oldImage);
System.out.println("Old File: " + oldFiles);
System.out.println("New Image Content: " + newContent);
});myClipboard.addEventListener((FileEvent) (oldContent, newContent) -> {
String oldText = oldContent.getText();
BufferedImage oldImage = oldContent.getImage();
List oldFiles = oldContent.getFiles();
System.out.println("Old Text: " + oldText);
System.out.println("Old Image: " + oldImage);
System.out.println("Old File" + oldFiles);
System.out.println("New File Content: " + newContent);
});
// Insert into the clipboard
myClipboard.insert("exampleContent");
// Insert and notify MyClipboard of the new content
myClipboard.insertAndNotify("exampleContent");// Set monitoring for clipboard types
myClipboard.setImageMonitored(true || false);
myClipboard.setTextMonitored(true || false);
myClipboard.setFileMonitored(true || false);
// Toggle monitoring for clipboard types
myClipboard.toggleTextMonitored();
myClipboard.toggleImagesMonitored();
myClipboard.toggleFilesMonitored();
// Stop listening for clipboard notifications
myClipboard.stopListening();
}
}
```# Windows / *Unix
This approach differs from the macOS section below as Windows/*Unix properly notify the program with global clipboard events.
This allows for a more event-driven approach as lostOwnership() is triggered whenever the clipboard has lost ownership - clipboard
has new content within it - and the contents can be observed by multiple consumers.# macOS
Unlike the aforementioned event-driven approach, macOS unfortunately is not very good at notifying the program if the
system clipboard has changed. To query the system clipboard contents, we need to employ a polling schedule. I have chosen
**200ms** to ensure that images and large files can be copied over as well as reducing the load on the CPU.# Installation
Latest Stable Version: ![maven-central]
Be sure to replace the VERSION key below with the one of the versions shown above!
**Maven**
```xmlme.goudham
MyClipboard
VERSION```
**Gradle**
```gradle
repositories {
mavenCentral()
}dependencies {
// https://mvnrepository.com/artifact/me.goudham/MyClipboard
implementation group: 'me.goudham', name: 'MyClipboard', version: 'VERSION'
}
```# License
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fsgoudham%2FMyClipboard?ref=badge_large)