Friday, June 24, 2011

QT keyboard events

I am here to make a quick post. I searched for this at many places but could not get the exact solutions. Every message I could read sounded cryptic.

I want to generate fake keyboard events and send to QWebView class.

Extend this class to have slots for signals. Connect it to a signal that can pass an ascii character.

Keyboard does not have 'a' and 'A'. It only has an 'A'. That means, I cannot directly send any lower case characters using the event (Which is not correct)

The solution is simple. The constructor of QKeyEvent can take QString as one of its argument. The signature is as follows:

QKeyEvent ( Type type, int key, Qt::KeyboardModifiers modifiers, const QString & text = QString(), bool autorep = false, ushort count = 1 )

Even if we don't pass the QString, it will work. However, by passing the QString, we can control exactly what will be displayed.
This is how I create my QKeyEvent:

void MyWebView::rcvKeys(int k)

char str[2];

QKeyEvent *k=new QKeyEvent(QEvent::KeyPress,k,Qt::NoModifier,QtString(str),false,0)

//-----------Pass it to postevent etc