# How to get highlighted text from event

I want to do the following:

Given a text in a Mathematica dynamic module "at runtime". If some text is selected with the cursor/mouse, after the mouse button release, get the selected text.

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Aug 26 '15 at 17:16
• How is the text presented? In a text cell printed from inside a dynamic module? Can you give an example? – C. E. Aug 26 '15 at 18:12
• The text is in an InputField in a dynamicModule. – Ricsi Nincs Aug 27 '15 at 9:14

The following copies the selected text to the clipboard

DynamicModule[{},
EventHandler[Dynamic@InputField[x],
{"MouseUp" :>
FrontEndExecute[FrontEndToken[FrontEndSelectedNotebook[], "Copy"]]},
PassEventsDown -> True]]


The text is stored (upon releasing the mouse button) in the variable val with this code:

 EventHandler[
InputField[],
{"MouseUp" :> (val = CurrentValue["SelectionData"])},
PassEventsDown -> True
]


It should work in other contexts as well besides InputField`, as I noted in my comment it is not clear from your question what the context is.