feat(input): upgrade graph personalization, english morphology detection, and atomic SendInput replacement

This commit is contained in:
Yun Chan 2026-10-29 08:16:26 +09:00
parent 52eef21bab
commit cd4a452148
21 changed files with 1098 additions and 110 deletions

View file

@ -18,6 +18,8 @@ const logger = getLogger('win32-unicode-input')
const INPUT_KEYBOARD = 1
const KEYEVENTF_KEYUP = 0x0002
const KEYEVENTF_UNICODE = 0x0004
const VK_BACK = 0x08
const VK_SPACE = 0x20
type SendInputFn = (cInputs: number, pInputs: Buffer, cbSize: number) => number
@ -80,6 +82,34 @@ function ensureLoaded(): SendInputFn | null {
}
}
function writeVirtualKeyPair(buffer: Buffer, pairIndex: number, vk: number): void {
const downBase = pairIndex * 2 * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, downBase)
buffer.writeUInt16LE(vk, downBase + unionOffset)
buffer.writeUInt16LE(0, downBase + unionOffset + 2)
buffer.writeUInt32LE(0, downBase + unionOffset + 4)
const upBase = (pairIndex * 2 + 1) * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, upBase)
buffer.writeUInt16LE(vk, upBase + unionOffset)
buffer.writeUInt16LE(0, upBase + unionOffset + 2)
buffer.writeUInt32LE(KEYEVENTF_KEYUP, upBase + unionOffset + 4)
}
function writeUnicodeKeyPair(buffer: Buffer, pairIndex: number, codeUnit: number): void {
const downBase = pairIndex * 2 * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, downBase)
buffer.writeUInt16LE(0, downBase + unionOffset)
buffer.writeUInt16LE(codeUnit, downBase + unionOffset + 2)
buffer.writeUInt32LE(KEYEVENTF_UNICODE, downBase + unionOffset + 4)
const upBase = (pairIndex * 2 + 1) * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, upBase)
buffer.writeUInt16LE(0, upBase + unionOffset)
buffer.writeUInt16LE(codeUnit, upBase + unionOffset + 2)
buffer.writeUInt32LE(KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, upBase + unionOffset + 4)
}
/**
* UTF-16 문자열을 Windows SendInput(KEYEVENTF_UNICODE) 호출 한 번으로 입력 큐에 넣는다.
* 성공 시 true, 지원하지 않거나 실패 시 false 를 반환한다.
@ -95,18 +125,7 @@ export function sendWin32UnicodeText(text: string): boolean {
const buffer = Buffer.alloc(totalEvents * inputStructSize)
for (let i = 0; i < codeUnits; i += 1) {
const code = text.charCodeAt(i)
const downBase = i * 2 * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, downBase)
buffer.writeUInt16LE(0, downBase + unionOffset)
buffer.writeUInt16LE(code, downBase + unionOffset + 2)
buffer.writeUInt32LE(KEYEVENTF_UNICODE, downBase + unionOffset + 4)
const upBase = (i * 2 + 1) * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, upBase)
buffer.writeUInt16LE(0, upBase + unionOffset)
buffer.writeUInt16LE(code, upBase + unionOffset + 2)
buffer.writeUInt32LE(KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, upBase + unionOffset + 4)
writeUnicodeKeyPair(buffer, i, text.charCodeAt(i))
}
const sent = sendInput(totalEvents, buffer, inputStructSize)
@ -116,3 +135,54 @@ export function sendWin32UnicodeText(text: string): boolean {
return false
}
}
/**
* (선택적 IME 조합 확정 Space) + 커서 앞 백스페이스 N번 + UTF-16 문자열 입력을
* 단 한 번의 Windows SendInput 호출로 원자적(atomic)으로 주입한다.
*
* Win32 SendInput 규약상 단일 호출 안의 INPUT 배열 사이에 사용자의 물리 키 입력이 끼어들 수 없으므로,
* 빠른 타이핑 도중에도 지우기와 다시 쓰기가 경쟁하지 않고 < 0.5ms 안에 끝난다.
*/
export function sendWin32UnicodeReplacement(
deleteCount: number,
text: string,
options: { commitComposition?: boolean } = {}
): boolean {
const safeDeleteCount = Math.max(0, Math.trunc(deleteCount))
if (safeDeleteCount === 0 && !text) return true
const sendInput = ensureLoaded()
if (!sendInput || inputStructSize <= 0) return false
try {
const commitSpacePairs = options.commitComposition && safeDeleteCount > 0 ? 1 : 0
const backspacePairs = safeDeleteCount + commitSpacePairs
const textPairs = text.length
const totalPairs = commitSpacePairs + backspacePairs + textPairs
const totalEvents = totalPairs * 2
if (totalEvents === 0) return true
const buffer = Buffer.alloc(totalEvents * inputStructSize)
let pairCursor = 0
if (commitSpacePairs > 0) {
writeVirtualKeyPair(buffer, pairCursor, VK_SPACE)
pairCursor += 1
}
for (let i = 0; i < backspacePairs; i += 1) {
writeVirtualKeyPair(buffer, pairCursor, VK_BACK)
pairCursor += 1
}
for (let i = 0; i < textPairs; i += 1) {
writeUnicodeKeyPair(buffer, pairCursor, text.charCodeAt(i))
pairCursor += 1
}
const sent = sendInput(totalEvents, buffer, inputStructSize)
return sent === totalEvents
} catch (error) {
logger.warn(
`SendInput(atomic replacement) 실행 실패: ${error instanceof Error ? error.message : String(error)}`
)
return false
}
}