Hi,
I am currently developing a keyboard based game - and have found out that the best way is an offscreen text field. I have a PlainTextEdit on the form - the rest is in the code below:
Code:
#include "QtGameForm.h"
#include <QKeyEvent>
#include <QMessageBox>
#include <aknviewappui.h>
QtGameForm::QtGameForm(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//arm the decoy field (like on the palm)
ui.dummyEdit->grabKeyboard();
//ui.dummyEdit->setVisible(false); MUST BE VISIBLE!!!
connect(ui.dummyEdit,SIGNAL(textChanged()),this,SLOT(textChanged()));
ui.dummyEdit->setGeometry(700,700,2,2); //MOVE OFFSCREEN
}
QtGameForm::~QtGameForm()
{
}
void QtGameForm::textChanged()
{
QString s=ui.dummyEdit->toPlainText();
if(s==NULL)return;
for(int oi=0;oi<s.length();oi++)
{
QChar suppliedChar=s[oi];
qint32 i;
qint32 best_elem=162;
qint16 maxdepth=-1;
//TOUCHEvtResetAutoOffTimer();
for(i=0;i<100;i++)
{
if(prefs.vars->sign[i]==suppliedChar && prefs.vars->paused==false)
{
if(maxdepth<prefs.vars->ypos[i])
{
best_elem=i;
maxdepth=prefs.vars->ypos[i];
}
}
}
if(best_elem<162) //it is set to 162 by default, if nothing is found
{
if(prefs.consts->players==1)
{
prefs.chars_hit[prefs.vars->sign[best_elem]]++;
}
prefs.vars->sign[best_elem]=NULL;
prefs.vars->xpos[best_elem]=NULL;
prefs.vars->ypos[best_elem]=NULL;
prefs.consts->hitChars++;
prefs.consts->hitstroke++;
prefs.consts->charsonscreen--;
if(prefs.consts->lifeleft<158)
{
prefs.consts->lifeleft++;
}
}
}
ui.dummyEdit->setPlainText(QString(""));
}