Skip to content

Keyboard Component ^0.7.0

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

📌 Platform Compatibility

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"   <!-- Controls show/hide -->
    v-model="inputValue"          <!-- Binds the input value -->
    v-model:car-lang="carLang"    <!-- Effective when mode is car, switches between Chinese/English -->
></hy-keyboard>

3. License Plate Keyboard Mode

The license plate keyboard supports two modes:

ModePropDescriptionUse Case
UncontrolledPass auto-switch-langLanguage switching is managed automatically inside the componentSimple scenarios, works out of the box
ControlledPass 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');

// Manual 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 the delete key and the close key:

  • The delete and close keys 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 prop
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 Prop

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 Prop

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 Prop

When set to true, the number 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; tapping keys while the keyboard is open will not change the positions of other keys.

8. close Event Handling

When the close button or the overlay is tapped, the close event is triggered, but the keyboard will not close automatically; 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>

Randomized 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>

Custom Title via Slot

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

PropDescriptionTypeDefault
showWhether to show the keyboardbooleanfalse
modelValueBound 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 keyboard key orderbooleanfalse
closeTextConfirm button textstring-
deleteTextDelete button textstring-
closeButtonLoadingWhether the close button shows a loading statebooleanfalse
modalWhether to show the overlaybooleanfalse
hideOnClickOutsideWhether to collapse the keyboard when tapping outsidebooleantrue
lockScrollWhether to lock scrollingbooleantrue
safeAreaInsetBottomWhether to fit within the bottom safe areabooleantrue
extraKeyExtra keys, supports string or string arraystring | string[]-
carLangLicense plate keyboard language mode, optional values: zh (provinces), en (letters); if not passed, it's uncontrolled modestring-
autoSwitchLangWhether the license plate keyboard automatically switches languagebooleanfalse
customStyleExternal styles to be appliedCSSProperties-
customClassCustom external class namestring-

Keyboard Events

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

Keyboard Slots

SlotDescriptionReceived Values
titleCustom title content-
03:29