I have created a project to get the print the selected Text by the user from the global screen when user selects it(by dragging the mouse).
I have used nativeMousePressed(NativeMouseEvent e) and nativeMouseReleased(NativeMouseEvent e) to detect selecting text from the global screen by using the following lines of code:
@Override
public void nativeMousePressed(NativeMouseEvent e) {
xPress=e.getX();
yPress=e.getY();
}
@Override
public void nativeMouseReleased(NativeMouseEvent e) {
xRelease=e.getX();
yRelease=e.getY();
if(Math.abs(xPress-xRelease)>0||Math.abs(yPress-yRelease)>0){ //if mouse dragged
try {
System.out.println("Selected Text is: "+foo.getSelectedText(foo));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}so here is the code that gets the selected text
Foo.java
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 java.awt.datatransfer.UnsupportedFlavorException;
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 {
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
}
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);
}
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 {
try{
return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
catch(UnsupportedFlavorException e){
System.out.println("UnsupportedFlavorException: error while transferring data");
return "";
}
}
void setClipboardText(String data) throws Exception {
try{
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(data), this);
}
catch(IllegalStateException e){
}
}
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);
String before = getClipboardText();
controlC(customUser32); // emulate Ctrl C
Thread.sleep(100); // give it some time
String text = getClipboardText();
setClipboardText(before);
return text;
}
String getWindowTitle(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);
String before = getClipboardText();
controlC(customUser32); // emulate Ctrl C
Thread.sleep(100); // give it some time
// restore what was previously in the clipboard
setClipboardText(before);
return windowTitle;
}
String getWindowTitle(Foo foo) throws Exception{
return (foo.getWindowTitle(User32.INSTANCE, CustomUser32.INSTANCE));
}
String getSelectedText(Foo foo) throws Exception{
return (foo.getSelectedText(User32.INSTANCE, CustomUser32.INSTANCE));
}
}
it works fine when i select some text by dragging the mouse
but my problem is that when i use the mouse drag to something else (like: moving a file),it doesn't return empty String it returns the clipboard text(text that copied to the clipboard), whenever it should return an empty String because i have selected nothing at that case.
can someone suggest me another better way to monitor the selected text from the global screen, or tell me solve this problem?
Well, there's a bit of JNA in there somewhere! ;-)
Obvious thought would be, what happens when you hit CTRL-C when nothing is selected? What if it's a no op? Your code seems to assume the clipboard value has changed. Maybe check if text still equals before and return an empty string in that case?
Surely there's a better way to do this?!
Best wishes,
Neil
Neil C Smith
Artist : Technologist : Adviser
http://neilcsmith.net