It doesn't always come from the keyboard. Nor can you only worry about the case where a user is typing at the end of a line. What about if the user is inserting text somewhere else in the line? Or deleting text? The text that is sent from the iSeries is not wrapped,
I see. Well, ... The source code for Tk text box I'm sure has the code you need. Don't know how readable. But it would handle all your cases.