You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
< template >
< div >
12354
< / div >
< / template >
< script >
export default {
data ( ) {
return {
}
} ,
mounted ( ) {
// 监听按键
var code = ''
var lastTime , nextTime // 上次时间、最新时间
var lastCode , nextCode // 上次按键、最新按键
document . onkeypress = ( e ) => {
debugger
// 获取按键
if ( window . event ) { // IE
nextCode = e . keyCode
} else if ( e . which ) { // Netscape/Firefox/Opera
nextCode = e . which
}
// 如果触发了回车事件(扫码结束时间)
if ( nextCode === 13 ) {
if ( code . length < 3 ) return // 手动输入的时间不会让code的长度大于2, 所以这里只会对扫码枪有
this . codeFind ( code ) // 获取到扫码枪输入的内容,做别的操作
code = ''
lastCode = ''
lastTime = ''
return
}
nextTime = new Date ( ) . getTime ( ) // 记录最新时间
if ( ! lastTime && ! lastCode ) { // 如果上次时间和上次按键为空
code += e . key // 执行叠加操作
}
// 如果有上次时间及上次按键
if ( lastCode && lastTime && nextTime - lastTime > 30 ) { // 当扫码前有keypress事件时,防止首字缺失
code = e . key
} else if ( lastCode && lastTime ) {
code += e . key
}
lastCode = nextCode
lastTime = nextTime
}
} ,
methods : {
// 业务逻辑根据项目更改
codeFind ( code ) {
alert ( code )
}
}
}
< / script >