YYPOST群发软件 发表于 6 天前

AIWORK的界面交互: 返回键功能实现

<p style="margin-bottom: 1.5em; box-sizing: border-box;">第一个例子:开启代理模式判断</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.start()<br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=e4e37ec6fa11d454837e6becf424a9ee" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"><br style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">agent.start()</code>&nbsp;函数用于开启代理模式,需 root 权限,无需参数。此函数执行后会返回一个布尔值:如果成功返回&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">true</code>,否则返回&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">false</code>。下面是一个可能的使用例子:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"> <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 尝试启动代理模式</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> result =agent.start();
printl(result);
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (result) {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"代理启动成功!"</span>);
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"代理启动失败,请检查是否具有root权限。"</span>);
}
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">此代码尝试启动代理模式,并根据返回值判断启动是否成功。如果成功,会输出“代理启动成功!”,否则输出“代理启动失败,请检查是否具有root权限。”</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第二例个例子:代理模式命令执行打开</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">adb.exec(‘ls’,false)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=026daaa5291cbf9c8e41a3c7e4273b2e" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">executeShellCommand</span>(<span class="hljs-params" style="box-sizing: border-box;">command, isRoot</span>) </span>{
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">try</span> {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> result = adb.exec(command, isRoot);
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> output = <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">JSON</span>.parse(result);


      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (output.errorMsg) {
            <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.error(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"命令执行出错:"</span>, output.errorMsg);
      } <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
            <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"命令执行成功, 输出结果:"</span>);
            <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(output.success.replace(<span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">/\\n/g</span>, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'\n'</span>));
      }
    } <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">catch</span> (error) {
      <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.error(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"执行期间发生异常:"</span>, error);
    }
}


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 示例调用</span>
executeShellCommand(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'ls'</span>, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">false</span>);
</code></pre><h3 id="h3--code-executeshellcommand-code-" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; font-size: 1.25em !important;"><a name="函数 <code>executeShellCommand</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>函数&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">executeShellCommand</code></h3><h4 id="h4-u53C2u6570" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; font-size: 1.1em !important;"><a name="参数" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>参数</h4><ul style="margin-bottom: 1.5em; margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">command</code>: 要执行的 shell 命令。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isRoot</code>: 布尔值,表示是否以 root 权限执行命令。</li></ul><h4 id="h4-u6B65u9AA4" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; font-size: 1.1em !important;"><a name="步骤" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>步骤</h4><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">执行命令</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">adb.exec(command, isRoot)</code>&nbsp;来执行命令,并获取返回结果。</li><li style="list-style: disc; box-sizing: border-box;">返回结果被假设为 JSON 格式的字符串。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">解析结果</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">JSON.parse(result)</code>&nbsp;将结果字符串解析为对象&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">output</code>。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">检查错误信息</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">如果&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">output.errorMsg</code>&nbsp;存在,表示执行过程中出现错误,输出错误信息。</li><li style="list-style: disc; box-sizing: border-box;">如果没有错误信息,表示命令执行成功,并输出执行结果。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">异常处理</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">try-catch</code>&nbsp;块捕获执行过程中可能发生的异常,并输出异常信息。</li></ul></li></ol><h3 id="h3-u793Au4F8Bu8C03u7528" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; font-size: 1.25em !important;"><a name="示例调用" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>示例调用</h3><ul style="margin-bottom: 1.5em; margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">executeShellCommand('ls', false);</code>: 调用该函数,执行&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ls</code>&nbsp;命令,并不使用 root 权限。</li></ul><p style="margin-bottom: 1.5em; box-sizing: border-box;">该代码的目的是在 JavaScript 环境中通过 ADB 调用安卓设备的 shell 命令,并处理输出结果。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第三个例子,代理模式返回值,</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.back()</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=d299284b2bb97a80a2c3885bb8968f5f" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">function</span> <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">goBack</span>() {
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 按下返回键</span>
    <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">agent</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.back</span>();
    <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">console</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.log</span>(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"返回键已按下"</span>);
}


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 调用示例</span>
<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">goBack</span>();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">第四个例子,代理模式真实座标点击</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.click(100,100)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=42982ff3fb1b3d97930b57e1dbcfc5bd" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 尝试点击屏幕上的某个位置</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> xCoordinate = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 横坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> yCoordinate = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 纵坐标</span>

