Skip to content

Commit

Permalink
Implementing modifier keys in sendEvent()
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans authored and ariya committed Oct 29, 2012
1 parent b7ca845 commit f402a8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/webpage.cpp
Expand Up @@ -1015,8 +1015,9 @@ QObject *WebPage::_getJsPromptCallback() {
return m_callbacks->getJsPromptCallback();
}

void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVariant &arg2, const QString &mouseButton)
void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVariant &arg2, const QString &mouseButton, const QVariant &modifierArg)
{
Qt::KeyboardModifiers keyboardModifiers(modifierArg.toInt());
// Normalize the event "type" to lowercase
const QString eventType = type.toLower();

Expand All @@ -1034,18 +1035,18 @@ void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVarian
if (arg1.type() == QVariant::Char) {
// a single char was given
text = arg1.toChar();
key = text.at(0).toAscii();
key = text.at(0).unicode();
} else if (arg1.type() == QVariant::String) {
// javascript invokation of a single char
text = arg1.toString();
if (!text.isEmpty()) {
key = text.at(0).toAscii();
key = text.at(0).unicode();
}
} else {
// assume a raw integer char code was given
key = arg1.toInt();
}
QKeyEvent *keyEvent = new QKeyEvent(keyEventType, key, Qt::NoModifier, text);
QKeyEvent *keyEvent = new QKeyEvent(keyEventType, key, keyboardModifiers, text);
QApplication::postEvent(m_customWebPage, keyEvent);
QApplication::processEvents();
return;
Expand All @@ -1056,14 +1057,14 @@ void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVarian
if (arg1.type() == QVariant::String) {
// this is the case for e.g. sendEvent("...", 'A')
// but also works with sendEvent("...", "ABCD")
foreach(QChar typeChar, arg1.toString()) {
sendEvent("keydown", typeChar);
sendEvent("keyup", typeChar);
foreach(const QChar typeChar, arg1.toString()) {
sendEvent("keydown", typeChar, NULL, NULL, modifierArg);
sendEvent("keyup", typeChar, NULL, NULL, modifierArg);
}
} else {
// otherwise we assume a raw integer char-code was given
sendEvent("keydown", arg1.toInt());
sendEvent("keyup", arg1.toInt());
sendEvent("keydown", arg1.toInt(), NULL, NULL, modifierArg);
sendEvent("keyup", arg1.toInt(), NULL, NULL, modifierArg);
}
return;
}
Expand Down Expand Up @@ -1106,9 +1107,9 @@ void WebPage::sendEvent(const QString &type, const QVariant &arg1, const QVarian
m_mousePos.setY(arg2.toInt());
}

// Prepare the Mouse event (no modifiers or other buttons are supported for now)
// Prepare the Mouse event
qDebug() << "Mouse Event:" << eventType << "(" << mouseEventType << ")" << m_mousePos << ")" << button << buttons;
QMouseEvent *event = new QMouseEvent(mouseEventType, m_mousePos, button, buttons, Qt::NoModifier);
QMouseEvent *event = new QMouseEvent(mouseEventType, m_mousePos, button, buttons, keyboardModifiers);

// Post and process events
QApplication::postEvent(m_customWebPage, event);
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.h
Expand Up @@ -248,7 +248,7 @@ public slots:
QObject *_getJsConfirmCallback();
QObject *_getJsPromptCallback();
void uploadFile(const QString &selector, const QString &fileName);
void sendEvent(const QString &type, const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant(), const QString &mouseButton = QString());
void sendEvent(const QString &type, const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant(), const QString &mouseButton = QString(), const QVariant &modifierArg = QVariant());

/**
* Returns a Child Page that matches the given <code>"window.name"</code>.
Expand Down

0 comments on commit f402a8d

Please sign in to comment.