<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[键鼠助手 5.20 新增内置函数 GetUpTime()：给宏脚本一个毫秒时钟（用法与示例）]]></title><description><![CDATA[<p dir="auto">这篇说明 5.20 版本新增的内置函数 <strong><code>GetUpTime()</code></strong>：宏脚本里第一个「读时间」的接口。<br />
它没有参数，返回<strong>设备开机以来经过的毫秒数</strong>，用来计量耗时、给动作做绝对时间对齐。</p>
<blockquote>
<p dir="auto">相关： <a href="https://phubber.com/topic/8">键鼠助手 5.0 资源汇总：主程序 / 文档 / 配套工具下载</a></p>
</blockquote>
<h2>一句话用法</h2>
<pre><code class="language-lua">local t = GetUpTime()   -- 单位毫秒，整型；返回值一般只用来算差值
</code></pre>
<h2>接口说明</h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>函数</th>
<th>参数</th>
<th>返回值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>GetUpTime()</code></td>
<td>无</td>
<td>整型</td>
<td>设备开机以来经过的毫秒数</td>
</tr>
</tbody>
</table>
<p dir="auto">几点需要说清楚：</p>
<ul>
<li><strong>不是「当前时间」</strong>：它是「开机以来的时长」，设备上电时从 0 开始，不表示几点几分，也不是时间戳。它的用途是<strong>算差值</strong>。</li>
<li><strong>单位是毫秒，返回值是整型</strong>：脚本里 <code>math.type(GetUpTime())</code> 得到 <code>integer</code>，可以直接参与加减乘除与比较。</li>
<li><strong>分辨率 1 毫秒</strong>：设备内部按更细的步长计数，换算成毫秒时向下取整，所以相邻两次调用可能返回同一个值。</li>
<li><strong>不会溢出</strong>：毫秒计数是 64 位整数，连续开机到溢出需要以「亿年」计，正常使用无需关心。</li>
<li><strong>大小写不敏感</strong>：<code>GetUpTime()</code> 与 <code>getuptime()</code> 是同一个函数，和 <code>IsKeyDown</code> / <code>iskeydown</code> 的规则一致。</li>
<li><strong>它是「读时钟」，不是「等待」</strong>：调用本身几乎不耗时、不会阻塞宏；要等一段时间请用 <code>Delay(毫秒)</code>。</li>
</ul>
<h2>在电脑上先调好再上机：两种环境的时间原点不同</h2>
<p dir="auto">同一个 <code>GetUpTime()</code>，在两个地方跑，时间原点不一样：</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>运行环境</th>
<th>时间原点</th>
<th>典型用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>编辑器 →「脚本校验」</td>
<td>本次校验开始的时刻</td>
<td>在电脑上先确认脚本的节奏与耗时</td>
</tr>
<tr>
<td>设备（真机）</td>
<td>设备本次上电的时刻</td>
<td>真实的宏执行计时</td>
</tr>
</tbody>
</table>
<p dir="auto">所以<strong>不要跨环境比较绝对数值</strong>（电脑上的 1200 和真机上的 1200 不是一个起点），只比较<strong>同一个环境内的差值</strong>。</p>
<h2>用法一：测量一段宏的耗时</h2>
<p dir="auto">取两次时间戳相减，就能知道中间这段动作真实花了多久 —— 排查「宏为什么比预期慢」时很有用。</p>
<pre><code class="language-lua">local t0 = GetUpTime()

KeyTap("a")
Delay(300)
KeyTap("b")

Log("这一段耗时 %d ms", GetUpTime() - t0)
</code></pre>
<blockquote>
<p dir="auto"><code>Log</code> 的首个参数带 <code>%d</code> 时按 printf 格式化，数字会原样打印出来，便于对照。</p>
</blockquote>
<h2>用法二：用绝对时间对齐，避免 Delay 累积误差</h2>
<p dir="auto">连续多次 <code>Delay(500)</code> 时，每一步自身的执行耗时会让整体越走越慢，误差逐次累加。<br />
改用「目标时刻」判断，就能把每轮的落点重新拉回时间轴上：</p>
<pre><code class="language-lua">-- 执行 4 轮，每轮之间间隔 500ms，落点分别对齐到 500 / 1000 / 1500 / 2000ms
local t0 = GetUpTime()
for i = 1, 4 do
    KeyTap("f")
    local target = i * 500
    while GetUpTime() - t0 &lt; target do
        Delay(10)   -- 小步让出，别用空转等待
    end
end
</code></pre>
<p dir="auto">要点：等待循环里一定要有 <code>Delay(小值)</code>，不要把 <code>while</code> 写成不带 <code>Delay</code> 的紧凑空转。</p>
<h2>用法三：把宏做成固定的节拍</h2>
<p dir="auto">需要「每 1 秒做一件事、共 5 次」这类节奏时，用时间戳做判断比用 <code>Delay</code> 堆叠更稳：</p>
<pre><code class="language-lua">local last = GetUpTime()
local count = 0
while count &lt; 5 do
    local now = GetUpTime()
    if now - last &gt;= 1000 then
        last = last + 1000      -- 以理论落点累加，不丢节拍
        KeyTap("space")
        count = count + 1
    end
    Delay(10)
end
</code></pre>
<blockquote>
<p dir="auto"><code>last = last + 1000</code>（而不是 <code>last = now</code>）能让误差不累积：即使某轮判定晚了一点，下一轮的落点仍然回到原来的时间轴上。</p>
</blockquote>
<h2>版本要求与兼容性</h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>组合</th>
<th>结果</th>
</tr>
</thead>
<tbody>
<tr>
<td>5.20 软件 + 配套固件</td>
<td>正常使用</td>
</tr>
<tr>
<td>5.20 之前的软件</td>
<td>编译不通过：<code>GetUpTime</code> 会被当成未定义的函数</td>
</tr>
<tr>
<td>5.20 软件 + 更早的固件</td>
<td>宏在设备上执行到该函数时会失败中断（不会静默返回 0）</td>
</tr>
</tbody>
</table>
<p dir="auto">所以升级软件时，请把<strong>固件一并升到配套版本</strong>。软件与固件的版本对应关系，参考资源汇总帖里的说明。</p>
<h2>还是有问题？</h2>
<p dir="auto">在本帖下回复，写清楚：<strong>软件版本</strong>（关于页的 <code>Software version</code>）+ <strong>固件版本</strong> + 你脚本里调用 <code>GetUpTime()</code> 的那几行 + 现象。</p>
]]></description><link>https://phubber.com/topic/11/键鼠助手-5.20-新增内置函数-getuptime-给宏脚本一个毫秒时钟-用法与示例</link><generator>RSS for Node</generator><lastBuildDate>Fri, 16 Oct 2026 15:04:49 GMT</lastBuildDate><atom:link href="https://phubber.com/topic/11.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Oct 2026 18:41:09 GMT</pubDate><ttl>60</ttl></channel></rss>