vue3 引用虚拟键盘simple-keyboard

simple-keyboard官网地址:https://virtual-keyboard.js.org

目前实现效果图是(实现数字、大小写字母键盘):
在这里插入图片描述

1.需要先安装simple-keyboard
npm install simple-keyboard --save
2.封装sinpleKeyboard 组件
<!-- keyboard-box.vue-->
<template><div class="keyboard-box"><div :class="keyboardClass"></div></div>
</template><script setup name= "SimpleKeyboard">
//引用
import Keyboard from "simple-keyboard";
import "simple-keyboard/build/css/index.css";
import { onMounted, ref, watch } from "vue";
//自定义键盘图片
import delIcon from "../assets/del.png";
import lockIcon from "../assets/lock.png";
import bigIcon from "../assets/big.png";const emit = defineEmits(["onChange", "onKeyPress"]);
const visible = ref(true);
const props = defineProps({keyboardClass: { type: String, default: "simple-keyboard" },input: String,layout: {type: Object,default: () => {return {default: ["1 2 3 {bksp}","4 5 6 .","7 8 9 @","{close} 0 {abc} {enter}",],abc: ["q w e r t y u i o p","a s d f g h j k l","{lock} z x c v b n m {bksp}","{123} . 0 @ {close} {enter}",],lock: ["Q W E R T Y U I O P","A S D F G H J K L","{big} Z X C V B N M {bksp}","{123} . 0 @ {close} {enter}",],// 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: [//     "~ ! @ # $ % ^ &amp; * ( ) _ + {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 &lt; &gt; ? {shift}",//     "@ {space}",//   ],};},},
});
let keyboard = ref(null);
onMounted(() => {keyboard.value = new Keyboard(props.keyboardClass, {onChange: onChange,//这里一定要注意驼峰命名(否则会不生效)onKeyPress: onKeyPress,});keyboard.value.setOptions({layoutName: "default",layout: props.layout,display: {// 中文语言包"{enter}": "完成","{123}": "123","{tab}": "tab","{shift}": "shift","{space}": " ","{bksp}": `<img src=${delIcon} style='' width='30'>`,"{big}": `<img src=${lockIcon} style='' width='30'>`,"{lock}": `<img src=${bigIcon} style='' width='30'>`,"{close}": "关闭","{abc}": "abc",},});
});watch(()=>props.input, (newValue, oldValue) => {keyboard.value.setInput(newValue);
});const onChange = (input) => {emit("onChange", input);
};
const onKeyPress = (button) => {emit("onKeyPress", button);if (button === "{123}" || button === "{abc}") {handleShift();}if (button === "{lock}" || button === "{big}") hadleLock();// return;if (button === "{big}" ||button === "{abc}" ||button === "{lock}" ||button === "{123}") {console.log(keyboard.value.options.layoutName,"keyboard.value.options.layoutName");}
};const handleShift = () => {let currentLayout = keyboard.value.options.layoutName;let shiftToggle = currentLayout === "default" ? "abc" : "default";keyboard.value.setOptions({layoutName: shiftToggle,});
};
const hadleLock = () => {let currentLayout = keyboard.value.options.layoutName;let shiftToggle = currentLayout === "abc" ? "lock" : "abc";keyboard.value.setOptions({layoutName: shiftToggle,});
};
</script><style  scoped>
.keyboard-box{width: 40%;margin: auto;
}
.simple-keyboard {background: none !important;font-size: 20px;
}::v-deep(.hg-button:nth-of-type(4)),
::v-deep(.hg-button-close),
::v-deep(.hg-button-abc) {box-sizing: border-box;width: 20px;max-width: none !important;
}
.hg-button {/* width: calc(100% / 4); */
}
</style>
3.引用组件
<template>
<el-inputsize="large"class="text"v-model="mobile"clearable@focus="focusMobile"@blur="blurMobile"></el-input><!-- 键盘 --><div class="el-drawer keyboard"><number-keyboardv-if="isKeyboard"@onChange="onChange"@onKeyPress="onkeyPress":input="model"></number-keyboard></div>
</template>
<script setup>
import numberKeyboard from "../components/numberKeyboard.vue";let mobile=ref('')
let isKeyboard=ref(false)
// 键盘按键的值const onChange = (e) => {console.log(state.currentProp, "23423423");model.value= e;};// 键盘按键为完成和关闭时隐藏键盘const onkeyPress = (button) => {if (button === "{enter}" || button === "{close}") {isKeyboard.value = false;model.value = "";}};const focusMobile = (e) => {nextTick(() => {isKeyboard.value = true;model.value  =  model.value });console.log(e, "23424聚焦点");};const blurMobile = (e) => {console.log(e, "失去焦点");};
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/659698.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode55:跳跃游戏

题目描述 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个下标&#xff0c;如果可以&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 解题思想 每次…

【Harmony3.1/4.0】笔记七-选项卡布局

概念 当页面信息较多时&#xff0c;为了让用户能够聚焦于当前显示的内容&#xff0c;需要对页面内容进行分类&#xff0c;提高页面空间利用率。Tabs组件可以在一个页面内快速实现视图内容的切换&#xff0c;一方面提升查找信息的效率&#xff0c;另一方面精简用户单次获取到的…

AJAX——事件循环(EventLoop)

1.事件循环&#xff08;EventLoop&#xff09; 概念&#xff1a;JavaScript有一个基于事件循环的并发模型&#xff0c;事件循环负责执行代码、收集和处理事件以及执行队列中的子任务。这个模型与其它语言中的模型截然不同&#xff0c;比如C和Java。 原因&#xff1a;JavaScri…

Nacos、OpenFeign、网关 笔记

一、远程调用 1.1配置RestTemplate配置类 package com.hmall.cart.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;Configuration public c…

测试新人,如何快速上手一个陌生的系统!

大家好&#xff0c;我是狂师&#xff01; 作为刚不行不久的测试新人&#xff0c;面对一个陌生的系统时&#xff0c;可能会感到有些手足无措。面对一个全新的系统系统&#xff0c;如何快速上手并展开有效的测试工作是一个重要的挑战。 本文将探讨测试新人如何通过一系列步骤和…

VSCode SSH连接远程主机失败,显示Server status check failed - waiting and retrying

vscode ssh连接远程主机突然连接不上了&#xff0c;终端中显示&#xff1a;Server status check failed - waiting and retrying 但是我用Xshell都可以连接成功&#xff0c;所以不是远程主机的问题&#xff0c;问题出在本地vscode&#xff1b; 现象一&#xff1a; 不停地输入…

lt Redis变慢的原因及排查解决方法

前言 Redis 作为优秀的内存数据库&#xff0c;其拥有非常高的性能&#xff0c;单个实例的 OPS 能够达到 10W 左右(5-10W)。但也正因此如此&#xff0c;当我们在使用 Redis 时&#xff0c;如果发现操作延迟变大的情况&#xff0c;就会与我们的预期不符。 你也许或多或少地&…

OpenAI发布GPT-4.0使用指南

大家好&#xff0c;ChatGPT 自诞生以来&#xff0c;凭借划时代的创新&#xff0c;被无数人一举送上生成式 AI 的神坛。在使用时&#xff0c;总是期望它能准确理解我们的意图&#xff0c;却时常发现其回答或创作并非百分之百贴合期待。这种落差可能源于我们对于模型性能的过高期…

Unity 合并子物体获得简化Mesh

合并子物体获得简化Mesh &#x1f959;环境&#x1f96a;Demo &#x1f959;环境 PackageManager安装Editor Coroutines 导入插件&#x1f448; &#x1f96a;Demo 生成参数微调&#xff1a;Assets/EasyColliderEditor/Scripts/VHACDSettings/VHACDSettings.asset

【骑友警惕】停骑的惊人后果揭晓 你真的有“休息”的权利吗?

骑行&#xff0c;不仅仅是一项运动&#xff0c;它是一部分人的生活方式&#xff0c;是对自由的追逐&#xff0c;对健康的向往。然而&#xff0c;在这条充满汗水和风景的路上&#xff0c;有时候我们会因为各种原因不得不暂时停下脚步。但你知道吗&#xff1f;停骑&#xff0c;看…

Python | Leetcode Python题解之第58题最后一个单词的长度

题目&#xff1a; 题解&#xff1a; class Solution:def lengthOfLastWord(self, s: str) -> int:ls[]for i in s.split():ls.append(i)return len(ls[-1])

若依:Linux Centos 7.9 安装部署RuoYi前后端集成版

目录 1.虚拟机操作系统版本 2.删除旧的jdk 3.下载JDK 17 &#xff1a; 4.下载 mvn 3.9.6&#xff1a; 5.下载mysql:5.7.44版本 6.git下载若依&#xff1a; 7.修改数据库连接&#xff1a; 8.mvn 清理和打包 9.启动若依&#xff1a; 1.虚拟机操作系统版本 2.删除旧的jd…