[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: VerifyListener problem

Derek R. wrote:
Hi all,

First off, my goal is to have a Text Widget that only contains uppercase input, regardless of whether shift is held or caps lock is on. Since I have not found anyway of doing this using a style bit, I am doing so using the VerifyListener below (please let me know if there is a proper way of telling a Text Widget to convert all input to uppercase!!):

VerifyListener verifyListener = new VerifyListener()
{
    public void verifyText(VerifyEvent e)
    {
         e.text = e.text.toUpperCase();
    }
};

Anyway, the issue I am seeing with adding a verifyListener to a Text Widget is that shift+backspace does not perform a backspace for some reason, as it does when the verifyListener is not added to the Text Widget. I don't believe this has anything to do with the uppercase line because the same thing happens with an empty verifyText method. Is this a known issue? Does anyone have a solution for this?

Your listener needs to be a little intelligent about how it handles the event; depending on how the text is modifies (single keystroke, Paste action, programmatic, etc) the event's fields will be populated differently.
The best thing I did was set up a simple VerifyListener and put a breakpoint in it so I could try different ways of modifying the text and inspect the resulting event. As an example, here is a verifyText() that I use:


public void verifyText(VerifyEvent event) {
	// Allow Delete and Backspace keys to pass through:
	if (event.keyCode == SWT.DEL || event.keyCode == SWT.BS) {
		return;
	}

	// If this is not a single keystroke, validate the entire text
	if (event.keyCode == 0) {
		event.doit = matches(event.text);
	} else {
	// Otherwise only validate the typed character:
		event.doit = matches(event.character);
	}
}


Hope this helps, Eric