• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get selected text

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rancher,
How do I get the selected text of a screen. I don't want it from the current screen, Like if I select this underline sentence, and then press F2(or any key of my choice which is available to my use), then that selected text should copy to the google search. So, here I am not copying, I am just selecting.
Can I do this using java ?

Thanks
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can I do this using java ?  


Yes, if all the parts are in a java application.  I don't think it can be done with other applications.

Keep us posted if you find a way to do what you are asking about.  It sounds useful.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the selected text is in the system clipboard, then this can be done with Toolkit.getDefaulttoolkit().getSystemClipboard().
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:If the selected text is in the system clipboard, then this can be done with Toolkit.getDefaulttoolkit().getSystemClipboard().


Yes. But I don't want it from clipboard. Instead of crtl+c , I want to have my own keystrock which select the selected text(not from the clipboard).
Can you please tell me how to get access to the keystrocks ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how to get access to the keystrocks ?


Take a look at the tutorial for key binding: https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
 
Marshal
Posts: 28236
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you clarified where the selected string is. You asked "How do I get the selected string"... well, you should just look at it with your eyes. But I expect you are asking how your Java application can get the selected string. So, is the selected string in something to do with that Java application, or is it perhaps in some other unrelated application?

If it's the former then you need to tell us about that application and how it's displaying the text which was selected. If it's some other application then you need to tell us how you're going to choose that other application on behalf of your application. Remember that there might be several applications running on your computer and any or all of them might happen to have some text selected.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

how to get access to the keystrocks ?


Take a look at the tutorial for key binding: https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html


Thanks Norm.
Can we use any other key(user defined not the OS defined) for copying the text. Like if I select a text and press F9, then that text should get copied to the clipboard. Can we do this. Please help.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:It would help if you clarified where the selected string is. You asked "How do I get the selected string"... well, you should just look at it with your eyes. But I expect you are asking how your Java application can get the selected string. So, is the selected string in something to do with that Java application, or is it perhaps in some other unrelated application?

If it's the former then you need to tell us about that application and how it's displaying the text which was selected. If it's some other application then you need to tell us how you're going to choose that other application on behalf of your application. Remember that there might be several applications running on your computer and any or all of them might happen to have some text selected.


Hi Paul,
The selected text can be anywhere. Like, if I am reading a post on coderanch, then if I select a text over here(only select, not copying), and then if I press F9(or whatever valid key i can use) then my JAVA application should get opened with that string written (which I have selected on coderanch) on my JAVA application.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can we do this. Please help.


Yes when it is all within a java application.  
AFAIK The displaying of text and the allowing of selection of text being shown by an application is handled by each application separately from all other applications.  Each application does its own thing.
The OS provides a clipboard that applications can write to and read from.
The only way I know for application one to see what text is being shown and selected in application two is by taking a screen print and scanning the pixels.

There may be some underlying OS feature that can detect text that is being displayed and that some of the text is selected, but I don't know of it.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
Can we use any other key(user defined not the OS defined) for copying the text. Like if I select a text and press F9, then that text should get copied to the clipboard. Can we do this. Please help.


See my answer about using Toolkit.getDefaulttoolkit().getSystemClipboard(). That returns the Clipboard, which has a setContents() method.
 
Paul Clapham
Marshal
Posts: 28236
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most operating systems make it very difficult for one application to spy on the processing of another application. Consider for example a problem not unlike yours, except now you want the application to observe an input field adjacent to a text string which looks like "Credit Card Number". Such an example would make it possible for an application to steal secure information from a user. That's why operating systems try to prevent it.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The only way I know for application one to see what text is being shown and selected in application two is by taking a screen print and scanning the pixels.


Can you provide me any source where i can learn this ?

Paul wrote:Most operating systems make it very difficult for one application to spy on the processing of another application. Consider for example a problem not unlike yours, except now you want the application to observe an input field adjacent to a text string which looks like "Credit Card Number". Such an example would make it possible for an application to steal secure information from a user. That's why operating systems try to prevent it.


Thanks Paul, That's an useful info I learned today.

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Robot class has a method for capturing the screen.
The scanning of the captured screen's pixels would require sophisticated software.  I don't know of any.
 
reply
    Bookmark Topic Watch Topic
  • New Topic