前言:在大型触屏设备(如双屏设备)中,就没有键盘去操作,而且在触屏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中进行了修改~
- 在需要应用的页面 引入 注册 使用即可
<!-- 全键盘组件 -->
<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.bmabk.com/index.php/post/79208.html