html输入框只能扫描枪,vue.js 扫码枪 input 输入法问题

import { Observable } from "rxjs";

const Sdic = {

"1": "!",

"2": "@",

"3": "#",

"4": "$",

"5": "%",

"6": "^",

"7": "&",

"8": "*",

"9": "(",

"0": ")",

"-": "_",

"=": "+",

"[": "{",

"]": "}",

"\\": "|",

";": ":",

"'": '"',

",": "

".": ">",

"/": "?"

};

function IsLower(s: string) {

return s.toLowerCase() === s;

}

function GetShiftChar(cur: KeyboardEvent): string {

if (cur.keyCode >= 65 && cur.keyCode <= 90) {

return IsLower(cur.key) ? cur.key.toUpperCase() : cur.key.toLowerCase();

} else {

return Sdic[cur.key];

}

}

function GetStop(input: HTMLInputElement) {

const end$ = Observable.fromEvent(input, "compositionend");

const keyup$ = Observable.fromEvent(input, "keyup");

const enter$ = keyup$.filter(e => e.keyCode === 13);

return Observable.combineLatest(end$.merge(enter$), keyup$).debounceTime(250);

}

function GetLast(str: string) {

return str.substr(str.length - 1, 1);

}

export function GetInputSource(input: HTMLInputElement) {

const stop$ = GetStop(input);

return Observable.fromEvent(input, "keyup")

.scan(

(prev, cur) => {

// //汉字

// if (cur.keyCode === 231) {

// return {

// IsShift: false,

// key: GetLast(input.value)

// };

// }

return {

IsShift: cur.keyCode === 16,

key: prev.IsShift ? GetShiftChar(cur) : cur.key

};

},

{ IsShift: false, key: "" }

)

.map(t => t.key)

.filter(t => t != null && t.length === 1)

.bufferWhen(() => stop$)

.map(arr => arr.join("").trim());

}

class="form-control"

:readonly="readonly"

ref="input" />

class="btn btn-danger"

@click="clear">X

  • :key="i"

    v-text="s">

import { GetInputSource } from '@/utils/Typeahead.ts';

const TRUE_RESULT = '123abc!@#$%^&<>;[]';

export default {

data() {

return {

value: '',

TRUE_RESULT,

list: []

}

},

computed: {

readonly() {

return this.value != null && this.value != '';

}

},

mounted() {

this.clear();

},

methods: {

init() {

const input = this.$refs.input;

this.sub && this.sub.unsubscribe();

this.sub = GetInputSource(input)

.takeWhile(() => !this.readonly)

.subscribe(this.add);

},

add(str) {

console.log(str);

console.log(str === TRUE_RESULT);

this.SetValue(str);

this.list.push(str);

},

async SetValue(val) {

this.value = val;

await this.$nextTick();

const input = this.$refs.input;

input.value = val;

input.focus();

},

clear() {

this.init();

this.SetValue('');

}

},

beforeDestroy() {

this.sub && this.sub.unsubscribe();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值