import { FrameNode, NodeController, Position } from '@kit.ArkUI'; import { emitter } from '@kit.BasicServicesKit'; import { KeyboardController } from '../model/KeyboardController'; class MyNodeController extends NodeController { private rootNode: FrameNode | null = null; message: string = "Node"; left: FrameNode | null = null; constructor(message: string) { super(); this.message = message; } context?: UIContext; makeNode(context: UIContext): FrameNode | null { let rootNode = new FrameNode(context); this.rootNode = rootNode; this.context = context; return this.rootNode; } } @Entry @Component struct Index { adjustSizeX: number = 0; adjustSizeY: number = 0; adjustSizeWidth: number = 0; adjustSizeHeight: number = 0; adjustSizeMoveStartPos: Position = { x: -1, y: -1 }; private myNodeController: MyNodeController = new MyNodeController("D"); build() { RelativeContainer() { Stack() { NodeContainer(this.myNodeController) .width('100%') .height(300) .backgroundColor('#e2e2e2'); Text('拖动悬浮键盘区域'); }; } .width('100%') .height('100%') .onTouch((event) => { this.adjustSize(event); }); } // 键盘拖动时判断大小位置信息 adjustSize(event: TouchEvent) { if (!event) { return; } const firstTouch = event.touches[0]; let displayX = Math.round(firstTouch.displayX); let displayY = Math.round(firstTouch.displayY); switch (event.type) { // 按下状态 case TouchType.Down: this.adjustSizeMoveStartPos.x = displayX; this.adjustSizeMoveStartPos.y = displayY; this.adjustSizeX = KeyboardController.floatWindowRect.left; this.adjustSizeY = KeyboardController.floatWindowRect.top; this.adjustSizeWidth = KeyboardController.floatWindowRect.right - KeyboardController.floatWindowRect.left; this.adjustSizeHeight = KeyboardController.floatWindowRect.bottom - KeyboardController.floatWindowRect.top; break; case TouchType.Move: let offsetX = displayX - this.adjustSizeMoveStartPos.x; let offsetY = displayY - this.adjustSizeMoveStartPos.y; this.adjustSizeX = this.adjustSizeX + this.getUIContext().vp2px(offsetX); this.adjustSizeY = this.adjustSizeY + this.getUIContext().vp2px(offsetY); this.adjustSizeWidth = 650; this.adjustSizeHeight = 650; if (this.adjustSizeX < 0) { this.adjustSizeX = 0; } if (this.adjustSizeY < 0) { this.adjustSizeY = 0; } // 悬浮键盘被拖动时,记录拖动的坐标位置 let eventData: emitter.EventData = { data: { "x": this.adjustSizeX, "y": this.adjustSizeY, "width": this.adjustSizeWidth, "height": this.adjustSizeHeight } }; KeyboardController.floatWindowRect.left = this.adjustSizeX; KeyboardController.floatWindowRect.top = this.adjustSizeY; KeyboardController.floatWindowRect.right = this.adjustSizeX + this.adjustSizeWidth; KeyboardController.floatWindowRect.bottom = this.adjustSizeY + this.adjustSizeHeight; // 使用emitter传递键盘位置信息 emitter.emit('onResize', eventData); this.adjustSizeMoveStartPos.x = displayX; this.adjustSizeMoveStartPos.y = displayY; break; case TouchType.Cancel: case TouchType.Up: break; } } }
import { Rect } from '@kit.ArkUI'; import { inputMethodEngine, InputMethodExtensionContext } from '@kit.IMEKit'; import { emitter } from '@kit.BasicServicesKit'; // 调用输入法框架的getInputMethodAbility方法获取实例,并由此实例调用输入法框架功能接口 const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); export class KeyboardController { private mContext: InputMethodExtensionContext | undefined = undefined; private panel: inputMethodEngine.Panel | undefined = undefined; public onCreate(context: InputMethodExtensionContext): void { this.mContext = context; this.initWindow(); // 初始化窗口 this.registerListener(); // 注册对输入法框架的事件监听 } // 应用生命周期销毁 public onDestroy(): void { this.unRegisterListener(); // 取消事件监听 if (this.panel) { this.panel.hide(); // 销毁窗口 inputMethodAbility.destroyPanel(this.panel); } if (this.mContext) { this.mContext.destroy(); } } // 初始化窗口 private initWindow(): void { if (this.mContext === undefined) { return; } let panelInfo: inputMethodEngine.PanelInfo = { type: inputMethodEngine.PanelType.SOFT_KEYBOARD, flag: inputMethodEngine.PanelFlag.FLG_FLOATING }; inputMethodAbility.createPanel(this.mContext, panelInfo).then(async (inputPanel: inputMethodEngine.Panel) => { this.panel = inputPanel; if (this.panel) { await this.adjustSize(); await this.panel.setUiContent('InputMethodExtensionAbility/pages/Index'); } }); } async adjustSize() { let x = KeyboardController.floatWindowRect.left; let y = KeyboardController.floatWindowRect.top; let w = KeyboardController.floatWindowRect.right - KeyboardController.floatWindowRect.left; let h = KeyboardController.floatWindowRect.bottom - KeyboardController.floatWindowRect.top; // 设置输入法应用横竖屏大小 this.panel?.adjustPanelRect(inputMethodEngine.PanelFlag.FLG_FLOATING, { landscapeRect: { left: x, top: y, width: w, height: h }, portraitRect: { left: x, top: y, width: w, height: h } }); } public static floatWindowRect: Rect = { left: 325, top: 1300, right: 975, bottom: 1950 }; private registerListener(): void { this.registerInputListener(); // 注册对输入法框架服务的监听 // 注册隐藏键盘事件监听等 } // 注册对输入法框架服务的开启及停止事件监听 private registerInputListener(): void { emitter.on('onResize', async (data: emitter.EventData) => { console.info(`eventData: ${JSON.stringify(data)}`); this.adjustSize(); }); } private unRegisterListener(): void { emitter.off('onResize'); } } const keyboardController = new KeyboardController(); export default keyboardController;