How to receive the return of a selected text in windows?

Asked

Viewed 58 times

0

I wonder if there are any classe no java, with some method that allows me to receive those text passages that we selected with the course of text in any document within the Windows(is a text of a PDF,Word or some página web).

The reason why I’m wanting this functionality, is that I will develop a program that uses this piece of text acquired through selection,as input for some dictionary server to return me the meaning of the word.

1 answer

0

No, there are no classes for this feature within the Java SE or Java EE Platform. Which doesn’t mean you can’t build this functionality using Java, it’s perfectly possible. You will need to write native code. This will actually hold your code over to the Operating System, in your case Windows.

For this Java prove what we call JNI

Java Native Interface

Check documentation here: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/

carefully read this other tutorial

http://blog.caelum.com.br/escrevendo-metodos-nativos-em-java-com-jni-e-jna/

Last but not least, I found this code made for someone wanted to do something myth similar to what you want to do:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.StdCallLibrary;

public class Foo implements ClipboardOwner {
    public interface CustomUser32 extends StdCallLibrary {
        CustomUser32 INSTANCE = (CustomUser32) Native.loadLibrary("user32", CustomUser32.class);
        HWND GetForegroundWindow();
        void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        // dummy: needed for `ClipboardOwner`
    }

    void controlC(CustomUser32 customUser32) {
        customUser32.keybd_event((byte) 0x11 /* VK_CONTROL*/, (byte) 0, 0, 0);
        customUser32.keybd_event((byte) 0x43 /* 'C' */, (byte) 0, 0, 0);
        customUser32.keybd_event((byte) 0x43 /* 'C' */, (byte) 0, 2 /* KEYEVENTF_KEYUP */, 0);
        customUser32.keybd_event((byte) 0x11 /* VK_CONTROL*/, (byte) 0, 2 /* KEYEVENTF_KEYUP */, 0);// 'Left Control Up
    }

    String getClipboardText() throws Exception {
        return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
    }

    void setClipboardText(String data) throws Exception {
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(data), this);
    }

    String getSelectedText(User32 user32, CustomUser32 customUser32) throws Exception {
        HWND hwnd = customUser32.GetForegroundWindow();
        char[] windowText = new char[512];
        user32.GetWindowText(hwnd, windowText, 512);
        String windowTitle = Native.toString(windowText);
        System.out.println("Will take selected text from the following window: [" + windowTitle + "]");
        String before = getClipboardText();
        controlC(customUser32); // emulate Ctrl C
        Thread.sleep(100); // give it some time
        String text = getClipboardText();
        System.out.println("Currently in clipboard: " + text);
        // restore what was previously in the clipboard
        setClipboardText(before);
        return text;
    }

    public static void main(String[] args) throws Exception {
        Foo foo = new Foo();
        Thread.sleep(2000); // take some time for you to select something anywhere
        System.out.println(foo.getSelectedText(User32.INSTANCE, CustomUser32.INSTANCE));
    }
}

When you run, you will have two seconds to select text in any application and then you will print these strings.

Source: https://stackoverflow.com/questions/13839780/monitor-text-that-is-highlighted

  • Filipe, It will be a further motivation to implement these extra features. I am very grateful for your help. Too bad I still can’t vote ,otherwise you’d have my vote guaranteed!! Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.