PDA

View Full Version : shift + key



jajdoo
22nd July 2010, 11:12
for some reason i cant make this work.

i tried to detect shift + tab:


switch(e->key())
{

case Qt::Key_Tab:
if(textCursor().selectionEnd() - textCursor().selectionStart() > 0)
{
bool shiftHeld = (e->modifiers() & Qt::ShiftModifier);
tabPressedEvent( shiftHeld );
return;
}
}

didn't work. idea?

saa7_go
22nd July 2010, 13:50
Maybe you need to add
e->accept(); before return statement.

snah
22nd July 2010, 14:22
QWidget detects shift-tab in the event() method (together with some other key combinations, see the source if you're curious) and prevents keypressEvent from being called.
You can re-implement QWidget::event() and handle it there or forward it to keypressEvent manually.

Be aware that this may have side effects for navigating widgets using tab/shift-tab, but I'm not sure about that.

jajdoo
22nd July 2010, 14:42
in an example i found they did it in the KeyPressedEvent - is that outdated?
http://doc.trolltech.com/4.4/tools-customcompleter.html

the accept didn't work.

debugged it a little, seems it detects shift as the input key.
can you post (or point me to) an example of how to do so with the QWidget::event()?

snah
22nd July 2010, 17:26
Ok, just had another look at this and it turns out that QWidget::event() doesn't actually block shift-tab. sorry for misinforming you, looked over the source a bit to hastily.

I've done some testing and found out that shift-tab is not reported as Qt::Key_Tab, but as Qt::Key_Backtab (at least on my system, debian linux with kde). So all you have to do is check for Qt::Key_Backtab:



switch(e->key())
{
case Qt::Key_Tab:
if(textCursor().selectionEnd() - textCursor().selectionStart() > 0)
{
bool shiftHeld = e->modifiers() & Qt::ShiftModifier;
tabPressedEvent( shiftHeld );
}
break;

case Qt::Key_Backtab:
tabPressedEvent( true );
break;
}

Where I kept the shift modifier check, just in case there is a system which does not use backtab to identify shift+tab.

If you need to dissable the behavour that tab/shift+tab changes focus to the next widget you will still need to re-implement event() however. The article Events and Event Filters (http://doc.trolltech.com/4.6/eventsandfilters.html) has an example on this (which is perfect for you;)).

hope this helps and happy coding:cool:.

jajdoo
22nd July 2010, 18:25
wow.. this is almost embarrassingly simple.

this is also true for brackets and braces it seems