Skip to content

Commit

Permalink
jsKeyToFcitxString
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Sep 4, 2024
1 parent cbcd481 commit 8e81cca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions page/Fcitx5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface FCITX {
updateStatusArea: () => void
getConfig: (uri: string) => Config
setConfig: (uri: string, json: object) => void
jsKeyToFcitxString: (event: KeyboardEvent) => string
}

export const fcitxReady: Promise<null>
3 changes: 2 additions & 1 deletion page/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Module from './module'
import { blur, clickPanel, focus } from './focus'
import { keyEvent } from './keycode'
import { jsKeyToFcitxString, keyEvent } from './keycode'
import { commit, hidePanel, placePanel, setPreedit } from './client'
import { currentInputMethod, getAllInputMethods, getInputMethods, setCurrentInputMethod, setInputMethods } from './input-method'
import { getConfig, setConfig } from './config'
Expand Down Expand Up @@ -43,6 +43,7 @@ window.fcitx = {
getAllInputMethods,
getConfig,
setConfig,
jsKeyToFcitxString,
enable() {
document.addEventListener('focus', focus, true)
document.addEventListener('blur', blur, true)
Expand Down
30 changes: 23 additions & 7 deletions page/keycode.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import Module from './module'
import { getInputElement } from './focus'

export function keyEvent(event: KeyboardEvent) {
if (!getInputElement()) {
return
}
const isRelease = event.type === 'keyup'
function extract(event: KeyboardEvent): [string, string, number] | undefined {
const { key, code, shiftKey, altKey, ctrlKey, metaKey } = event
// Host IME
if (key === 'Process') {
return
return undefined
}
const capsLock = event.getModifierState('CapsLock')
const modifiers = Number(shiftKey) | Number(capsLock) << 1 | Number(ctrlKey) << 2 | Number(altKey) << 3 | Number(metaKey) << 6
if (Module.ccall('process_key', 'bool', ['string', 'string', 'number', 'bool'], [key, code, modifiers, isRelease])) {
return [key, code, modifiers]
}

export function keyEvent(event: KeyboardEvent) {
if (!getInputElement()) {
return
}
const extracted = extract(event)
if (!extracted) {
return
}
const isRelease = event.type === 'keyup'
if (Module.ccall('process_key', 'bool', ['string', 'string', 'number', 'bool'], [...extracted, isRelease])) {
event.preventDefault()
}
}

export function jsKeyToFcitxString(event: KeyboardEvent) {
const extracted = extract(event)
if (!extracted) {
return ''
}
return Module.ccall('js_key_to_fcitx_string', 'string', ['string', 'string', 'number'], extracted)
}
10 changes: 10 additions & 0 deletions src/keycode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "keycode.h"
#include <emscripten.h>
#include <fcitx-utils/log.h>

namespace fcitx {
Expand Down Expand Up @@ -224,4 +225,13 @@ Key js_key_to_fcitx_key(const std::string &key, const std::string &code,
return Key{js_key_to_fcitx_keysym(key, code), KeyStates{modifiers},
js_keycode_to_fcitx_keycode(code)};
}

extern "C" {
EMSCRIPTEN_KEEPALIVE const char *
js_key_to_fcitx_string(const char *key, const char *code, uint32_t modifiers) {
static std::string ret;
ret = js_key_to_fcitx_key(key, code, modifiers).normalize().toString();
return ret.c_str();
}
}
} // namespace fcitx

0 comments on commit 8e81cca

Please sign in to comment.