Skip to main content
Version: v3+

keyboard API

Start from API_LEVEL 4.2. Please refer to API_LEVEL.

The keyboard API provides rich input interface capabilities, greatly simplifying the development complexity of custom keyboards.

Overview

The Keyboard API includes the following main functional modules:

  • Input: input field management, cursor and text operations
  • Keyboard switching
  • Input buffer management
  • System integration

Import

import { keyboard } from '@zos/ui'

API List

1. Input Field Area

setContentRect(rect)

Set the size and position of the input field.

Type
(rect: object) => void
Parameters
ParameterDescriptionRequiredType
rectObject containing x, y, w, h propertiesYESobject
Code Example
keyboard.setContentRect({
x: 10,
y: 20,
w: 300,
h: 310
})

getContentRect()

Get the size and position of the input area.

Type
() => {x: number, y: number, w: number, h: number}
Code Example
let rect = keyboard.getContentRect()
console.log(rect.x, rect.y, rect.w, rect.h)

2. Input Field Text Operations

getTextContext()

Get the text content of the current input field.

Type
() => string
Code Example
const text = keyboard.getTextContext()

inputText(text)

Insert text at the current cursor position.

Type
(text: string) => void
Parameters
ParameterDescriptionRequiredType
textText to be insertedYESstring
Code Example
keyboard.inputText('yesterday')

backspace(count)

Delete a specified number of characters.

Type
(count?: number) => void
Parameters
ParameterDescriptionRequiredTypeDefault
countNumber of characters to deleteNOnumber1
Code Example
keyboard.backspace() // Delete 1 character
keyboard.backspace(4) // Delete 4 characters

clearInput()

Clear the input field content.

Type
() => void
Code Example
keyboard.clearInput()

3. Special Key Operations

sendFnKey(keyType)

Send special function key events.

Type
(keyType: number) => void
Parameters
ParameterDescriptionRequiredType
keyTypeKey type constantYESnumber
Key Type Constants
ConstantDescription
keyboard.BACKSPACEBackspace delete
keyboard.ENTERConfirm/submit input
keyboard.SWITCHSwitch keyboard input method
keyboard.SELECTEnter keyboard selection
Code Example
keyboard.sendFnKey(keyboard.BACKSPACE)
keyboard.sendFnKey(keyboard.ENTER)

4. Cache Management

inputBuffer(text, color, underlineColor)

Set input buffer content.

Type
(text: string, color?: number, underlineColor?: number) => void
Parameters
ParameterDescriptionRequiredTypeDefault
textBuffer textYESstring-
colorText colorNOnumber0xffffff
underlineColorUnderline colorNOnumber0xffffff
Code Example
keyboard.inputBuffer('国')
keyboard.inputBuffer('me', 0xff00ff, 0x00ff00)

getBuffer()

Read buffer text.

Type
() => string
Code Example
let text = keyboard.getBuffer()

clearBuffer()

Clear buffer text.

Type
() => void
Code Example
keyboard.clearBuffer()

5. Keyboard Switching

switchInputType(inputType)

Switch to a specified type of keyboard.

Type
(inputType: number) => void
Parameters
ParameterDescriptionRequiredType
inputTypeInput type constantYESnumber
Input Type Constants
ConstantDescription
inputType.EMOJIEmoji keyboard
inputType.NUMNumber keyboard
inputType.CHARCharacter keyboard
inputType.VOICEVoice input
inputType.JSKBCustom keyboard
Code Example
keyboard.switchInputType(inputType.NUM)

6. System Integration

checkVoiceInputAvailable()

Check if voice input is supported.

Type
() => boolean
Code Example
let voiceSupport = keyboard.checkVoiceInputAvailable()

isEnabled()

Check if the current keyboard is enabled in settings.

Type
() => boolean
Code Example
let enable = keyboard.isEnabled()

isSelected()

Check if the current keyboard is selected for use.

Type
() => boolean
Code Example
let select = keyboard.isSelected()

gotoSettings()

Navigate to the keyboard settings page in system settings: System Settings -> Preferences -> Keyboard.

Type
() => void
Code Example
keyboard.gotoSettings()

Code Example

import { keyboard, inputType } from '@zos/ui'

// Keyboard Widget usage example
DataWidget({
build() {
// Set input area
keyboard.setContentRect({
x: 10,
y: 20,
w: 300,
h: 310
})

// Check keyboard status
if (keyboard.isEnabled()) {
console.log('Keyboard is enabled')
}

// Handle text input
keyboard.inputText('Hello')

// Switch to number keyboard
keyboard.switchInputType(inputType.NUM)
}
})