From dabe7472ca5f0277b5c345a75873f5872ca8480e Mon Sep 17 00:00:00 2001 From: hunny <306675637@qq.com> Date: Tue, 27 Feb 2024 12:53:48 +0800 Subject: [PATCH] =?UTF-8?q?testime.c=E7=9A=84keydown=E5=92=8CTextInput?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/Index.ets | 68 ++++++++++++++++++- .../src/main/ets/service/InvokeNative.ts | 8 +++ .../src/main/ets/service/ResponseNative.ts | 4 ++ .../entry/src/main/ets/workers/SDLAppWork.ts | 12 ++++ src/core/ohos/SDL_napi.h | 3 +- src/core/ohos/SDL_ohos.cpp | 64 ++++++++++++++++- src/core/ohos/SDL_ohos.h | 1 + src/main/ohos/testdraw2.c | 2 +- 8 files changed, 158 insertions(+), 4 deletions(-) diff --git a/ohos-project/entry/src/main/ets/pages/Index.ets b/ohos-project/entry/src/main/ets/pages/Index.ets index 5c71b7d56..2afaa79bc 100644 --- a/ohos-project/entry/src/main/ets/pages/Index.ets +++ b/ohos-project/entry/src/main/ets/pages/Index.ets @@ -31,6 +31,7 @@ import { setWindowStyle, shouldMinimizeOnFocusLoss, showTextInput, + showTextInputKeyboard, setPointer } from '../service/ResponseNative'; @@ -39,7 +40,9 @@ import { nativePause, nativeResume, onNativeFocusChanged, - nativeSendQuit + nativeSendQuit, + onNativeKeyDown, + onNativeTextInput } from '../service/InvokeNative'; enum NativeState { @@ -58,6 +61,9 @@ struct Index { private isResumedCalled?: Boolean; private context = getContext(this) as common.UIAbilityContext @StorageProp("cursorID") @Watch("setPointer") cursorID: Number = 0; + @State myinput:string ='' + //@State textVisibility :Visibility = Visibility.None + @State textVisibility :Visibility = Visibility.Visible /* Transition to next state */ handleNativeState(): void { @@ -149,6 +155,15 @@ struct Index { showTextInput(argument.x, argument.y, argument.w, argument.h) break; } + case "showTextInputKeyboard": { + if(argument.isshow) + { + this.textVisibility = Visibility.Visible + }else { + this.textVisibility = Visibility.None + } + break; + } case "hideTextInput": { hideTextInput(); break; @@ -255,6 +270,7 @@ struct Index { } build() { + Stack() { Column() { XComponent({ id: CommonConstants.XCOMPONENT_ID, @@ -265,5 +281,55 @@ struct Index { } .width(CommonConstants.FULL_PARENT) .height(CommonConstants.FULL_PARENT) + + .onKeyEvent((event: KeyEvent) => { + let keyCode: number = 0; + if (event.type === KeyType.Down) { + // this.eventType = 'Down' + keyCode = event.keyCode + // this.text = 'KeyType:' + this.eventType + '\nkeyCode:' + event.keyCode + '\nkeyText:' + event.keyText + '\ncode' + keyCode + console.log('keycode = ' + keyCode) + onNativeKeyDown(keyCode) + + } + if (event.type === KeyType.Up) { + // this.eventType = 'Up' + } + }) + + + Column() { + TextArea({text:this.myinput}) + .height('100%') + .width('100%') + .caretColor('#000') + .onChange( + (text:string)=>{ + console.log('text = ' + text) + this.myinput = text + if (this.myinput.match('\r|\n')) { + // console.log("该字符串包含回车"); + this.myinput = '' + } + onNativeTextInput(text.length, this.myinput) + //onNativeTextInput(7, 'ckjGHUw') + } + ) + .visibility(this.textVisibility) + .defaultFocus(true) + } + /* + .onMouse((event:MouseEvent)=>{ + console.log('x = ' + event.x + 'y = ' + event.y) + + }) + + .onTouch((event:TouchEvent)=>{ + console.log('x = ' + event.touches[0].x + 'y = ' + event.touches[0].y) + + }) + */ + + } } } diff --git a/ohos-project/entry/src/main/ets/service/InvokeNative.ts b/ohos-project/entry/src/main/ets/service/InvokeNative.ts index 7df0a5cab..412e513a4 100644 --- a/ohos-project/entry/src/main/ets/service/InvokeNative.ts +++ b/ohos-project/entry/src/main/ets/service/InvokeNative.ts @@ -69,3 +69,11 @@ export function onNativeFocusChanged(focus: Boolean) { sdl.onNativeFocusChanged(focus) console.log("onNativeFocusChanged") } +export function onNativeKeyDown(keyCode: number) { + sdl.keyDown(keyCode) + console.log("keyCode = " + keyCode) +} +export function onNativeTextInput(count :number, textcontent: string) { + sdl.textInput(count, textcontent) + console.log("textcontent = " + textcontent) +} diff --git a/ohos-project/entry/src/main/ets/service/ResponseNative.ts b/ohos-project/entry/src/main/ets/service/ResponseNative.ts index 065c3f015..d4153525a 100644 --- a/ohos-project/entry/src/main/ets/service/ResponseNative.ts +++ b/ohos-project/entry/src/main/ets/service/ResponseNative.ts @@ -51,6 +51,10 @@ export function showTextInput(x: number, y: number, w: number, h: number) { console.log("ShowTextInput"); } +export function showTextInputKeyboard() { + console.log("ShowTextInputKeyboard") +} + export function hideTextInput() { console.log("OHOS_NAPI_HideTextInput") } diff --git a/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts b/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts index 0a8d2d2dc..107564a9f 100644 --- a/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts +++ b/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts @@ -22,6 +22,8 @@ export interface NapiCallback { setWindowStyle(fullscree: boolean): void; + showTextInputKeyboard(isshow: boolean): void;//show virtual keyboard + setOrientation(w: number, h: number, resizable: number, hint: string): void; shouldMinimizeOnFocusLoss(): void; @@ -38,6 +40,16 @@ export interface NapiCallback { let workerPort: ThreadWorkerGlobalScope = worker.workerPort; export class ArkNapiCallback implements NapiCallback { + showTextInputKeyboard(isshow: boolean): void { + const subJson = {} + subJson["isshow"] = isshow + + const Json = {} + Json["title"] = "showTextInputKeyboard" + Json["data"] = subJson + workerPort.postMessage(Json) + } + setTitle(title: string): void { console.log("OHOS_NAPI_SetTitle") const subJson = {} diff --git a/src/core/ohos/SDL_napi.h b/src/core/ohos/SDL_napi.h index dc027e016..4f9067c93 100644 --- a/src/core/ohos/SDL_napi.h +++ b/src/core/ohos/SDL_napi.h @@ -48,7 +48,8 @@ private: static napi_value OHOS_NAPI_RegisterCallback(napi_env env, napi_callback_info info); static napi_value OHOS_NativeSetupNAPI(napi_env env, napi_callback_info info); static napi_value OHOS_SetResourceManager(napi_env env, napi_callback_info info); - static napi_value OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info info); + static napi_value OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info info); + static napi_value OHOS_TextInput(napi_env env, napi_callback_info info); }; } // namespace SDL } // namespace OHOS diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index e903d2260..befd440f9 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -403,6 +403,21 @@ void OHOS_NAPI_SetWindowStyle(SDL_bool fullscreen) napi_get_named_property(napiCallback->env, callback, "setWindowStyle", &jsMethod); napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); } +void OHOS_NAPI_ShowTextInputKeyboard(SDL_bool isshow)//show virtural keyboard +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_value argv[1] = {nullptr}; + + napi_get_boolean(napiCallback->env, isshow, &argv[0]); + + napi_value callback = nullptr; + napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, &callback); + napi_value jsMethod; + napi_get_named_property(napiCallback->env, callback, "showTextInput2", &jsMethod); + napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); +} + // OHOS_NAPI_SetOrientation void OHOS_NAPI_SetOrientation(int w, int h, int resizable, const char *hint) @@ -516,6 +531,52 @@ napi_value SDLNapi::OHOS_OnNativeResize(napi_env env, napi_callback_info info) return nullptr; } +napi_value SDLNapi::OHOS_TextInput(napi_env env, napi_callback_info info) +{ + size_t requireArgc = 2; + size_t argc = 2; + napi_value args[2] = {nullptr}; + char* inputBuffer = nullptr;//存储atkts传过来的文本框的内容 + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + + napi_valuetype valuetype0; + napi_typeof(env, args[0], &valuetype0); + + napi_valuetype valuetype1; + napi_typeof(env, args[1], &valuetype1); + + int bufSize;//atkts传过来的文本框的内容的长度 + napi_get_value_int32(env, args[0], &bufSize); + size_t stringLength = bufSize + 1; + size_t length; + + inputBuffer = new char[stringLength]; + + napi_get_value_string_utf8(env, args[1], inputBuffer, stringLength, &length); + + SDL_Log("OHOS_TextInput %s",inputBuffer); + //SDL_SendKeyboardText(inputBuffer); + + SDL_Event keyEvent; + keyEvent.type = SDL_KEYDOWN; + keyEvent.key.keysym.sym = SDLK_RETURN; + SDL_PushEvent(&keyEvent); + + SDL_Event event; + memset(&event, 0, sizeof(SDL_Event)); // 清空event结构体 + + event.type = SDL_TEXTINPUT; + + strcpy(event.text.text, inputBuffer); + + SDL_PushEvent(&event); // 推送事件到事件队列 + + delete inputBuffer; + return nullptr; +} + + + napi_value SDLNapi::OHOS_KeyDown(napi_env env, napi_callback_info info) { int keycode; @@ -675,7 +736,8 @@ napi_value SDLNapi::Init(napi_env env, napi_value exports) {"registerCallback", nullptr, OHOS_NAPI_RegisterCallback, nullptr, nullptr, nullptr, napi_default, nullptr}, {"nativeSetScreenResolution", nullptr, OHOS_NativeSetScreenResolution, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"onNativeResize", nullptr, OHOS_OnNativeResize, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"onNativeResize", nullptr, OHOS_OnNativeResize, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"textInput", nullptr, OHOS_TextInput, nullptr, nullptr, nullptr, napi_default, nullptr}, {"keyDown", nullptr, OHOS_KeyDown, nullptr, nullptr, nullptr, napi_default, nullptr}, {"keyUp", nullptr, OHOS_KeyUp, nullptr, nullptr, nullptr, napi_default, nullptr}, {"onNativeKeyboardFocusLost", nullptr, OHOS_OnNativeKeyboardFocusLost, nullptr, nullptr, nullptr, napi_default, diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index d0829ae89..e98e47116 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -60,6 +60,7 @@ extern void OHOS_NAPI_ShouldMinimizeOnFocusLoss(int a); extern void OHOS_NAPI_SetTitle(const char *title); extern void OHOS_NAPI_SetWindowStyle(SDL_bool fullscreen); extern void OHOS_NAPI_SetOrientation(int w, int h, int resizable, const char *hint); +extern void OHOS_NAPI_ShowTextInputKeyboard(SDL_bool fullscreen); const char * SDL_OHOSGetInternalStoragePath(); int OHOS_FileOpen(SDL_RWops *ctx, const char *fileName, const char *mode); diff --git a/src/main/ohos/testdraw2.c b/src/main/ohos/testdraw2.c index 6bf70dc8d..529d086ca 100644 --- a/src/main/ohos/testdraw2.c +++ b/src/main/ohos/testdraw2.c @@ -16,7 +16,7 @@ #include "../../core/ohos/SDL_ohos.h" #include #include "SDL.h" -int main(int argc, char *argv[]) { +int main2(int argc, char *argv[]) { SDL_bool ret; OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Test request permission begin."); ret = OHOS_NAPI_RequestPermission("ohos.permission.MICROPHONE"); -- Gitee