Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:45:01+00:00 2026-06-15T21:45:01+00:00

This is more of a hypothetical question, I’m brainstorming some ideas for a project

  • 0

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)

Any knowledge in what APIs exist for this would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:45:02+00:00Added an answer on June 15, 2026 at 9:45 pm

    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.

    This is a short sample:

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a hypothetical question. I am wondering if anyone knows if/how
This is more of a hypothetical question as I am using .NET 3.5 more
(This was meant to be a general hypothetical question, me whining that .NET was
this is more of a fundamental question at how apache/threading works. in this hypothetical
This is more of a hypothetical whilst I'm debugging some code. Lets say I
This is probably going to be more of a discussion or hypothetical question, but
Ok, I'm going to try to make this more clear because my last question
This is more of a design question. I have a template class, and I
This question is quite hypothetical but I'll ask anyway. Let's say you start a
This is a theoretical question, so expect that many details here are not computable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.