PDA

View Full Version : QKeyEvent - no ASCII characters



ctarsoaga
16th February 2010, 20:23
Hi everybody,

I have a simple problem: I try to restrict input characters into a QPlainTextEdit widget to ASCII characters (codes 0 - 127)
I have overridden eventFilter(), installed the event filter, but I don't know how to create the condition.

I tried a naive


if (Qt::Key_nobreakspace <= key && key <= Qt::Key_ydiaeresis) //128 < key < 255
//ignore this since it's not plain ascii

but it does not work as expected; If I press Alt+161 (to enter the char with code 161), I will receive successive QKeyEvents for: Alt - 1 - 6 - 1
and I cannot intercept the real character to be inputted like this

Any suggestion is welcome, thanks
Chris

Lykurg
17th February 2010, 01:08
One other possibility is to get the text document via QPlainTextEdit::document(). Then connect the signal QTextDocument::contentsChange() to a local slot where you check the last added chars. If there is a non valid, remove it.

And is QPlainTextEdit::keyPressEvent() also called only for "ALT-1-6-1"?

bismitapadhy
17th February 2010, 04:19
Hi everybody,

I have a simple problem: I try to restrict input characters into a QPlainTextEdit widget to ASCII characters (codes 0 - 127)
I have overridden eventFilter(), installed the event filter, but I don't know how to create the condition.

I tried a naive


if (Qt::Key_nobreakspace <= key && key <= Qt::Key_ydiaeresis) //128 < key < 255
//ignore this since it's not plain ascii

but it does not work as expected; If I press Alt+161 (to enter the char with code 161), I will receive successive QKeyEvents for: Alt - 1 - 6 - 1
and I cannot intercept the real character to be inputted like this

Any suggestion is welcome, thanks
Chris
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
int key = keyEvent->key();
QString text = keyEvent->text();
if ((keyEvent->key() < '0' || keyEvent->key() > '9') && keyEvent->key() != Qt::Key_Backspace &&
keyEvent->key() != Qt::Key_Left && keyEvent->key() != Qt::Key_Right && keyEvent->key() != Qt::Key_Delete)
{
return true;
}
else
return QObject::eventFilter(obj, event);
}
else
return QObject::eventFilter(obj, event);
}

ctarsoaga
17th February 2010, 09:14
One other possibility is to get the text document via QPlainTextEdit::document(). Then connect the signal QTextDocument::contentsChange() to a local slot where you check the last added chars. If there is a non valid, remove it.

And is QPlainTextEdit::keyPressEvent() also called only for "ALT-1-6-1"?



thanks for the suggestion
no the problem is not related to one specific char, 161 was just an example for a non-ascii character (char with code above 127).
what I want is to allow only ascii (0..127) so I have to discard all inputs not matching this interval

BUT, I am always able to type any character (non ascii, even unicode) by typing Alt+character code e.g: to enter character with unicode value 12531 I can type Alt-1-2-5-3-1
I want to discard such inputs.

Reacting to document changes is one option, thanks.

ctarsoaga
17th February 2010, 09:18
thanks bismitapadhy

Before trying it, I wonder what your code does :-) As I can see, it discards all keys that are not digits... Am I right?
If I am correct, that's not what I want. I have to allow only plain ascii characters (0-127) and discard anyhting else.
Usually, one can insert any character (even unicode) by typing alt+character code, e.g Alt19157 will give you the unicode char 19157 - how can I discard such an input? [I do receive successive keyevents for Alt,1,9,1,5,7]

thanks
Chris

Lykurg
17th February 2010, 09:26
by typing Alt+character code e.g: to enter character with unicode value 12531 I can type Alt-1-2-5-3-1
If you receive a alt key, start recording the following numbers in a private member. If the first non digit key appears, if the value is valid: insert if not, discard.

ctarsoaga
17th February 2010, 10:34
thanks Lykurg, I'll do it like that :-)
I am somehow lazy and I was wondering how Qt inserts such special characters. He handles itself somewhere this kind of input...