PDA

View Full Version : key mapping program Qtimer problems



Charvi
12th July 2012, 10:49
Hi,

I am trying to write a code so that when I press "1" it should map it and show a , b, or c according tot he timeout conditions from the previous key pressed.
Following is my code:



if((e->key() == Qt::Key_1) && ((target->objectName() == "textBox1") || (target->objectName() == "textBox2") || (target->objectName() == "textBox3") || (target->objectName() == "textBox4")))
{
int value;
bool stat = connect(sameKey, SIGNAL(timeout()), this, SLOT(second()));
qDebug()<<"connect returns "<< stat;
sameKey->start(1000);

value = processKey(Qt::Key_1);
qDebug()<<"value received from processKey is "<<value;
if(value == 0)
{
if(target->objectName() == "textBox1")
{
QString temp = textBox1->toPlainText();
temp.append("a");
textBox1->setText(temp);
}
}
else if(value == 1)
{
if(target->objectName() == "textBox1")
{
QString temp = textBox1->toPlainText();
temp.append("b");
textBox1->setText(temp);
}
}
else if(value == 2)
{
if(target->objectName() == "textBox1")
{
QString temp = textBox1->toPlainText();
temp.append("c");
textBox1->setText(temp);
}
}


the process key function


int MainWindow::processKey(int keyCode)
{
// qDebug()<<"got the keycode as "<<keyCode;
current_key = keyCode;

if((!timerFlag) && (current_key == prev_key))
{
qDebug("b4 timer stop");
count++;
if(count == 3)
count = 0;
sameKey->stop();
}
else
{
qDebug("in else");
count = 0;
timerFlag = 0;
// sameKey->stop();
}

prev_key = current_key;
qDebug()<<"returning count as "<<count;
return count;
}


the update timer function


void MainWindow::second()
{
qDebug("in timer update slot.");
sameKey->stop();
timerFlag = 1;
}



But the output I get when I press 1 twice simultaneously is "ab" and not just "b".
Please help me.
Thanks in advance.
- Charvi

high_flyer
12th July 2012, 11:06
you mean something like the following?:



//Header file
...

typedef QMap<int, QString> KeyMapType;
....

KeyMapType m_keyMap;
...

//Implementation
//in some init method (contructor?)
m_keyMap[Qt::Qt::Key_1] = "a";
m_keyMap[Qt::Qt::Key_2] = "b";
...


void MyClass::keyPressEvent ( QKeyEvent * event )
{
KeyMapType::const_iterator itKey = m_keyMap.find(event->key());
if(itKey != m_keyMap.end()){
qDebug()<<(*itKey)->second;
}
}


This is just pseudo code to show the idea.

wysota
12th July 2012, 11:10
... and use QTextCursor to insert characters instead of reading the whole text and setting it back again after appending one character.

Charvi
12th July 2012, 11:34
ummm, actually in my case Key_1 maps to a, b and c depending on the number of times the key is pressed withing a pre-defined time interval.
This corresponds to a functionality just like that in earlier cell phones without Qwerty.

and QTextCursor will certainly reduce the number of LoC. Thanks.

wysota
12th July 2012, 11:35
So attach a string list and take the appropriate cell from the list based on the time period.


and QTextCursor will certainly reduce the number of LoC. Thanks.
It will greatly reduce the lag in your application.