前言:在大型触屏设备(如双屏设备)中,就没有键盘去操作,而且在触屏input或者textarea的输入时候就无法去输入值,没办法触发输入框enter事件,所以就需要去建立一个虚拟键盘去操作
就想着找一找vue有没有类似的键盘组件。 还!真!有!
项目使用框架:vue+element ui
官网
https://virtual-keyboard.js.org/vuejs
示例
https://hodgef.com/simple-keyboard/demos/
文档
https://hodgef.com/simple-keyboard/getting-started/
黑的、白的、移动端、全键盘、数字键盘等
实际应用
1.先安装
npm i simple-keyboard -S
2.我这里是单独写了一个SimpleKeyboard.vue的组件
<template>
<div :class="keyboardClass"></div>
</template>
<script>
import Keyboard from "simple-keyboard";
import "simple-keyboard/build/css/index.css";
export default {
name: "SimpleKeyboard",
props: {
keyboardClass: {
default: "simple-keyboard",
type: String,
},
input: {
type: String,
},
layout: {
type: Object,
default: function () {
return {
default: [
"` 1 2 3 4 5 6 7 8 9 0 - = {bksp}",
"{tab} q w e r t y u i o p [ ] \\",
"{lock} a s d f g h j k l ; ' {enter}",
"{shift} z x c v b n m , . / {shift}",
"@ {space}",
],
shift: [
"~ ! @ # $ % ^ & * ( ) _ + {bksp}",
"{tab} Q W E R T Y U I O P { } |",
'{lock} A S D F G H J K L : " {enter}',
"{shift} Z X C V B N M < > ? {shift}",
"@ {space}",
],
};
},
},
},
data: () => ({
keyboard: null,
}),
mounted() {
this.keyboard = new Keyboard(this.keyboardClass, {
onChange: this.onChange,
onKeyPress: this.onKeyPress,
});
this.keyboard.setOptions({
layoutName: "default",
layout: this.layout,
display: {
"{enter}": "close",
"{shift}": "shift",
"{bksp}": "del",
"{tab}": "tab",
"{space}": "space",
"{lock}": "caps",
},
});
},
methods: {
onChange(input) {
this.$emit("onChange", input);
},
onKeyPress(button) {
this.$emit("onKeyPress", button);
/**
* If you want to handle the shift and caps lock buttons
*/
if (button === "{shift}" || button === "{lock}") this.handleShift();
},
handleShift() {
let currentLayout = this.keyboard.options.layoutName;
let shiftToggle = currentLayout === "default" ? "shift" : "default";
this.keyboard.setOptions({
layoutName: shiftToggle,
});
},
},
watch: {
input(input) {
this.keyboard.setInput(input);
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
tips: 因为我这边有几个键需要自定义,所以在mounted中进行了修改~文章来源:https://www.toymoban.com/news/detail-501300.html
- 在需要应用的页面 引入 注册 使用即可
<!-- 全键盘组件 -->
<div class="keyboard-mask">
<SimpleKeyboard @onChange="onChange" @onKeyPress="onKeyPress" :input="input"/>
</div>
onChange(input) {
// input 是当前点击按钮的值
},
onKeyPress(button) {
if (button == "{enter}") {
// 如果按确认键的相应操作
}
if (button == "{bksp}") {
// 删除键的相应操作
}
},
小tips:
含有中文输入法的文章:
https://www.cnblogs.com/linjiangxian/p/16223681.html#_label1_1文章来源地址https://www.toymoban.com/news/detail-501300.html
到了这里,关于vue 管理后台 使用虚拟键盘组件 支持中英文切换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!