3

这更像是一个假设性问题,我正在为我正在计划的一个项目集思广益,并且很好奇是否有人知道任何 API 或在任何窗口上立即获取任何突出显示的文本的方法,例如从浏览器或文字处理器。它也可能有一个只有在按下时才会读取的键命令(类似于 CTRL+C 将所选文本添加到剪贴板)

任何有关为此存在哪些 API 的知识将不胜感激。

4

2 回答 2

5

您可以使用 JNA 在前台窗口中实际模拟Ctrl-C(复制操作),然后读取剪贴板中的内容,之后您只需要恢复剪贴板中的内容。

这是一个简短的示例:

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));
    }
}

当您运行它时,您将有两秒钟的时间在任何应用程序的某处选择一些文本,然后它通常会打印它。

将从以下窗口中获取选定的文本:[java - 突出显示的监控文本 - 堆栈内存溢出 - Google Chrome]

目前在剪贴板中:我正在为我正在计划的项目集思广益

您不需要接受我的回答,这只是为了向您展示我在上面的评论中所说的话。

于 2012-12-13T07:38:10.980 回答
0
public static String getSelectedData() {
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        Thread.sleep(2*1000);
        robot.keyPress(KeyEvent.VK_C);
        Thread.sleep(2*1000);
        robot.keyRelease(KeyEvent.VK_C);
        Thread.sleep(2*1000);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(2*1000);
    } catch (Exception ex) {
        System.out.println(ex);
    }
    String word = getSelectedData();
    return word;
}

私有静态字符串 getDataFromClipboard() {

Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
try {
    String result = (String) clipboard.getData(DataFlavor.stringFlavor);
    return result;
} catch (Exception e) {
    System.out.println("ERROR");
    return null;
}
}
于 2019-11-15T09:13:46.753 回答