Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
im creating a windows forms some why i want to get selected text from other running applications using global hook keys i know how to use hook keys but i dont know how to get the selected text
like web browser or note bad
Posted
Comments
Vedat Ozan Oner 13-Feb-14 15:56pm    
do you mean pasting from clipboard, by any chance?
prince.Hassan22 13-Feb-14 16:00pm    
nolook ineed to get the currently selected text when i prees f2 key

1 solution

Going in for such things is apparently the bad business. There is no an universal reliable way of doing such tricks; you can do it for one specific application, but that method won't work for others. Notably, if you can access some control and get its text by using raw Windows API based on Windows messaging mechanism, it won't work for WPF applications, which are based on Direct X and participate in windows message pumping only on the very top level of the window, while internal life of events controlling UI elements remains totally non-transparent to Windows API.

With the global Windows Hook, you of course can perfectly simulate input devices and hence input to any kinds of applications. It cannot help you to get data from those applications, such as selected text or anything like that.

And I cannot see truly legitimate reasons to do such things. This is not how real programming work. Software engineering is done by writing code, not by hacking other applications. If you want, call it "integration", but for the applications not specially designed for "integration" this is hacking. If you think about it — quite counter-productive activity, waste of time.

[EDIT]

Here is the thing: before, you did not mention that you merely wanted to get selected text in the system keyboard. Thank you for clarification in your comment below.

This is of course quite possible and actually easy enough. All you need is to use the raw Windows API SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

With .NET, you need to use P/Invoke to do it, but this is already done for you. Here:
http://www.pinvoke.net/default.aspx/user32.sendinput[^].

You can simply simulate the press to Ctrl+A (select all) Ctrl+C or Ctrl+Insert, or anything you need to get.

(For a record: to me, it will still remains a hack, even though it is very easy. This is not how real software engineering is done.)

—SA
 
Share this answer
 
v4
Comments
prince.Hassan22 13-Feb-14 16:31pm    
i dont need to hack application i just wont to make something like copying text like pressing ctrl + c the selected text is sent to clipboard i want the selected text to go to my textbox in my application that's all.
there is a solution on msdn http://social.msdn.microsoft.com/Forums/en-US/31f1d6e3-2d1f-41a2-a731-5c8b1e230c3b/capture-highlighted-text-from-any-window-using-c?forum=csharplanguage
but it only works with notebad but i need it get text from the web browser
Sergey Alexandrovich Kryukov 13-Feb-14 16:48pm    
It depends on what you call "hack". To me, the hack is starting with your question itself. I already answered: a reliable universal method cannot exist, try to explain why. Oh yes, you are right: if you only want to get away with getting some text to the system clipboard, you can always do it with any application... just a minute...
—SA
Sergey Alexandrovich Kryukov 13-Feb-14 16:53pm    
This is really easy — please see my update to the answer, after [EDIT]...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900