본문 바로가기

개발/Web_JavaScript

onscan.js 사용시 _(언더바), -(대시), |(파이브) 입력방법

728x90
반응형

웹에서 바코드스캐너를 사용하여 스캔 입력값을 사용하게 해주는 훌륭한 라이브러리 onscan.js가 존재한다.

github onscan.js - https://github.com/axenox/onscan.js

 

GitHub - axenox/onscan.js: On-scan events for hardware barcode scanners in vanilla JS

On-scan events for hardware barcode scanners in vanilla JS - GitHub - axenox/onscan.js: On-scan events for hardware barcode scanners in vanilla JS

github.com

 

하지만 숫자와 영문은 문제없이 잘 작동하나 _(언더바), -(대시), |(파이브)는 인식이 안되는 문제가 발생하는데 onscan.js에서 제공되는 attachTo 메서드에 keyCodeMapper 옵션을 적용하면 언더바, 대시, 파이브가 정상적으로 인식되게 수정할 수 있다.

[onscan.js] keyCodeMapper

 

onScan.attachTo(document, {
    onScan: function (sCode, iQty) {
        console.log('Scanned : ' + iQty + ', x :' + sCode);
    },
    keyCodeMapper: function (oEvent) {
        // 참고부분
        if (oEvent.key == '-') {
            return '-';
        } else if (oEvent.key == '_') {
            return '_';
        } else if (oEvent.key == '|') {
            return '|';
        }
        return onScan.decodeKeyEvent(oEvent);
    }
});

document.addEventListener('scan', function (sScancode, iQuantity) {
    if (sScancode) {
        var sCode = sScancode.detail.scanCode;
        console.log('sCode : ' + sCode);
    }
});

 

 

728x90
반응형