Any idea how to detect which text is dragged?

I want this functionality.
when I select some text by dragging, and then, I click my custom button to make those selected text red(or whatever color).
to do so, I should know which text is selected when I click my button,
how can I achieve that?