| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <template>
- <view>
- <view>监听到的内容:{{ inputString }}</view>
- <view class="">
- --------------------分割线-----------------------------
- </view>
- <view class="">
- {{text}}
- </view>
- <view class="">
- --------------------分割线-----------------------------
- </view>
- <view class="">
- {{aaa}}
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- inputString: '',
- inputCache: '',
- text:'',
- aaa:'',
- };
- },
- methods: {
- onConfirm(text) {
- // alert(JSON.stringify(text))
- console.log('拿到的text', text);
- // 此处写我们自己的逻辑
- this.text = text;
- },
- keypress(e) {
- this.aaa = JSON.stringify(e);
- // alert(JSON.stringify(e))
- if (e.keyCode === 66 || e.key === 'Enter') {
- this.inputString = this.inputCache;
- this.onConfirm(this.inputString);
- this.inputCache = '';
- } else {
- this.inputCache += e.key;
- }
- }
- },
- mounted() {
- // #ifdef APP-PLUS
- plus.key.addEventListener("keyup", this.keypress);
- // #endif
- // #ifdef H5
- document.addEventListener("keyup", this.keypress);
- // #endif
- },
- beforeDestroy() {
- // #ifdef APP-PLUS
- plus.key.removeEventListener("keyup", this.keypress);
- // #endif
- // #ifdef H5
- document.removeEventListener("keyup", this.keypress);
- // #endif
- },
- deactivated() {
- // #ifdef APP-PLUS
- plus.key.removeEventListener("keyup", this.keypress);
- // #endif
- // #ifdef H5
- document.removeEventListener("keyup", this.keypress);
- // #endif
- }
- };
- </script>
|