Skip to content

Keyboard Component ^0.7.0 ​

A virtual keyboard component that supports multiple modes, including numeric keyboard, license plate keyboard, and ID card keyboard, with a customizable header and keys.

📌 Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

2. v-model Binding ​

The component uses v-model:show to control visibility, and v-model to bind the input value:

html
<hy-keyboard
    v-model:show="showKeyboard"   <!-- Control show/hide -->
    v-model="inputValue"          <!-- Bind the input value -->
    v-model:car-lang="carLang"    <!-- Effective when mode is car; switches between Chinese and English -->
></hy-keyboard>

3. License Plate Keyboard Mode ​

The license plate keyboard supports two modes:

ModePropertyDescriptionUse Case
Uncontrolled modePass auto-switch-langThe component manages language switching internallySimple scenarios, ready to use out of the box
Controlled modePass v-model:car-langManually control language switchingNeed to listen to or control the language in the parent component

Uncontrolled mode example:

html
<!-- Set auto-switch-lang to switch automatically -->
<hy-keyboard mode="car" v-model="value" auto-switch-lang></hy-keyboard>

Controlled mode example:

html
<hy-keyboard mode="car" v-model="value" v-model:car-lang="carLang"></hy-keyboard>
ts
const carLang = ref<'zh' | 'en'>('zh');

// Manually control the switching logic
const handleInput = (val: string) => {
    if (val.length === 1) {
        carLang.value = 'en';
    }
};

const handleDelete = () => {
    if (carControlledValue.value.length === 1) {
        carLang.value = 'zh';
    }
};

4. custom Mode Description ​

The keyboard in custom mode has a fixed sidebar on the right side, containing a delete key and a close key:

  • The delete key and close key in the sidebar are always displayed and require no extra configuration
  • If you need to customize additional key positions, you can use the extra-key property
html
<!-- custom mode: custom keys at the bottom, fixed delete and close keys on the right -->
<hy-keyboard mode="custom" v-model="value" :extra-key="['00', '.']" close-text="Done"></hy-keyboard>

5. extraKey Property ​

extraKey supports two formats:

html
<!-- String: a single extra key, displayed to the left of 0 -->
<hy-keyboard mode="custom" extra-key="+"></hy-keyboard>

<!-- Array: multiple extra keys (effective in custom mode) -->
<hy-keyboard mode="custom" :extra-key="['.', '00']"></hy-keyboard>

6. showDotKey Property ​

Controls whether to display the decimal point key, which is displayed by default:

html
<!-- Hide the decimal point -->
<hy-keyboard :show-dot-key="false"></hy-keyboard>

<!-- Show the decimal point (default) -->
<hy-keyboard :show-dot-key="true"></hy-keyboard>

7. randomKeyOrder Property ​

When set to true, the numeric keys will be shuffled in random order:

html
<hy-keyboard :random-key-order="true"></hy-keyboard>

Note

The random order is generated only once each time the keyboard is shown; pressing keys while the keyboard is open will not change the positions of other keys.

8. close Event Handling ​

Clicking the close button or the overlay triggers the close event, but it will not automatically close the keyboard; you need to handle it manually:

html
<hy-keyboard v-model:show="showKeyboard" @close="showKeyboard = false"></hy-keyboard>

9. ID Card Keyboard ​

The ID card keyboard automatically includes the X key; it is recommended to use it together with maxlength:

html
<hy-keyboard mode="idcard" :maxlength="18"></hy-keyboard>

🏯 Basic Usage Examples ​

Default Numeric Keyboard ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="default"
    v-model="value"
    close-text="Done"
    @input="handleInput"
    @delete="handleDelete"
    @close="handleClose"
></hy-keyboard>
ts
const showKeyboard = ref(false);
const value = ref('');

const handleInput = (val: string) => {
    console.log('Input:', val);
};

const handleDelete = () => {
    console.log('Delete');
};

const handleClose = () => {
    console.log('Close');
    showKeyboard.value = false;
};

Keyboard with Right Sidebar ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    v-model="value"
    mode="custom"
    close-text="Done"
    @input="handleInput"
    @delete="handleDelete"
    @close="handleClose"
></hy-keyboard>

ID Card Keyboard ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="idcard"
    v-model="value"
    @input="handleInput"
    @delete="handleDelete"
    @close="handleClose"
></hy-keyboard>

License Plate Keyboard (Uncontrolled Mode) ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="car"
    auto-switch-lang="true"
    v-model="value"
    @input="handleInput"
    @delete="handleDelete"
    @close="handleClose"
></hy-keyboard>

License Plate Keyboard (Controlled Mode) ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="car"
    v-model="value"
    v-model:car-lang="carLang"
    @input="handleInput"
    @delete="handleDelete"
    @close="handleClose"
