Hi. I have a wxRichtextCtrl (although plain text is fine if I can find a way to do this), and I would like to return the last word typed while the user carries on.

You know that way MS Word and OpenOffice etc, highlight a wrongly spelled word as soon as you move away from it? Well that's what I mean (although I need to return the word, not highlight it).

Iv tried all manner of things in an EVT_TEXT event, and to some extent things have worked, right up to the point where I use Backspace or Delete, or move around the document with the mouse or the arrow keys. Once any of those things happen it all falls apart!

At the moment I have two vars. One hold the start and the other holds the end of the current word. When the user hits Return or types a space, I use GetRange() to return the last word and then set Startvar to EndVar and carry on.

Like I said, it works, but only up to a point.

Can anyone help me?



Recommended Answers

All 6 Replies

I cant find the edit button so Ill make a new post.....

Just in case its helpful to know, the reason for this is to highlight specific words as they are typed. For example if the user types the word "Theory" (including caps) then as they carry on typing, that word will be highlighted in some way.


Like I said, it works, but only up to a point.

What do you mean by this? So can you correctly highlight words? If so, what is your problem?

Also, could you post your code? I'd love to dive into this and come up with a solution, as it would prove to be helpful to one of the scripts I'm writing.

EDIT: Okay, I think after some re-reading I understand your problem a little better. So your problem is when the user strays away from the current word by using the arrow keys/mouse/etc? Post your code and I could help you.

Some code. This should highlight every word as it is typed, although of course there would be some kind of check in place to highlight only specific words.

It works but its only linear; if you start typing it will work, but change the location of the cursor with the mouse or the arrow keys, or indeed the delete or backspace keys, and things start to go wrong.

I did this exact thing years ago in Delphi under Windows, but I no longer have that code to reference and Iv a feeling that the methods exposed for the text control were somewhat different. Id like to sort this out because I have a long running project that I use on a daily basis and it is in need of a brush up! Highlighting words on the fly would be of major benefit (This isn't for a spell checker btw)

def txt1_onText(self, event):

        self.EndP = self.txt1.GetInsertionPoint()

        if self.EndP < self.StartP: self.StartP = self.EndP

        m = self.txt1.GetRange(self.EndP-1,self.EndP)

        if len(m) > 0:

            if ord(m) == 10 or ord(m) ==  32:

                e = self.txt1.GetRange(self.StartP,self.EndP)

                #self.T1 = self.StartP

                #self.T2 = self.EndP


		a = rt.RichTextRange(self.StartP,self.EndP-1)

		b = rt.TextAttrEx()




		self.txt1.SetStyle(a, b)

                self.StartP = self.EndP

A little more:
Further playing with this has helped - I set the StartP and EndP variables mentioned above when the mouse is clicked in the control, but this does not seem to work all the time, and sometimes (I cant seem to pinpoint exactly why/when) backspace and delete mess things up, and moving the cursor with the arrow keys does the same.

Also, the code as it stands only checks and highlights when the user hits the return key or space (indicating the end of a word), but somehow this should also work if the user types a word, does NOT press space or enter, and moves the cursor away. However, Iv not managed to get that working and I ended up with the 'Space or Return' method above.



As for your mouse problem, I think I may have a solution. Right now, I assume you are binding the Enter and Space keys to your txt1_onText method. If you're just having trouble setting your EndP variable when the user clicks away, why not just bind the mouse to a transition method before the program has a chance to change the insertion point? You can do this by binding a mouse click to a method that calls your txt1_onText method before the cursor is moved by the program:

# Wherever you do your bindings
self.txt1.Bind(wx.EVT_LEFT_DOWN, self.txt1_onText)

def mouseClick(self, event):
    self.txt1_onText(event=None) # Wait until you get your EndP data before continuing with the event (next line)

Does this solve the mouse problem?

Thanks, but that causes some really nasty problems! When you click away, the mouse event triggers the text event, but the end position is set straight away, which means that large swaths of the document are highlighted!

I tried this in the mouse event instead:

self.EndP = self.tEDIT.GetInsertionPoint()
self.StartP = self.EndP

and it seems to work fairly well, but there are still issues when clicking around, although again, its hard to pin down exactly whats happening, because sometimes its fine and sometimes its not. Iv a feeling that it depends on the user typing (or not) when the mouse is moved about.

Iv spent some time hunting for help on this whole thing and so far drawn a blank. Getting the last character typed is easy enough, but keeping track of the last word typed, and its location, no matter what the user does with the cursor, is not so easy it seems!

If I make any progress Ill update here, and if you do the same we may actually work this out once and for all!



Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.