From b41068c0cbd4e48d14d8bc613589dfa3c0a2598c Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 21 May 2022 00:22:50 +0800 Subject: [PATCH] ctlIME // Block unprintable ASCII chars from being committed out. --- Source/Modules/IMEModules/ctlInputMethod.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index 5e677281..bfe39811 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -260,8 +260,20 @@ extension ctlInputMethod { if buffer.isEmpty { return } + + var bufferOutput = "" + + // 防止輸入法輸出不可列印的字元。 + for theChar in buffer { + if let charCode = theChar.utf16.first { + if !(theChar.isASCII && !(charCode.isPrintable())) { + bufferOutput += String(theChar) + } + } + } + (client as? IMKTextInput)?.insertText( - buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound) + bufferOutput, replacementRange: NSRange(location: NSNotFound, length: NSNotFound) ) }