Question

I'm creating a IE browser plug-in that needs to be kept updated about what text has been selected by the user, or if no text is currently selected at all. I learned how to get the selected text by reading here. This is my code for doing so:

var doc = browser.Document as IHTMLDocument2;
var selection = doc.selection as IHTMLSelectionObject;
var range = selection.createRange() as IHTMLTxtRange;
string selectedText = range.text;

However, I am having difficulty determining the event handler(s) from which to access range.text to update my plug-in. Using the selectionchange event alone does not work in all cases. When the user deselects the text by clicking directly on the selection, as opposed to clicking on a different part of the webpage, range.text still contains the old (non-null) value when selectionchange is raised. A workaround is to also listen for click events. A click event is raised immediately after selectionchange, but at which point range.text is finally null. However, I've encountered a further problem for which I haven't found a solution. That is, if a user double-clicks on a word, thus selecting it, the selectionchange event isn't raised at all. Plus, when the click event (or mousedown event or selectstart event) is raised, range.text is still null!

How can I solve this latter problem? Or is there an overall better approach I can take?

Was it helpful?

Solution

Try to use the ondragend event for the simple selection and the ondblclick event for the double click selection. If it doesn't work try to combine some events (http://javascript.gakaa.com/c/events.aspx)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top