KeyHandler & InputHandler // Refuse non-ANSI charCodes.

This commit is contained in:
ShikiSuen 2022-05-09 12:04:15 +08:00
parent d8c2f668d2
commit bd65f2c000
2 changed files with 59 additions and 4 deletions

View File

@ -61,16 +61,16 @@ enum KeyCode: UInt16 {
case kF8 = 100 case kF8 = 100
case kF9 = 101 case kF9 = 101
case kF11 = 103 case kF11 = 103
case kF13 = 105 case kF13 = 105 // PrtSc
case kF16 = 106 case kF16 = 106
case kF14 = 107 case kF14 = 107
case kF10 = 109 case kF10 = 109
case kF12 = 111 case kF12 = 111
case kF15 = 113 case kF15 = 113
case kHelp = 114 case kHelp = 114 // Insert
case kHome = 115 case kHome = 115
case kPageUp = 116 case kPageUp = 116
case kWindowDelete = 117 // Renamed from "kForwardDelete" to avoid nomenclatural confusions. case kWindowsDelete = 117 // Renamed from "kForwardDelete" to avoid nomenclatural confusions.
case kF4 = 118 case kF4 = 118
case kEnd = 119 case kEnd = 119
case kF2 = 120 case kF2 = 120
@ -82,6 +82,33 @@ enum KeyCode: UInt16 {
case kUpArrow = 126 case kUpArrow = 126
} }
enum KeyCodeBlackListed: UInt16 {
case kF17 = 64
case kVolumeUp = 72
case kVolumeDown = 73
case kMute = 74
case kF18 = 79
case kF19 = 80
case kF20 = 90
case kF5 = 96
case kF6 = 97
case kF7 = 98
case kF3 = 99
case kF8 = 100
case kF9 = 101
case kF11 = 103
case kF13 = 105 // PrtSc
case kF16 = 106
case kF14 = 107
case kF10 = 109
case kF12 = 111
case kF15 = 113
case kHelp = 114 // Insert
case kF4 = 118
case kF2 = 120
case kF1 = 122
}
// CharCodes: https://theasciicode.com.ar/ascii-control-characters/horizontal-tab-ascii-code-9.html // CharCodes: https://theasciicode.com.ar/ascii-control-characters/horizontal-tab-ascii-code-9.html
enum CharCode: UInt /* 16 */ { enum CharCode: UInt /* 16 */ {
case yajuusenpai = 114_514_191_191_810_893 case yajuusenpai = 114_514_191_191_810_893
@ -172,6 +199,26 @@ class InputHandler: NSObject {
"<\(super.description) inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>" "<\(super.description) inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>"
} }
// ANSI charCode Swift KeyHandler
var isInvalidInput: Bool {
switch charCode {
case 0x20...0xFF: // ANSI charCode
return false
default:
if isReservedKey, !isKeyCodeBlacklisted {
return false
}
return true
}
}
var isKeyCodeBlacklisted: Bool {
guard let code = KeyCodeBlackListed(rawValue: keyCode) else {
return false
}
return code.rawValue != KeyCode.kNone.rawValue
}
var isShiftHold: Bool { var isShiftHold: Bool {
flags.contains([.shift]) flags.contains([.shift])
} }
@ -269,7 +316,7 @@ class InputHandler: NSObject {
} }
var isDelete: Bool { var isDelete: Bool {
KeyCode(rawValue: keyCode) == KeyCode.kWindowDelete KeyCode(rawValue: keyCode) == KeyCode.kWindowsDelete
} }
var isCursorBackward: Bool { var isCursorBackward: Bool {

View File

@ -46,6 +46,14 @@ extension KeyHandler {
return false return false
} }
// Megrez
if input.isInvalidInput {
IME.prtDebugIntel("550BCF7B: KeyHandler just refused an invalid input.")
errorCallback()
stateCallback(state)
return true
}
// Ignore the input if the composing buffer is empty with no reading // Ignore the input if the composing buffer is empty with no reading
// and there is some function key combination. // and there is some function key combination.
let isFunctionKey: Bool = let isFunctionKey: Bool =