PDA

View Full Version : How to catch "ENTER" with keyPress?



lumber44
14th February 2006, 07:11
Im writing a little game in opengl game using QT(in c++). I got a fullscreen widget and have been using keyPress for keys like down,left,up,down. But when I tried with Enter and Space it don't work. Actually without doing anything, pressing "Enter" messes up animations and stuff. I read some in the documentation and it says it has something to do with focusing maybe? Like with tab, do I have to reimplement QEvent? How? =]

Im using QT 3.2.2 and its my first experience with QT.

high_flyer
14th February 2006, 09:38
Can you show your code that is catching the "enter" key?
Basically you need to overload keyPressEvent(), and check for the key code Qt::Key.
Should not be a problem.

lumber44
14th February 2006, 13:20
This is what I have, I only have problem with ENTER and SPACE of the keys I have tried.


void GM_QtWindow::keyPressEvent( QKeyEvent *k )
{
paintGL();
unsigned char key = k->ascii();

if(key !=0)
{
_keyboard(key,0,0);
}
else
{
int speckey = k->key();

switch(speckey)
{
case Key_A:
_keyboard('a',0,0);
break;
case Key_Up:
_specKeyboard(GM_KEY_UP,0,0);
break;
case Key_Down:
_specKeyboard(GM_KEY_DOWN,0,0);
break;
case Key_Left:
_specKeyboard(GM_KEY_LEFT,0,0);
break;
case Key_Right:
_specKeyboard(GM_KEY_RIGHT,0,0);
break;
case Key_PageUp:
_specKeyboard(GM_KEY_PAGE_UP,0,0);
break;
case Key_PageDown:
_specKeyboard(GM_KEY_PAGE_DOWN,0,0);
break;
case Key_Home:
_specKeyboard(GM_KEY_HOME,0,0);
break;
case Key_End:
_specKeyboard(GM_KEY_END,0,0);
break;
case Key_Insert:
_specKeyboard(GM_KEY_INSERT,0,0);
break;
//not working
case Key_Enter:
cout << "PRESSING ENTER\n";
_specKeyboard(GM_KEY_ENTER,0,0);
break;
case Key_Space:
_specKeyboard(GM_KEY_SPACE,0,0);
break;

//mod-keys
case Key_Shift:
_setModKey(GM_SHIFT_BUTTON); _message("Shift-button down");
break;
case Key_Control:
_setModKey(GM_CONTROL_BUTTON); _message("Ctrl-button down");
break;
case Key_Alt:
_setModKey(GM_ALT_BUTTON); _message("Alt-button down");
break;

default:
break;
}//end switch
}
paintGL();
}

high_flyer
14th February 2006, 14:57
add Key_unknown to the defalut case and see if it gets cought.
You can also put a qDebug("cought key:%d",k->key()) to see what key code is being cought and try to find it in the docs.

wysota
14th February 2006, 15:21
Remember that the so called "ENTER" key, is really called "Return". "Enter" is the key on the numeric keypad. The big one above shift is called "Return" and is handled by Key_Return.

ct
14th February 2006, 19:02
This is what I have, I only have problem with ENTER and SPACE of the keys I have tried.

void GM_QtWindow::keyPressEvent( QKeyEvent *k )
{
paintGL();
unsigned char key = k->ascii();

if(key !=0)
{
_keyboard(key,0,0);
}
else
{
int speckey = k->key();
....


you could also do

if(key==13) //13 being the ascii code for enter
{

//do action for the enter key
}

lumber44
15th February 2006, 00:29
The Key_Return didn't work but the catching of ascii code 13 works. I can now catch Enter but still, it isnt good. Cause every time I hit Enter,space or Tab(if I catch it or not) the widget "freezes", I think it gets/lose focus or something. These keys are handled in some manner in QEvent?("freezes" = all timers stop and stuff :()

From the qt docs (Keyboard Focus):


Another exception to Tab support is text-entry widgets that must support the insertion of tabs; almost all text editors fall into this class. Qt treats Control+Tab as Tab and Control+Shift+Tab as Shift+Tab, and such widgets can reimplement QWidget::event() and handle Tab before calling QWidget::event() to get normal processing of all other keys. However, since some systems use Control+Tab for other purposes, and many users aren't aware of Control+Tab anyway, this isn't a complete solution.

This may be my problem? or am I doing something wrong? =)

lumber44
15th February 2006, 00:49
Hehe, well after reading the docs, I just did what it said :) I thought it would be alot of work, but it turned out to be quite easy :). Its all working now, this is my solution if your interested. Thanks for all your help :)


bool GM_QtWindow::event(QEvent* qe)
{
if (qe->type() == QEvent::KeyPress)
{
QKeyEvent *ke = (QKeyEvent *) qe;
if (ke->key() == Key_Tab)
{
cout << "LOL";
return true;
}
if (ke->key() == Key_Enter)
{
_specKeyboard(GM_KEY_ENTER,0,0);
return true;
}
if (ke->key() == Key_Return)
{
_specKeyboard(GM_KEY_ENTER,0,0);
return true;
}
if (ke->key() == Key_Space)
{
_specKeyboard(GM_KEY_SPACE,0,0);
return true;
}
}
return QWidget::event(qe);
}

joshlareau
30th July 2007, 17:21
Has anyone considered using the QShortcut class to handle intercepting keyboard shortcuts? I had a table widget that was "stealing" the enter and tab keys from me and I got around the problem by using QShortCut as follows:

//Intercept the CTRL-TAB key from the table widget
QShortcut* shortcutTabKey;
shortcutTabKey = new QShortcut( QKeySequence( tr("Ctrl+TAB") ), m_tableWidget );
connect( shortcutTabKey, SIGNAL( activated() ), this, SLOT( OnTableShortCutTabKey() ) );

Hopefully this helps someone, as it seems easier than overriding QWidget::event.