@@ -111,60 +111,62 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
111111 }
112112
113113 public func keyPressed( _ key: String , _ code: String ) {
114+ processKey ( key, code)
115+ }
116+
117+ public func forwardKey( _ key: String , _ code: String ) {
114118 // documentContextBeforeInput could be all text or text in current line before cursor.
115119 // In the latter case, it will be '\n' if caret is at the beginning of a non-first line.
116- if !processKey( key, code) {
117- switch code {
118- case " ArrowDown " :
119- let offset = lengthOfLastLine ( textDocumentProxy. documentContextBeforeInput ?? " " )
120- let step = lengthOfFirstLine ( textDocumentProxy. documentContextAfterInput ?? " " )
121- textDocumentProxy. adjustTextPosition ( byCharacterOffset: step)
122- DispatchQueue . main. async {
123- // Move to the start of next line if exists.
124- self . textDocumentProxy. adjustTextPosition ( byCharacterOffset: 1 )
125- // Must have a delay, otherwise nextLineLength is always 0.
126- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
127- let nextLineLength = lengthOfFirstLine (
128- self . textDocumentProxy. documentContextAfterInput ?? " " )
129- self . textDocumentProxy. adjustTextPosition (
130- byCharacterOffset: min ( offset, nextLineLength) )
131- }
120+ switch code {
121+ case " ArrowDown " :
122+ let offset = lengthOfLastLine ( textDocumentProxy. documentContextBeforeInput ?? " " )
123+ let step = lengthOfFirstLine ( textDocumentProxy. documentContextAfterInput ?? " " )
124+ textDocumentProxy. adjustTextPosition ( byCharacterOffset: step)
125+ DispatchQueue . main. async {
126+ // Move to the start of next line if exists.
127+ self . textDocumentProxy. adjustTextPosition ( byCharacterOffset: 1 )
128+ // Must have a delay, otherwise nextLineLength is always 0.
129+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
130+ let nextLineLength = lengthOfFirstLine (
131+ self . textDocumentProxy. documentContextAfterInput ?? " " )
132+ self . textDocumentProxy. adjustTextPosition (
133+ byCharacterOffset: min ( offset, nextLineLength) )
132134 }
133- case " ArrowLeft " :
134- textDocumentProxy . adjustTextPosition ( byCharacterOffset : - 1 )
135- case " ArrowRight " :
136- textDocumentProxy . adjustTextPosition ( byCharacterOffset : 1 )
137- case " ArrowUp " :
138- let offset = lengthOfLastLine ( textDocumentProxy . documentContextBeforeInput ?? " " )
139- textDocumentProxy. adjustTextPosition ( byCharacterOffset : - offset )
140- DispatchQueue . main . async {
141- // Move to the end of previous line if exists.
142- self . textDocumentProxy . adjustTextPosition ( byCharacterOffset : - 1 )
143- // Must have a delay, otherwise previousLineLength may always be 0.
144- DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 0.1 ) {
145- let previousLineLength = lengthOfLastLine (
146- self . textDocumentProxy . documentContextBeforeInput ?? " " )
147- if previousLineLength > offset {
148- self . textDocumentProxy . adjustTextPosition (
149- byCharacterOffset : - ( previousLineLength - offset ) )
150- }
135+ }
136+ case " ArrowLeft " :
137+ textDocumentProxy . adjustTextPosition ( byCharacterOffset : - 1 )
138+ case " ArrowRight " :
139+ textDocumentProxy . adjustTextPosition ( byCharacterOffset : 1 )
140+ case " ArrowUp " :
141+ let offset = lengthOfLastLine ( textDocumentProxy. documentContextBeforeInput ?? " " )
142+ textDocumentProxy . adjustTextPosition ( byCharacterOffset : - offset )
143+ DispatchQueue . main . async {
144+ // Move to the end of previous line if exists.
145+ self . textDocumentProxy . adjustTextPosition ( byCharacterOffset : - 1 )
146+ // Must have a delay, otherwise previousLineLength may always be 0.
147+ DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 0.1 ) {
148+ let previousLineLength = lengthOfLastLine (
149+ self . textDocumentProxy . documentContextBeforeInput ?? " " )
150+ if previousLineLength > offset {
151+ self . textDocumentProxy . adjustTextPosition (
152+ byCharacterOffset : - ( previousLineLength - offset ) )
151153 }
152154 }
153- case " Backspace " :
154- textDocumentProxy . deleteBackward ( )
155- updateTextIsEmpty ( )
156- case " End " :
157- let textAfter = textDocumentProxy . documentContextAfterInput ?? " "
158- textDocumentProxy. adjustTextPosition ( byCharacterOffset : lengthOfFirstLine ( textAfter ) )
159- case " Enter " :
160- commitString ( " \n " ) // \r doesn't work in Safari address bar.
161- case " Home " :
162- let textBefore = textDocumentProxy . documentContextBeforeInput ?? " "
163- textDocumentProxy. adjustTextPosition ( byCharacterOffset : - lengthOfLastLine ( textBefore ) )
164- default :
165- if !key . isEmpty {
166- commitString ( key)
167- }
155+ }
156+ case " Backspace " :
157+ textDocumentProxy . deleteBackward ( )
158+ updateTextIsEmpty ( )
159+ case " End " :
160+ let textAfter = textDocumentProxy. documentContextAfterInput ?? " "
161+ textDocumentProxy . adjustTextPosition ( byCharacterOffset : lengthOfFirstLine ( textAfter ) )
162+ case " Enter " :
163+ commitString ( " \n " ) // \r doesn't work in Safari address bar.
164+ case " Home " :
165+ let textBefore = textDocumentProxy. documentContextBeforeInput ?? " "
166+ textDocumentProxy . adjustTextPosition ( byCharacterOffset : - lengthOfLastLine ( textBefore ) )
167+ default :
168+ if ! key. isEmpty {
169+ commitString ( key )
168170 }
169171 }
170172 }
0 commit comments