<?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[一键输入常用长文本：TypeText 的两种模式]]></title><description><![CDATA[<p dir="auto">地址、邮箱、公司抬头、客服模板句、报税编号——这些内容每次都要一个字一个字敲，是办公场景里最典型的浪费。<code>TypeText</code> 就是干这个的。</p>
<h2>基本写法</h2>
<pre><code class="language-lua">-- 把常改的部分提到最前面
local ADDRESS = "北京市海淀区中关村大街 1 号 A 座 1201"

TypeText(ADDRESS)
</code></pre>
<p dir="auto">编译期它会被展开成一串按键动作——<code>TypeText</code> 不是运行时逐个字符打出来的，而是<strong>编译时就把文本编码成 Alt + 小键盘（或 Option + HEX）序列</strong>。这一点决定了它的适用范围，后面细说。</p>
<h2>Windows：默认 GBK，用 Alt + 小键盘</h2>
<pre><code class="language-lua">TypeText("你好")                -- 等价于 mode="gbk"
TypeText("你好😀", "unicode")   -- Emoji 走 Unicode 码点
</code></pre>
<ul>
<li>GBK 覆盖常用汉字，编码短、按键次数少；</li>
<li>Emoji、生僻字、日文假名之外的特殊符号，GBK 编不了，必须显式给 <code>"unicode"</code>。</li>
</ul>
<p dir="auto"><strong>前提条件</strong>，三条缺一不可：</p>
<ol>
<li><strong>小键盘可用</strong>。Alt + 小键盘数字是靠 <code>num0</code>–<code>num9</code> 打的，笔记本没有独立小键盘、NumLock 关着的，全部打不出来；</li>
<li><strong>输入法处于英文 / 直连状态</strong>。中文输入法开着的时候，Alt + 数字可能被输入法截走；</li>
<li><strong>目标程序接受 Alt 组合键</strong>。极少数程序把 Alt 当菜单键，会在输入过程中弹出菜单栏。</li>
</ol>
<h2>macOS：默认 Unicode，用 Option + HEX</h2>
<pre><code class="language-lua">TypeText("你好")   -- 等价于 mode="unicode"，展开为 Option + HEX
</code></pre>
<p dir="auto">用之前必须先在 <strong>系统设置 → 键盘 → 输入法 / 输入源</strong> 里启用并切换到 <strong>Unicode Hex Input</strong>。</p>
<p dir="auto">macOS 不支持 <code>mode="gbk"</code>——GBK 机内码输入属于 Windows 输入体系，在 Mac 上没有对应机制。</p>
<h2>长度：这是最容易踩的坑</h2>
<p dir="auto">因为编译期展开，文本长度直接换算成按键次数。一个汉字在 GBK 下是 2 字节，每个字节展开成「Alt + 4 位数字 + 松开」，大约 6 次按键动作。</p>
<p dir="auto">粗算一下：</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>文本长度</th>
<th>大致按键动作数</th>
<th>实际耗时</th>
</tr>
</thead>
<tbody>
<tr>
<td>10 个汉字</td>
<td>约 120 次</td>
<td>1–2 秒</td>
</tr>
<tr>
<td>50 个汉字（一段地址）</td>
<td>约 600 次</td>
<td>6–10 秒</td>
</tr>
<tr>
<td>500 个汉字（一篇文章）</td>
<td>约 6000 次</td>
<td>一分钟以上，且极易被目标程序截断</td>
</tr>
</tbody>
</table>
<p dir="auto">所以：</p>
<ul>
<li><strong>适合</strong>：地址、邮箱、模板句、编号、十几到几十个字的内容；</li>
<li><strong>不适合</strong>：整段落、整篇文章。真要批量输入正文，用系统的输入法自定义短语或者剪贴板，不要用 <code>TypeText</code>。</li>
</ul>
<p dir="auto">超过 100 字就该重新想方案了。</p>
<h2>一个实用的组合：替换而不是追加</h2>
<p dir="auto">输入前先全选，能保证结果是替换而不是拼在后面：</p>
<pre><code class="language-lua">local TEMPLATE = "您好，您反馈的问题我们已收到，会在 24 小时内处理。"

KeyDown("lctrl")
KeyTap("a")
KeyUp("lctrl")
Delay(80)              -- 等目标程序完成全选
TypeText(TEMPLATE)
</code></pre>
<p dir="auto"><code>Delay(80)</code> 这一步不要省。全选是异步的，紧接着就输入，前面几个字符可能打进旧选区。</p>
<h2>现场排查</h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>现象</th>
<th>原因</th>
</tr>
</thead>
<tbody>
<tr>
<td>打出来是一串数字，不是文字</td>
<td>小键盘 NumLock 关着，或输入法处于中文状态</td>
</tr>
<tr>
<td>只有前几个字进去，后面丢了</td>
<td>目标程序有输入长度或速率限制，把 <code>TypeText</code> 拆成几段、中间加 <code>Delay</code></td>
</tr>
<tr>
<td>弹出了程序菜单</td>
<td>目标程序把 Alt 当菜单键，换用剪贴板方案</td>
</tr>
<tr>
<td>Emoji 变成问号或方块</td>
<td>没给 <code>"unicode"</code>，GBK 编不出来</td>
</tr>
<tr>
<td>Mac 上完全没反应</td>
<td>没切到 Unicode Hex Input 输入源</td>
</tr>
</tbody>
</table>
<h2>什么时候不该用它</h2>
<p dir="auto">如果目标是「把一段固定内容填进某个字段」，还有一个更省事的思路：<strong>能不能让目标软件自己读取？</strong> 比如表格的自动填充、Word 的自动图文集、输入法的自定义短语。这些方案不占宏时长，也不受输入法状态影响。</p>
<p dir="auto">宏的价值在于<strong>跨软件、跨界面</strong>、目标程序没有现成功能时的那一下。能用软件自带功能解决的，优先用自带功能。</p>
]]></description><link>https://www.byteohm.com/topic/54/一键输入常用长文本-typetext-的两种模式</link><generator>RSS for Node</generator><lastBuildDate>Thu, 19 Nov 2026 06:23:29 GMT</lastBuildDate><atom:link href="https://www.byteohm.com/topic/54.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Nov 2026 16:46:07 GMT</pubDate><ttl>60</ttl></channel></rss>