<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> result = agent.click(xCoordinate, yCoordinate);

<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (result) {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击成功!"</span>);
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击失败,请检查坐标是否正确。"</span>);
}

goBack函数:

定义了一个函数goBack,用于按下返回键。
在函数内部调用agent.back(),执行返回操作。

agent.back():

这是一个没有参数也没有返回值的函数。
作用是模拟按下设备上的返回键。
</code></pre><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义坐标</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">xCoordinate = 100</code>:横向坐标设置为100。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">yCoordinate = 100</code>:纵向坐标设置为100。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">调用点击函数</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">agent.click(xCoordinate, yCoordinate)</code>:尝试点击指定的坐标。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">检查结果并输出</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">如果<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">result</code>为<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">true</code>,说明点击成功,输出”点击成功!”。</li><li style="list-style: disc; box-sizing: border-box;">如果<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">result</code>为<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">false</code>,说明点击失败,输出”点击失败,请检查坐标是否正确。”。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box;">第五个例子,代理模式点击屏幕百分比座标</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.click(0.5,0.5)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=4b317228262462c7a3eeb5b83c308bcd" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 尝试点击屏幕中心位置</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> horizontalPercent = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0.144</span>; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 横坐标百分比</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> verticalPercent = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0.3292</span>;   <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 纵坐标百分比</span>


agent.clickPercent(horizontalPercent, verticalPercent);


<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"已在屏幕中心点击。"</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">这段代码实现了在屏幕中心的点击操作。具体步骤如下:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义点击位置的百分比坐标</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">horizontalPercent</code>和<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">verticalPercent</code>分别定义了横坐标和纵坐标的百分比值。<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">0.5</code>表示水平方向和垂直方向的50%位置,也就是屏幕的中心。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">调用点击函数</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">agent.clickPercent(horizontalPercent, verticalPercent);</code>&nbsp;这行代码使用百分比坐标在屏幕上执行点击操作。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">输出结果</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">console.log("已在屏幕中心点击。");</code>&nbsp;输出一条信息表示点击已在屏幕中心完成。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box;">这个函数通常在自动化测试或脚本中使用,用于模拟用户点击动作。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第六个例子,</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.home()</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=2440631cf98e6a62ff581c5bf3bef824" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 按下设备的HOME键</span>
<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">agent</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.home</span>();

