@@ -21,6 +21,7 @@ const barcodeHidReader = (function () {
2121 let node
2222 let log
2323
24+ const letterCodes = Object . fromEntries ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . split ( '' ) . map ( c => [ 'Key' + c , [ c . toLowerCase ( ) , c ] ] ) )
2425 const latinKeyCodes = {
2526 BracketLeft : [ '[' , '{' ] ,
2627 BracketRight : [ ']' , '}' ] ,
@@ -43,7 +44,8 @@ const barcodeHidReader = (function () {
4344 Minus : [ '-' , '_' ] ,
4445 Equal : [ '=' , '+' ] ,
4546 Backquote : [ '`' , '~' ] ,
46- IntlBackslash : [ '\\' , '|' ]
47+ IntlBackslash : [ '\\' , '|' ] ,
48+ ...letterCodes
4749 }
4850
4951 function reset ( ) {
@@ -64,10 +66,15 @@ const barcodeHidReader = (function () {
6466 }
6567
6668 function handleTimeout ( ) {
67- events . forEach ( el => {
68- el . explicitOriginalTarget . dispatchEvent ( el )
69- } )
70- reset ( )
69+ if ( suffix !== '' || barcode . length < ( prefix === '' ? 2 : 1 ) ) {
70+ logger ( 'timeout' , barcode )
71+ events . forEach ( el => {
72+ el . explicitOriginalTarget . dispatchEvent ( el )
73+ } )
74+ reset ( )
75+ } else {
76+ completeBarcode ( )
77+ }
7178 }
7279
7380 function logger ( ) {
@@ -80,30 +87,37 @@ const barcodeHidReader = (function () {
8087 }
8188 }
8289
90+ function completeBarcode ( ) {
91+ logger ( 'barcode' , barcode )
92+ callback ( barcode )
93+
94+ reset ( )
95+ }
96+
8397 function dispatchKeyUp ( e ) {
8498 logger ( shortState , e , e . custom )
8599
86100 if ( e . altKey || e . ctrlKey ) {
87101 return
88102 }
89103
90- if ( shortState === CAPTURING && e . key . length === 1 ) {
104+ const keyCode = e . code . toLowerCase ( )
105+ const isPrintable = e . key . length === 1
106+
107+ if ( shortState === CAPTURING && isPrintable ) {
91108 e . preventDefault ( )
92109 e . stopPropagation ( )
93110 events . push (
94111 Object . assign ( new KeyboardEvent ( 'keyup' , e ) , { custom : true } )
95112 )
96113 }
97- if ( shortState === CAPTURING && e . key . toLowerCase ( ) === suffix ) {
114+ if ( shortState === CAPTURING && keyCode === suffix ) {
98115 timeoutID && clearTimeout ( timeoutID )
99116
100117 e . preventDefault ( )
101118 e . stopPropagation ( )
102119
103- logger ( 'barcode' , barcode )
104- callback ( barcode )
105-
106- reset ( )
120+ completeBarcode ( )
107121 }
108122 }
109123
@@ -118,41 +132,43 @@ const barcodeHidReader = (function () {
118132 return
119133 }
120134
121- if ( shortState !== CAPTURING && e . key . length !== 1 ) {
122- return
123- }
124-
125135 if ( e . altKey || e . ctrlKey ) {
126136 return
127137 }
128138
129- if ( shortState === CAPTURING && e . key . toLowerCase ( ) === suffix ) {
139+ const keyCode = e . code . toLowerCase ( )
140+ const isPrintable = e . key . length === 1
141+ const isPrefix = prefix !== '' && keyCode === prefix
142+
143+ if ( shortState === CAPTURING && keyCode === suffix ) {
130144 e . preventDefault ( )
131145 e . stopPropagation ( )
132146 return
133147 }
134148
135- if ( prefix !== '' && e . key === prefix ) {
149+ let prefixFlag = false
150+ if ( shortState === IDLE && isPrefix ) {
136151 shortState = CAPTURING
152+ prefixFlag = true
137153 }
138154
139- if ( prefix === '' && e . key . length === 1 ) {
155+ if ( prefix === '' && isPrintable ) {
140156 shortState = CAPTURING
141157 }
142158
143- if ( shortState === CAPTURING && e . key . length === 1 ) {
159+ if ( shortState === CAPTURING && isPrintable ) {
144160 events . push (
145161 Object . assign ( new KeyboardEvent ( 'keydown' , e ) , { custom : true } )
146162 )
147163 events . push (
148164 Object . assign ( new KeyboardEvent ( 'keypress' , e ) , { custom : true } )
149165 )
150166
151- if ( e . key !== prefix ) {
152- if ( convertToLatin && ( latinKeyCodes [ e . code ] || e . code ?. startsWith ( 'Key' ) ) ) {
167+ if ( ! prefixFlag || ! isPrefix ) {
168+ if ( convertToLatin && latinKeyCodes [ e . code ] ) {
153169 barcode += e . shiftKey
154- ? latinKeyCodes [ e . code ] ?. [ 1 ] ?? e . code . charAt ( 3 )
155- : latinKeyCodes [ e . code ] ?. [ 0 ] ?? e . code . charAt ( 3 ) . toLowerCase ( )
170+ ? latinKeyCodes [ e . code ] ?. [ 1 ]
171+ : latinKeyCodes [ e . code ] ?. [ 0 ]
156172 } else {
157173 barcode += e . key
158174 }
@@ -183,6 +199,7 @@ const barcodeHidReader = (function () {
183199 defaults ,
184200 options
185201 ) )
202+ prefix = prefix . toLowerCase ( )
186203 suffix = suffix . toLowerCase ( )
187204 reset ( )
188205 node = doc
0 commit comments