問題描述
我想在我的 Laravel 應用程序中使用條形碼掃描儀.這是一個在線銷售點應用程序.我知道條碼掃描器只返回一個字符串,然后按 Enter 按鈕.但是為了捕獲這個字符串,我需要使用一個表單并選擇輸入字段.如果我不選擇輸入字段,它就無法捕獲數據.我想要的是在不選擇表格的情況下使用條形碼掃描儀.有可能嗎?
I would like to use barcode scanner in my Laravel application. This is an online Point of Sale Application. I know that barcode scanner returns just a string and press Enter Button. But to capture this string I need to use a form and select the input field as well. If I dont select the input field it cann't capture the data. What I want is to work the barcode scanner without selecting the form. Is it possible anyway ?
推薦答案
您可以使用 JavaScript 捕獲條形碼閱讀器發送的按鍵.
You can capture the keypresses that the barcode reader sends with JavaScript.
向窗口或文檔對象添加事件偵聽器以捕獲文檔中任何位置的任何按鍵.檢查傳入字符中是否有表示條形碼結束的字符(可能是新行).
Add an event listener to the window or document object to capture any keypresses anywhere in the document. Check the incoming characters for one which signals the end of the barcode (probably a new line).
這是我使用 RFID 閱讀器為類似任務編寫的一些代碼.它取決于 jQuery(主要是因為 jQuery 對 event.which
進行的規范化使得識別按下的字符變得方便)但是如果您愿意,您可以重寫它以避免這種情況.
This is some code I wrote for a similar task using an RFID reader. It depends on jQuery (mostly because the normalisation jQuery does on event.which
makes identifying the character pressed convenient) but you can rewrite it to avoid that if you like.
它將每個按鍵存儲在一個數組中,除非按鍵是 Enter(我使用的 RFID 閱讀器在每次掃描后發送).如果它得到一個回車,它接受掃描的代碼并對其進行操作(我正在使用 Socket.IO 將它發送到服務器,你可以用它做任何你喜歡的事情)然后清除數組,以便下一次掃描可以從新鮮開始.
It stores each keypress in an array unless the press is of Enter (which the RFID reader I was using sent after each scan). If it gets an Enter, it takes the scanned code and acts on it (I'm using Socket.IO to send it to the server, you can do whatever you like with it) and then clears the array so that the next scan can start from fresh.
var keybuffer = [];
function press(event) {
if (event.which === 13) {
return send();
}
var number = event.which - 48;
if (number < 0 || number > 9) {
return;
}
keybuffer.push(number);
}
$(document).on("keypress", press);
function send() {
socket.emit('scan', keybuffer.join(""));
keybuffer.length = 0;
}
這篇關于如何在 Web 應用程序中使用條碼掃描儀的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!