<?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[把一串重复操作压成一次按键]]></title><description><![CDATA[<p dir="auto">快捷键序列是宏最本分的用法：一个动作要按四次组合键、还得按固定顺序，那就把它压成一次。</p>
<h2>基本结构：按住、敲、松开</h2>
<p dir="auto">宏里的组合键不能写成 <code>Ctrl+C</code> 这种字符串，要拆成三步：</p>
<pre><code class="language-lua">KeyDown("lctrl")
KeyTap("c")
KeyUp("lctrl")
</code></pre>
<p dir="auto"><code>KeyTap</code> 本身没有独立的底层动作，它就是三条子命令的组合：</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>写法</th>
<th>实际执行</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>KeyTap("c")</code></td>
<td><code>KeyDown("c")</code> → <code>Delay(20)</code> → <code>KeyUp("c")</code></td>
</tr>
</tbody>
</table>
<p dir="auto">所以上面三行等于「按住 Ctrl，敲一下 C，松开 Ctrl」——那 20ms 的按下-松开正好夹在 <code>KeyDown</code> 与 <code>KeyUp</code> 之间，Ctrl 全程没被松开过。这也是组合键必须写成三段式的原因：真正让两个键<strong>同时</strong>处于按下状态的是 <code>KeyDown</code> 和 <code>KeyUp</code> 的位置，不是 <code>KeyTap</code>。</p>
<p dir="auto">一个容易想错的地方：<strong><code>KeyTap</code> 传多个参数是逐个串行点击，不是同时按下。</strong></p>
<pre><code class="language-lua">KeyTap("a", "b")   -- a 按下 → 20ms → a 松开 → b 按下 → 20ms → b 松开
</code></pre>
<p dir="auto">两个键从不同时按住，总耗时 40ms 起，键越多叠得越多。要两个键同时按住，用 <code>KeyDown("a", "b")</code>；<code>KeyTap</code> 的多参数只在「连续敲几个键」时才有意义。</p>
<p dir="auto"><strong>修饰键必须先按下、后松开，中间夹目标键</strong>。写成 <code>KeyTap("c")</code> 然后 <code>KeyTap("lctrl")</code> 是无效的——那是先打了 c 再按了一下 Ctrl。</p>
<h2>一个真实例子：整列搬到另一张表</h2>
<p dir="auto">Excel 里常见的动作序列：选中当前列 → 复制 → 切到另一个表 → 定位 → 粘贴。</p>
<pre><code class="language-lua">-- 选中整列（Ctrl + 空格），复制
KeyDown("lctrl")
KeyTap("spacebar")
KeyUp("lctrl")
Delay(60)

KeyDown("lctrl")
KeyTap("c")
KeyUp("lctrl")
Delay(120)             -- 复制到剪贴板需要时间，别省

-- 切换到下一个工作表
KeyDown("lctrl")
KeyTap("pagedown")
KeyUp("lctrl")
Delay(250)             -- 切表比一般操作慢，给足

-- 跳到 A1 并粘贴
KeyDown("lctrl")
KeyTap("home")
KeyUp("lctrl")
Delay(60)

KeyDown("lctrl")
KeyTap("v")
KeyUp("lctrl")
</code></pre>
<h2>延时怎么给</h2>
<p dir="auto">这张表是按「目标软件的响应速度」排的，不是按理论值：</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>动作</th>
<th>建议延时</th>
<th>原因</th>
</tr>
</thead>
<tbody>
<tr>
<td>普通快捷键</td>
<td>60–80ms</td>
<td>界面刷新一帧就够</td>
</tr>
<tr>
<td>复制 / 剪切</td>
<td>100–150ms</td>
<td>剪贴板写入是异步的</td>
</tr>
<tr>
<td>切换工作表 / 标签页</td>
<td>200–300ms</td>
<td>整页重绘</td>
</tr>
<tr>
<td>打开对话框</td>
<td>300–500ms</td>
<td>等窗口出来再输入</td>
</tr>
<tr>
<td>保存 / 导出</td>
<td>800ms 以上</td>
<td>磁盘 IO，不可控</td>
</tr>
</tbody>
</table>
<p dir="auto">原则：<strong>给接收方的耗时永远比你估计的长</strong>。宁多给 50ms（人感觉不出来），也不要少给导致动作串到上一个状态上。</p>
<h2><code>ReleaseAllKeys</code> 什么时候用</h2>
<p dir="auto">它释放所有键盘按键。用在这些场合：</p>
<ol>
<li><strong>宏结束前兜底</strong>。万一中间某步异常退出，修饰键会一直处于按下状态，用户感觉到的是「Ctrl 粘住了」。脚本末尾加一行 <code>ReleaseAllKeys()</code> 可以避免。</li>
<li><strong>两种操作模式之间切换</strong>。前一段是「按住 Shift 连续点」，后一段是纯点击，中间清一次状态比较干净。</li>
</ol>
<pre><code class="language-lua">-- 末尾兜底
ReleaseAllKeys()
</code></pre>
<p dir="auto">它不需要参数，也不会有副作用——没有按下的键，调用它什么也不做。</p>
<h2>配合场景切换用</h2>
<p dir="auto">一套宏通常不止一个用途：白天办公、晚上别的用途。10 个场景就是为这个准备的。</p>
<ul>
<li>每个场景可以配自己的切换快捷键，<strong>纯硬件切换</strong>（不经过电脑软件）；</li>
<li>场景互相独立，同一个键在不同场景可以绑不同的宏；</li>
<li>偏好页里可以给场景命名，只存在本地，方便自己认。</li>
</ul>
<p dir="auto">所以「办公宏」和「其他宏」不用挤在一个剧本里靠 <code>if</code> 判断，直接分场景更干净。</p>
<h2>三个容易忽略的细节</h2>
<p dir="auto"><strong>1. 目标窗口必须先获得焦点。</strong> 宏只发按键，不切窗口。如果触发时焦点在别的程序上，这一串按键就全打到别的程序里了。要么手动先点一下，要么把「点击目标窗口」写进宏的第一步（<code>MouseClick</code> 到窗口标题栏）。</p>
<p dir="auto"><strong>2. 中文输入法会吃掉字母键。</strong> 输入法处于中文状态时，<code>KeyTap("c")</code> 可能变成候选词输入。涉及字母键的序列，在脚本开头把输入法切到英文，或者要求使用者在触发前切好。</p>
<p dir="auto"><strong>3. 撤销是最好的兜底。</strong> 序列里某一步没生效，后面的动作会作用在错误状态上，可能产生难以回退的后果（比如批量删除了内容）。在开头的注释里写清「出问题按 Ctrl+Z 撤回」，比任何代码都实用。</p>
]]></description><link>https://www.byteohm.com/topic/55/把一串重复操作压成一次按键</link><generator>RSS for Node</generator><lastBuildDate>Thu, 19 Nov 2026 06:23:35 GMT</lastBuildDate><atom:link href="https://www.byteohm.com/topic/55.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Nov 2026 16:46:07 GMT</pubDate><ttl>60</ttl></channel></rss>