Java - Listen for copy and paste from clipboard

Viewed 397

( 1 ) Is there a way to listen for any clipboard updates (including Ctrl+C/X, PrtSc (screenshot) and changes made by other programs) in Java? I have tried this:

    Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(new FlavorListener() {
        @Override
        public void flavorsChanged(FlavorEvent e) {
            System.out.println("Copy detected");
        }
    });

This handles Ctrl+C changes well but doesn't notice changes which are not made by user manually, e.g. by screenshotting software or PrtSc button.

( 2 ) Is there a way to listen for paste actions (Ctrl+V, "paste" button, etc.)? I want something like that (or just with similar functionality):

// ...
    @Override 
    public void prePaste(PasteEvent e) {
        System.out.println("Paste detected");
        e.cancel(); // reject the paste (so that user's Ctrl+V pastes nothing)
    }
// ...
0 Answers
Related