@@ -2,12 +2,13 @@ import { webview } from '@kit.ArkWeb';
2
2
import type { InputMethodExtensionContext } from '@kit.IMEKit';
3
3
import { inputMethodEngine } from '@kit.IMEKit';
4
4
import { display } from '@kit.ArkUI';
5
- import { KeyCode } from '@kit.InputKit';
5
+ import { Action, KeyAction, KeyCode, KeyEvent } from '@kit.InputKit';
6
6
import { SystemEvent, VirtualKeyboardEvent } from '../../../fcitx5-keyboard-web/src/api';
7
7
import fcitx, { InputContextState } from 'libentry.so';
8
8
import { FcitxEvent } from './FcitxEvent';
9
9
import { convertCode } from './keycode';
10
10
import { onTextChange, redo, resetStacks, undo } from './TextOperation';
11
+ import { KeyState, statesFromKeyEvent } from './KeyState';
11
12
12
13
const ability: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
13
14
const keyboardDelegate = inputMethodEngine.getKeyboardDelegate() // Physical keyboard
@@ -278,7 +279,7 @@ export class KeyboardController {
278
279
}
279
280
280
281
public handleKey(key: string, keyCode?: number): void {
281
- const res = fcitx.processKey(key ? key.charCodeAt(0) : 0, keyCode ?? 0, false)
282
+ const res = fcitx.processKey(key ? key.charCodeAt(0) : 0, keyCode ?? 0, KeyState.VIRTUAL, false)
282
283
if (!this.processResult(res)) {
283
284
switch (keyCode) { // Check code first as enter has to be handled differently, which has key \r.
284
285
case KeyCode.KEYCODE_DEL:
@@ -391,10 +392,19 @@ export class KeyboardController {
391
392
})
392
393
}
393
394
394
- private physicalKeyEventHandler(e: inputMethodEngine.KeyEvent): boolean {
395
- const isRelease = e.keyAction === 3
396
- const res = fcitx.processKey(0, e.keyCode, isRelease)
397
- return this.processResult(res)
395
+ private physicalKeyEventHandler(e: KeyEvent): boolean {
396
+ const isRelease = e.action.valueOf() === Action.UP
397
+ const states = statesFromKeyEvent(e)
398
+ const charCode = e.unicodeChar
399
+ if (charCode > 0 && charCode !== '\t'.charCodeAt(0) && charCode !== '\n'.charCodeAt(0)) {
400
+ const res = fcitx.processKey(charCode, 0, states, isRelease)
401
+ return this.processResult(res)
402
+ }
403
+ if (e.key.code != KeyCode.KEYCODE_UNKNOWN) {
404
+ const res = fcitx.processKey(0, e.key.code, states, isRelease)
405
+ return this.processResult(res)
406
+ }
407
+ return false
398
408
}
399
409
400
410
private initHelper() {
@@ -414,10 +424,7 @@ export class KeyboardController {
414
424
415
425
private registerListener(): void {
416
426
this.registerInputListener();
417
- keyboardDelegate.on('keyDown', (e) => {
418
- return this.physicalKeyEventHandler(e)
419
- })
420
- keyboardDelegate.on('keyUp', (e) => {
427
+ keyboardDelegate.on('keyEvent', (e) => {
421
428
return this.physicalKeyEventHandler(e)
422
429
})
423
430
// This is not called on focus.
@@ -471,8 +478,7 @@ export class KeyboardController {
471
478
ability.off('inputStart');
472
479
ability.off('inputStop', () => {
473
480
});
474
- keyboardDelegate.off('keyDown')
475
- keyboardDelegate.off('keyUp')
481
+ keyboardDelegate.off('keyEvent')
476
482
keyboardDelegate.off('textChange')
477
483
this.panel?.off('show')
478
484
this.panel?.off('hide')
0 commit comments