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-input
            size="large"
            class="text"
            v-model="mobile"
            clearable
            @focus="focusMobile"
            @blur="blurMobile"
          >
          </el-input>
    <!-- 键盘 -->
    <div class="el-drawer keyboard">
      <number-keyboard
        v-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>
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值