diff --git a/QtScrcpy/device/controller/controller.cpp b/QtScrcpy/device/controller/controller.cpp index d24c69fc5..953a11d03 100644 --- a/QtScrcpy/device/controller/controller.cpp +++ b/QtScrcpy/device/controller/controller.cpp @@ -56,6 +56,15 @@ void Controller::updateScript(QString gameScript) connect(m_inputConvert, &InputConvertBase::grabCursor, this, &Controller::grabCursor); } +bool Controller::isCurrentCustomKeymap() +{ + if (!m_inputConvert) { + return false; + } + + return m_inputConvert->isCurrentCustomKeymap(); +} + void Controller::onPostBackOrScreenOn() { ControlMsg *controlMsg = new ControlMsg(ControlMsg::CMT_BACK_OR_SCREEN_ON); diff --git a/QtScrcpy/device/controller/controller.h b/QtScrcpy/device/controller/controller.h index ca19b77b3..3a7ef0614 100644 --- a/QtScrcpy/device/controller/controller.h +++ b/QtScrcpy/device/controller/controller.h @@ -21,6 +21,7 @@ class Controller : public QObject void test(QRect rc); void updateScript(QString gameScript = ""); + bool isCurrentCustomKeymap(); public slots: void onPostGoBack(); diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertbase.h b/QtScrcpy/device/controller/inputconvert/inputconvertbase.h index 6cc28cf56..7cdde2ca0 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertbase.h +++ b/QtScrcpy/device/controller/inputconvert/inputconvertbase.h @@ -21,6 +21,10 @@ class InputConvertBase : public QObject virtual void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) = 0; virtual void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize) = 0; virtual void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize) = 0; + virtual bool isCurrentCustomKeymap() + { + return false; + }; signals: void grabCursor(bool grab); diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp index 5dcf38dc2..06244620c 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp +++ b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp @@ -78,7 +78,7 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c } // small eyes - if (from->key() == m_keyMap.getMouseMoveMap().data.mouseMove.smallEyes.key) { + if (m_keyMap.isValidMouseMoveMap() && from->key() == m_keyMap.getMouseMoveMap().data.mouseMove.smallEyes.key) { m_ctrlMouseMove.smallEyes = (QEvent::KeyPress == from->type()); if (QEvent::KeyPress == from->type()) { @@ -110,6 +110,9 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c case KeyMap::KMT_CLICK_TWICE: processKeyClick(node.data.clickTwice.keyNode.pos, true, false, from); return; + case KeyMap::KMT_CLICK_MULTI: + processKeyClickMulti(node.data.clickMulti.keyNode.delayClickNodes, node.data.clickMulti.keyNode.delayClickNodesCount, from); + return; case KeyMap::KMT_DRAG: processKeyDrag(node.data.drag.keyNode.pos, node.data.drag.keyNode.extendPos, from); return; @@ -121,6 +124,11 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize &frameSize, c } } +bool InputConvertGame::isCurrentCustomKeymap() +{ + return m_gameMap; +} + void InputConvertGame::loadKeyMap(const QString &json) { m_keyMap.loadKeyMap(json); @@ -164,8 +172,15 @@ void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAct if (!controlMsg) { return; } - controlMsg->setInjectTouchMsgData( - static_cast(id), action, static_cast(0), QRect(calcFrameAbsolutePos(pos).toPoint(), m_frameSize), 1.0f); + + QPoint absolutePos = calcFrameAbsolutePos(pos).toPoint(); + static QPoint lastAbsolutePos = absolutePos; + if (AMOTION_EVENT_ACTION_MOVE == action && lastAbsolutePos == absolutePos) { + return; + } + lastAbsolutePos = absolutePos; + + controlMsg->setInjectTouchMsgData(static_cast(id), action, static_cast(0), QRect(absolutePos, m_frameSize), 1.0f); sendControlMsg(controlMsg); } @@ -302,6 +317,34 @@ void InputConvertGame::processKeyClick(const QPointF &clickPos, bool clickTwice, } } +void InputConvertGame::processKeyClickMulti(const KeyMap::DelayClickNode *nodes, const int count, const QKeyEvent *from) +{ + if (QEvent::KeyPress != from->type()) { + return; + } + + int key = from->key(); + int delay = 0; + QPointF clickPos; + + for (int i = 0; i < count; i++) { + delay += nodes[i].delay; + clickPos = nodes[i].pos; + QTimer::singleShot(delay, this, [this, key, clickPos]() { + int id = attachTouchID(key); + sendTouchDownEvent(id, clickPos); + }); + + // Don't up it too fast + delay += 20; + QTimer::singleShot(delay, this, [this, key, clickPos]() { + int id = getTouchID(key); + sendTouchUpEvent(id, clickPos); + detachTouchID(key); + }); + } +} + void InputConvertGame::processKeyDrag(const QPointF &startPos, QPointF endPos, const QKeyEvent *from) { if (QEvent::KeyPress == from->type()) { @@ -361,8 +404,8 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) m_ctrlMouseMove.lastConverPos.setX(m_ctrlMouseMove.lastConverPos.x() + distance.x() / m_showSize.width()); m_ctrlMouseMove.lastConverPos.setY(m_ctrlMouseMove.lastConverPos.y() + distance.y() / m_showSize.height()); - if (m_ctrlMouseMove.lastConverPos.x() < 0.1 || m_ctrlMouseMove.lastConverPos.x() > 0.8 || m_ctrlMouseMove.lastConverPos.y() < 0.1 - || m_ctrlMouseMove.lastConverPos.y() > 0.8) { + if (m_ctrlMouseMove.lastConverPos.x() < 0.05 || m_ctrlMouseMove.lastConverPos.x() > 0.95 || m_ctrlMouseMove.lastConverPos.y() < 0.05 + || m_ctrlMouseMove.lastConverPos.y() > 0.95) { if (m_ctrlMouseMove.smallEyes) { m_processMouseMove = false; int delay = 30; @@ -442,7 +485,7 @@ void InputConvertGame::mouseMoveStopTouch() void InputConvertGame::startMouseMoveTimer() { stopMouseMoveTimer(); - m_ctrlMouseMove.timer = startTimer(1000); + m_ctrlMouseMove.timer = startTimer(500); } void InputConvertGame::stopMouseMoveTimer() @@ -456,6 +499,7 @@ void InputConvertGame::stopMouseMoveTimer() bool InputConvertGame::switchGameMap() { m_gameMap = !m_gameMap; + qInfo() << tr("current keymap mode: %1").arg(m_gameMap ? tr("custom") : tr("normal")); if (!m_keyMap.isValidMouseMoveMap()) { return m_gameMap; diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertgame.h b/QtScrcpy/device/controller/inputconvert/inputconvertgame.h index ab2c23d52..b286d7742 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertgame.h +++ b/QtScrcpy/device/controller/inputconvert/inputconvertgame.h @@ -17,6 +17,7 @@ class InputConvertGame : public InputConvertNormal virtual void mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize); virtual void wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize); virtual void keyEvent(const QKeyEvent *from, const QSize &frameSize, const QSize &showSize); + virtual bool isCurrentCustomKeymap(); void loadKeyMap(const QString &json); @@ -40,6 +41,9 @@ class InputConvertGame : public InputConvertNormal // click void processKeyClick(const QPointF &clickPos, bool clickTwice, bool switchMap, const QKeyEvent *from); + // click mutil + void processKeyClickMulti(const KeyMap::DelayClickNode *nodes, const int count, const QKeyEvent *from); + // drag void processKeyDrag(const QPointF &startPos, QPointF endPos, const QKeyEvent *from); diff --git a/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp b/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp index 6c795da5c..71909013d 100644 --- a/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp +++ b/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp @@ -175,6 +175,41 @@ void KeyMap::loadKeyMap(const QString &json) keyMapNode.data.click.switchMap = getItemBool(node, "switchMap"); m_keyMapNodes.push_back(keyMapNode); } break; + case KeyMap::KMT_CLICK_MULTI: { + // safe check + if (!checkForClickMulti(node)) { + qWarning() << "json error: keyMapNodes node format error"; + break; + } + QPair key = getItemKey(node, "key"); + if (key.first == AT_INVALID) { + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString(); + break; + } + KeyMapNode keyMapNode; + keyMapNode.type = type; + keyMapNode.data.clickMulti.keyNode.type = key.first; + keyMapNode.data.clickMulti.keyNode.key = key.second; + + QJsonArray clickNodes = node.value("clickNodes").toArray(); + QJsonObject clickNode; + keyMapNode.data.clickMulti.keyNode.delayClickNodesCount = 0; + + for (int i = 0; i < clickNodes.size(); i++) { + if (i >= MAX_DELAY_CLICK_NODES) { + qInfo() << "clickNodes too much, up to " << MAX_DELAY_CLICK_NODES; + break; + } + clickNode = clickNodes.at(i).toObject(); + DelayClickNode delayClickNode; + delayClickNode.delay = getItemDouble(clickNode, "delay"); + delayClickNode.pos = getItemPos(clickNode, "pos"); + keyMapNode.data.clickMulti.keyNode.delayClickNodes[i] = delayClickNode; + keyMapNode.data.clickMulti.keyNode.delayClickNodesCount++; + } + + m_keyMapNodes.push_back(keyMapNode); + } break; case KeyMap::KMT_STEER_WHEEL: { // safe check if (!checkForSteerWhell(node)) { @@ -242,7 +277,7 @@ void KeyMap::loadKeyMap(const QString &json) } // this must be called after m_keyMapNodes is stable makeReverseMap(); - qInfo() << "Script updated."; + qInfo() << tr("Script updated, current keymap mode:normal, Press ~ key to switch keymap mode"); parseError: if (!errorString.isEmpty()) { @@ -310,6 +345,10 @@ void KeyMap::makeReverseMap() QMultiHash &m = node.data.clickTwice.keyNode.type == AT_KEY ? m_rmapKey : m_rmapMouse; m.insert(node.data.clickTwice.keyNode.key, &node); } break; + case KMT_CLICK_MULTI: { + QMultiHash &m = node.data.clickMulti.keyNode.type == AT_KEY ? m_rmapKey : m_rmapMouse; + m.insert(node.data.clickMulti.keyNode.key, &node); + } break; case KMT_STEER_WHEEL: { QMultiHash &ml = node.data.steerWheel.left.type == AT_KEY ? m_rmapKey : m_rmapMouse; ml.insert(node.data.steerWheel.left.key, &node); @@ -410,6 +449,45 @@ bool KeyMap::checkForClick(const QJsonObject &node) return checkForClickTwice(node) && checkItemBool(node, "switchMap"); } +bool KeyMap::checkForClickMulti(const QJsonObject &node) +{ + bool ret = true; + + if (!node.contains("clickNodes") || !node.value("clickNodes").isArray()) { + qWarning("json error: no find clickNodes"); + return false; + } + + QJsonArray clickNodes = node.value("clickNodes").toArray(); + QJsonObject clickNode; + int size = clickNodes.size(); + if (0 == size) { + qWarning("json error: clickNodes is empty"); + return false; + } + + for (int i = 0; i < size; i++) { + if (!clickNodes.at(i).isObject()) { + qWarning("json error: clickNodes node must be json object"); + ret = false; + break; + } + + clickNode = clickNodes.at(i).toObject(); + if (!checkForDelayClickNode(clickNode)) { + ret = false; + break; + } + } + + return ret; +} + +bool KeyMap::checkForDelayClickNode(const QJsonObject &node) +{ + return checkItemPos(node, "pos") && checkItemDouble(node, "delay"); +} + bool KeyMap::checkForClickTwice(const QJsonObject &node) { return checkItemString(node, "key") && checkItemPos(node, "pos"); diff --git a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h index 1d4c55580..8cf92b79d 100644 --- a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h +++ b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h @@ -9,6 +9,8 @@ #include #include +#define MAX_DELAY_CLICK_NODES 50 + class KeyMap : public QObject { Q_OBJECT @@ -18,6 +20,7 @@ class KeyMap : public QObject KMT_INVALID = -1, KMT_CLICK = 0, KMT_CLICK_TWICE, + KMT_CLICK_MULTI, KMT_STEER_WHEEL, KMT_DRAG, KMT_MOUSE_MOVE @@ -32,13 +35,21 @@ class KeyMap : public QObject }; Q_ENUM(ActionType) + struct DelayClickNode + { + int delay = 0; + QPointF pos = QPointF(0, 0); + }; + struct KeyNode { ActionType type = AT_INVALID; int key = Qt::Key_unknown; - QPointF pos = QPointF(0, 0); // normal key - QPointF extendPos = QPointF(0, 0); // for drag - double extendOffset = 0.0; // for steerWheel + QPointF pos = QPointF(0, 0); // normal key + QPointF extendPos = QPointF(0, 0); // for drag + double extendOffset = 0.0; // for steerWheel + DelayClickNode delayClickNodes[MAX_DELAY_CLICK_NODES]; // for multi clicks + int delayClickNodesCount = 0; KeyNode( ActionType type = AT_INVALID, @@ -66,6 +77,10 @@ class KeyMap : public QObject KeyNode keyNode; } clickTwice; struct + { + KeyNode keyNode; + } clickMulti; + struct { QPointF centerPos = { 0.0, 0.0 }; KeyNode left, right, up, down; @@ -83,6 +98,7 @@ class KeyMap : public QObject DATA() {} ~DATA() {} } data; + KeyMapNode() {} ~KeyMapNode() {} }; @@ -116,6 +132,8 @@ class KeyMap : public QObject // safe check for KeyMapNode bool checkForClick(const QJsonObject &node); + bool checkForClickMulti(const QJsonObject &node); + bool checkForDelayClickNode(const QJsonObject &node); bool checkForClickTwice(const QJsonObject &node); bool checkForSteerWhell(const QJsonObject &node); bool checkForDrag(const QJsonObject &node); diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 86cb3188e..55ca712ee 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -339,6 +339,14 @@ Device::GroupControlState Device::controlState() return m_controlState; } +bool Device::isCurrentCustomKeymap() +{ + if (!m_controller) { + return false; + } + return m_controller->isCurrentCustomKeymap(); +} + bool Device::saveFrame(const AVFrame *frame) { if (!frame) { diff --git a/QtScrcpy/device/device.h b/QtScrcpy/device/device.h index 29d4b152d..38bc16463 100644 --- a/QtScrcpy/device/device.h +++ b/QtScrcpy/device/device.h @@ -56,6 +56,8 @@ class Device : public QObject void updateScript(QString script); Device::GroupControlState controlState(); + bool isCurrentCustomKeymap(); + signals: void deviceDisconnect(QString serial); diff --git a/QtScrcpy/device/ui/videoform.cpp b/QtScrcpy/device/ui/videoform.cpp index 88bc6c30e..282bc1671 100644 --- a/QtScrcpy/device/ui/videoform.cpp +++ b/QtScrcpy/device/ui/videoform.cpp @@ -501,7 +501,7 @@ void VideoForm::setDevice(Device *device) void VideoForm::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::MiddleButton) { - if (m_device) { + if (m_device && !m_device->isCurrentCustomKeymap()) { emit m_device->postGoHome(); } } @@ -580,7 +580,7 @@ void VideoForm::mouseDoubleClickEvent(QMouseEvent *event) } } - if (event->button() == Qt::RightButton && m_device) { + if (event->button() == Qt::RightButton && m_device && !m_device->isCurrentCustomKeymap()) { emit m_device->postBackOrScreenOn(); } @@ -701,7 +701,7 @@ void VideoForm::dropEvent(QDropEvent *event) const QMimeData *qm = event->mimeData(); QList urls = qm->urls(); - for (const QUrl& url : urls) { + for (const QUrl &url : urls) { QString file = url.toLocalFile(); QFileInfo fileInfo(file); diff --git a/docs/KeyMapDes.md b/docs/KeyMapDes.md index 8232a1cda..d9fabd37f 100644 --- a/docs/KeyMapDes.md +++ b/docs/KeyMapDes.md @@ -35,6 +35,7 @@ There are several types of key mapping as follows: -type The type of key mapping, each element in keyMapNodes needs to be specified, and can be of the following types: -KMT_CLICK Ordinary click, key press simulates finger press, key lift simulates finger lift -KMT_CLICK_TWICE Double click, key press simulates finger press and then lift, key lift simulates finger press and then lift + - KMT_CLICK_MULTI Click multiple times. According to the delay and pos in the clickNodes array, press one key to simulate touching multiple positions -KMT_DRAG drag and drop, the key press is simulated as a finger press and drag a distance, the key lift is simulated as a finger lift -KMT_STEER_WHEEL steering wheel mapping, which is dedicated to the mapping of the steering wheel for moving characters in FPS games, requires 4 buttons to cooperate. @@ -47,7 +48,11 @@ Description of the unique attributes of different key mapping types: -KMT_CLICK_TWICE -key The key code to be mapped - -pos simulates the location of the touch + -pos Simulates the location of the touch + +-KMT_CLICK_MULTI + -delay Delay `delay` ms before simulating touch + -pos Simulates the location of the touch -KMT_DRAG -key The key code to be mapped diff --git a/docs/KeyMapDes_zh.md b/docs/KeyMapDes_zh.md index 84ccda33e..1ea0ce072 100644 --- a/docs/KeyMapDes_zh.md +++ b/docs/KeyMapDes_zh.md @@ -35,6 +35,7 @@ - type 按键映射的类型,每个keyMapNodes中的元素都需要指明,可以是如下类型: - KMT_CLICK 普通点击,按键按下模拟为手指按下,按键抬起模拟为手指抬起 - KMT_CLICK_TWICE 两次点击,按键按下模拟为手指按下再抬起,按键抬起模拟为手指按下再抬起 + - KMT_CLICK_MULTI 多次点击,根据clickNodes数组中的delay和pos实现一个按键多次点击 - KMT_DRAG 拖拽,按键按下模拟为手指按下并拖动一段距离,按键抬起模拟为手指抬起 - KMT_STEER_WHEEL 方向盘映射,专用于FPS游戏中移动人物脚步的方向盘的映射,需要4个按键来配合。 @@ -49,6 +50,10 @@ - key 要映射的按键码 - pos 模拟触摸的位置 +- KMT_CLICK_MULTI + - delay 延迟delay毫秒以后再模拟触摸 + - pos 模拟触摸的位置 + - KMT_DRAG - key 要映射的按键码 - startPos 模拟触摸拖动的开始位置 diff --git a/keymap/test.json b/keymap/test.json new file mode 100644 index 000000000..913d92a0d --- /dev/null +++ b/keymap/test.json @@ -0,0 +1,39 @@ +{ + "switchKey": "Key_QuoteLeft", + "keyMapNodes": [ + { + "comment": "测试一键多点", + "type": "KMT_CLICK_MULTI", + "key": "Key_Space", + "clickNodes": [ + { + "delay": 500, + "pos": { + "x": 0.5, + "y": 0.5 + } + }, + { + "delay": 500, + "pos": { + "x": 0.8, + "y": 0.8 + } + } + ] + }, + { + "comment": "测试拖拽", + "type": "KMT_DRAG", + "key": "Key_Up", + "startPos": { + "x": 0.5, + "y": 0.7 + }, + "endPos": { + "x": 0.5, + "y": 0.3 + } + } + ] +} \ No newline at end of file