></hy-keyboard>
ts
const carLang = ref<'zh' | 'en'>('zh');

const handleInput = (val: string) => {
    if (val.length === 1) {
        carLang.value = 'en';
    }
    console.log('License plate input:', val);
};

const handleDelete = () => {
    if (carControlledValue.value.length === 1) {
        carLang.value = 'zh';
    }
    console.log('License plate delete:', carControlledValue.value);
};

⚙️ Keyboard Configuration ​

Hide the Decimal Point ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="default"
    v-model="value"
    :show-dot-key="false"
    close-text="Done"
></hy-keyboard>

Custom Extra Keys ​

html
<!-- Single extra key -->
<hy-keyboard
    v-model:show="showKeyboard"
    mode="default"
    v-model="value"
    extra-key="+"
    close-text="Done"
></hy-keyboard>

<!-- Multiple extra keys (custom mode) -->
<hy-keyboard
    v-model:show="showKeyboard"
    mode="custom"
    v-model="value"
    :extra-key="['00', '.']"
    close-text="Done"
></hy-keyboard>

Random Numeric Keyboard ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="default"
    v-model="value"
    :random-key-order="true"
    close-text="Done"
></hy-keyboard>

🏷️ Custom Header ​

Keyboard with Title ​

html
<hy-keyboard
    v-model:show="showKeyboard"
    mode="default"
    v-model="value"
    title="Title"
    close-text="Done"
></hy-keyboard>

Slot Custom Title ​

html
<hy-keyboard v-model:show="showKeyboard" mode="default" v-model="value" close-text="Done">
    <template #title>
        <view class="custom-title">
            <text class="custom-title-text">Custom Title</text>
        </view>
    </template>
</hy-keyboard>
scss
.custom-title {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;

    &-text {
        font-size: 32rpx;
        font-weight: 500;
        color: #333;
    }
}

API ​

Keyboard Props ​

ParameterDescriptionTypeDefault
showWhether to show the keyboardbooleanfalse
modelValueThe bound valuestring-
titleTitlestring-
modeKeyboard mode, optional values: default (numeric keyboard), custom (custom keyboard), car (license plate keyboard), idcard (ID card keyboard)stringdefault
zIndexZ-index levelnumber100
maxlengthMaximum input lengthnumberInfinity
showDeleteKeyWhether to show the delete keybooleantrue
showDotKeyWhether to show the decimal point keybooleantrue
randomKeyOrderWhether to randomize the keyboard key orderbooleanfalse
closeTextConfirm button textstring-
deleteTextDelete button textstring-
closeButtonLoadingWhether the close button shows a loading statebooleanfalse
overlayWhether to show the overlaybooleanfalse
overlayOpacityOverlay opacitybooleanfalse
hideOnClickOutsideWhether to collapse the keyboard when clicking outsidenumber0
lockScrollWhether to lock scrollingbooleantrue
safeAreaInsetBottomWhether to be within the bottom safe areabooleantrue
extraKeyExtra keys, supports a string or an array of stringsstring | string[]-
carLangLicense plate keyboard language mode, optional values: zh (provinces), en (letters); if not passed, it runs in uncontrolled modestring-
autoSwitchLangWhether the license plate keyboard switches language automaticallybooleanfalse
customStyleDefine external styles to be usedCSSProperties-
customClassCustom external class namestring-

Keyboard Events ​

Event NameDescriptionCallback Parameters
inputTriggered when content is inputtext: the input character
deleteTriggered when content is deleted-
closeTriggered when the keyboard is closed-
update:showTriggered when visibility changesshow: current visibility state
update:modelValueTriggered when the value changesvalue: current value
update:carLangTriggered when the license plate language changeslang: current language ('zh' or 'en')

Keyboard Slots ​

Slot NameDescriptionReceived Values
titleCustom title content-

CSS Variables ​

VariableDescriptionDefault
--hy-keyboard-header-heightHeader height80rpx
--hy-keyboard-header-paddingHeader padding0 32rpx
--hy-keyboard-title-font-sizeTitle font size32rpx
--hy-keyboard-close-font-sizeClose button font size28rpx
--hy-keyboard-key-gapKey gapFollows --hy-border-margin-padding-sm (10rpx)
--hy-keyboard-sidebar-widthSidebar width160rpx
--hy-keyboard-key-heightKey height100rpx
--hy-keyboard-key-radiusKey border radius12rpx
--hy-keyboard-key-shadowKey shadow0 2rpx 8rpx rgba(0, 0, 0, 0.06)
--hy-keyboard-car-key-heightLicense plate key height88rpx
--hy-keyboard-car-key-font-sizeLicense plate key font size28rpx
--hy-keyboard-key-font-sizeKey font sizeFollows --hy-font-size-lg (36rpx)
--hy-keyboard-borderHeader divider lineFollows --hy-border-line
12:33
battery