<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">console</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.log</span>(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"已经返回到主屏幕。"</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">解释:</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.home():调用该方法来模拟按下设备的HOME键,使设备返回到主屏幕。<br style="box-sizing: border-box;">console.log(“已经返回到主屏幕。”):输出一条信息到控制台,表示操作已完成。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第七个例子,ADB代理模式输入一个字符串</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.input(‘123456’)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=543aa80b23e7d2ecf8e66b4a62b64ea2" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 输入一个字符串</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> inputString = <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'123456'</span>;
agent.input(inputString);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 输出信息以确认输入操作</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"已输入字符串:"</span>, inputString);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">使用agent.input函数来模拟输入数字字符。该函数只支持英文字符和数字字符,不支持中文字符。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第八个例子,ADB代理模式常用按键事件码</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.keyPress(3)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=72edc133e475176778aca1f05a5094a3" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 定义常用按键事件码</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_HOME = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">3</span>;         <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// Home 键</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_BACK = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">4</span>;         <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// Back 键</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_MENU = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">82</span>;      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// Menu 键</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_VOLUME_UP = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">24</span>;   <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 音量增大键</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_VOLUME_DOWN = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">25</span>; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 音量减小键</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_POWER = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">26</span>;       <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 电源键</span>


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 示例调用不同的按键事件</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">simulateKeyPress</span>(<span class="hljs-params" style="box-sizing: border-box;">keyCode</span>) </span>{
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> result = agent.keyPress(keyCode);
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (result) {
      <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"按键事件成功:"</span>, keyCode);
    } <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
      <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"按键事件失败:"</span>, keyCode);
    }
}


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 调用按键事件</span>
simulateKeyPress(KEYCODE_HOME);
simulateKeyPress(KEYCODE_BACK);
simulateKeyPress(KEYCODE_MENU);
simulateKeyPress(KEYCODE_VOLUME_UP);
simulateKeyPress(KEYCODE_VOLUME_DOWN);
simulateKeyPress(KEYCODE_POWER);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">这段代码定义了一些常用的按键事件码,并通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">simulateKeyPress</code>&nbsp;函数来模拟按键事件。以下是代码的详细说明:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义按键事件码</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_HOME</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_BACK</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_MENU</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_VOLUME_UP</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_VOLUME_DOWN</code>&nbsp;和&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">KEYCODE_POWER</code>&nbsp;是对应不同按键的事件码,分别用于模拟按手机上不同的按钮。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">simulateKeyPress</code>&nbsp;函数</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">接收一个&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">keyCode</code>&nbsp;参数,表示要模拟的按键。</li><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">agent.keyPress(keyCode)</code>&nbsp;方法模拟按键事件。</li><li style="list-style: disc; box-sizing: border-box;">根据&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">result</code>&nbsp;的值来判断按键事件是否成功,并输出相应的日志信息。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">调用按键事件</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">simulateKeyPress</code>&nbsp;函数依次模拟 HOME、BACK、MENU、音量增大、音量减小以及电源按键的事件。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box;">通过这个代码,可以轻松地在程序中模拟各种常用按键的按下操作。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第九个例子,ADB截取代理模式的屏幕截图</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.screenShot(374,666,100)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=b671c4fe4c875e0ae8b4571cedeb9799" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 设置截图的宽度、高度和质量</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> width = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">374.0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> height = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">666.0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> quality = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>;


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 截取代理模式的屏幕截图</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> img = agent.screenShot(width, height, quality);


printl(img);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">变量定义:</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">width: 设置截图的宽度为374像素。<br style="box-sizing: border-box;">height: 设置截图的高度为666像素。<br style="box-sizing: border-box;">quality: 设置截图质量为100。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">截屏操作:</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">使用 agent.screenShot(width, height, quality) 方法截取屏幕截图,并将返回的图像对象存储在 img 变量中。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">结果检查:</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">通过判断 img 是否为真值来检查截屏是否成功。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第十个例子,执行Shell命令,通过坐标模拟点击屏幕</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.shell(‘input tap 455 974’)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=144c6f4604847e7d3ee24138cd3e20e4" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 执行Shell命令,通过坐标模拟点击屏幕</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> command = <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'input tap 455 974'</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> result = agent.shell(command);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 检查命令是否成功执行</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (result) {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Shell命令执行成功:"</span>, result);
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Shell命令执行失败"</span>);
}
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">变量 command:存储要执行的Shell命令,这里是模拟点击屏幕指定位置 (455, 974)。<br style="box-sizing: border-box;">agent.shell(command):发送命令给设备进行执行。<br style="box-sizing: border-box;">“tap” 是一个用于模拟触摸屏幕的命令。执行这个命令会在指定的坐标位置模拟一次点击动作。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第十一个例子,代理模式滑动功能</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.swip(50,100,500,100,10,0,0)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=fcafbde1f06d7c1e1eeef8faf639fc60" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 设置滑动的起点和终点坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> startX = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">50</span>;<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 起点横坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> startY = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>; <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 起点纵坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> endX = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>;   <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 终点横坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> endY = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>;   <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 终点纵坐标</span>


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 滑动的其他参数</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> steps = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">10</span>;       <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 步数中间移动几步</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> downTime = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>;   <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 按下延时毫秒</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> upTime = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>;       <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 拾起延时毫秒</span>


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 执行滑动操作</span>
agent.swip(startX, startY, endX, endY, steps, downTime, upTime);


