When i get selected text using JNA it returns the Clip Board text

183 views
Skip to first unread message

Hamreen Ahmad

unread,
Oct 10, 2015, 6:08:36 AM10/10/15
to Java Native Access

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?

Kustaa Nyholm

unread,
Oct 10, 2015, 6:39:15 AM10/10/15
to jna-...@googlegroups.com
I don't see the connection to JNA here ... maybe better luck on some other mailing list related to your platform, what ever it is.

br Kusti
> (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?
>
>
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


This e-mail may contain confidential or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. We will not be liable for direct, indirect, special or consequential damages arising from alteration of the contents of this message by a third party or as a result of any virus being passed on or as of transmission of this e-mail in general.

Neil C Smith

unread,
Oct 10, 2015, 7:32:23 AM10/10/15
to jna-users

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

Reply all
Reply to author
Forward
0 new messages