监视突出显示文本 [英] Monitor text that is highlighted
问题描述
这更是一个假设性问题,我集思广益为我计划项目的一些想法,并且很好奇,如果有人知道任何的API或得到任何方法的高亮文本即时任何窗口上,例如从一个浏览器或字处理器。它也可以有可能是一个关键的命令,将只读时pressed(类似于CTRL + C将选定的文本到剪贴板)
This is more of a hypothetical question, I'm brainstorming some ideas for a project that I'm planning, and was curious if anyone knew of any APIs or methods of getting any highlighted text instantly on any window, for example from a browser or word processor. It could also have possibly a key command that would only read when pressed (Akin to CTRL+C adding the selected text to a clipboard)
在什么样的API为这个存在的所有知识将大大AP preciated。
Any knowledge in what APIs exist for this would be greatly appreciated.
推荐答案
您可以使用JNA实际前台窗口上模拟按Ctrl-C
(复制操作)然后读什么是剪贴板,在那之后你只需要恢复什么在剪贴板中。
You can use JNA to actually emulate a Ctrl-C
(copy action) on the foreground window, and then read what is in the clipboard, after that you just need to restore what was in the clipboard.
这是一个简短的样本:
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 it, you will have two seconds to select some text somewhere on any application, and then it will normally print it.
会从下面的窗口中选中的文字:【JAVA - 是高亮显示器的文本 - 堆栈溢出 - 谷歌浏览器]
Will take selected text from the following window: [java - Monitor text that is highlighted - Stack Overflow - Google Chrome]
目前在剪贴板:我集思广益一些想法的一个项目,我正打算
Currently in clipboard: I'm brainstorming some ideas for a project that I'm planning
您不需要接受我的答案,它只是告诉你什么是我上面我的评论说。
You don't need to accept my answer, it was just to show you what I said in my comment above.
这篇关于监视突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!