printl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"滑动操作已完成"</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">演示了如何使用 swip 函数从起点滑动到终点。以下是参数的详细说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">int x1, y1: 起点的横坐标和纵坐标。<br style="box-sizing: border-box;">int x2, y2: 终点的横坐标和纵坐标。<br style="box-sizing: border-box;">int steps: 从起点到终点,滑动所需的步数。<br style="box-sizing: border-box;">int downTime: 按下延迟时间(毫秒)。<br style="box-sizing: border-box;">int upTime: 抬起延迟时间(毫秒)。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=f5773db9288a4555ffcbbdc9319a585d" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第十一个例子,代理模式AI模拟手工滑动功能</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.swipAI(0,0,0,0)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=7cf3e91c4c4d5079af005916ef92962b" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 使用AI算法进行滑动,从起点(0,0)到终点(500,500)</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> startX = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>;    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 起点横坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> startY = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>;    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 起点纵坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> endX = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>;    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 终点横坐标</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> endY = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>;    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 终点纵坐标</span>

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 调用AI滑动函数</span>
agent.swipAI(startX, startY, endX, endY);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 你的额外逻辑处理</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"AI滑动操作完成."</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">说明<br style="box-sizing: border-box;">arg0: int startX — 滑动开始的横坐标。<br style="box-sizing: border-box;">arg1: int startY — 滑动开始的纵坐标。<br style="box-sizing: border-box;">arg2: int endX — 滑动结束的横坐标。<br style="box-sizing: border-box;">arg3: int endY — 滑动结束的纵坐标。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">这个函数模仿人手势的滑动,适用于需要模拟真实触屏滑动的场景。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">第十二个例子,代理模式按下,移动,抬起</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.touchDown(0,0,0)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.touchMove(0,0,0)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">agent.touchUp(0)</p><p style="margin-bottom: 1.5em; box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=d911ad31c6664f63b7a668971a1a71d0" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"><br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=ff158b050d747fea0bd0c99cb72c0d54" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"><br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=eafeafb0d0d08702aa5716ca5d9d1339" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟一个在屏幕上点击并拖动的动作</span>

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 在位置 (100, 100) 触摸屏幕</span>
<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">agent</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.touchDown</span>(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 将触摸移动到位置 (200, 200)</span>
<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">agent</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.touchMove</span>(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">200</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">200</span>);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 松开触摸</span>
<span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">agent</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.touchUp</span>(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box;">何使用 touchDown、touchMove 和 touchUp 方法组合来模拟一个从屏幕上的一个点移动到另一个点的拖动效果。</p><p style="margin-bottom: 1.5em; box-sizing: border-box;">所有代码进行组合:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 开启代理模式</span>
agent.start();
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"代理模式已开启"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 执行Shell命令</span>
agent.shell(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'input tap 455 974'</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"执行Shell命令完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 屏幕截图</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">let</span> img = agent.screenShot(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">374</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">666</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"截图操作完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟按键事件</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">const</span> KEYCODE_HOME = <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">3</span>;
agent.keyPress(KEYCODE_HOME);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"模拟按键完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟点击屏幕</span>
agent.click(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"屏幕点击完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟滑动操作</span>
agent.swip(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">50</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">200</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">10</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"滑动操作完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// AI模拟手工滑动</span>
agent.swipAI(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">500</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"AI滑动操作完成"</span>);


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟触摸</span>
agent.touchDown(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">100</span>);
agent.touchMove(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">200</span>, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">200</span>);
agent.touchUp(<span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"触摸操作完成"</span>);
</code><div><br></div></pre><p></p>
页: [1]
查看完整版本: AIWORK的界面交互: 返回键功能实现