Skip to content

Instantly share code, notes, and snippets.

@ewoo
Last active August 29, 2015 14:24
Show Gist options
  • Save ewoo/2de353a0a1bcef088916 to your computer and use it in GitHub Desktop.
Save ewoo/2de353a0a1bcef088916 to your computer and use it in GitHub Desktop.
Browser Keyboard Input Steam Handler (for parsing-out SGN barcodes)
$(document).ready(function() {
console.log("Ready!");
});
$("#target").submit(function(event){
event.preventDefault();
});
function parseBuffer(key, buffer){
var index = buffer.indexOf(key);
var value = null;
if (index > -1) {
value = buffer.substring(index + key.length, buffer.length);
$('h1').text(value);
}
return value;
}
var inputBuffer = "";
var barcodes = [];
// Handling the 'keypress' is critical. If you use keyDown or keyUp,
// you won't be able to reliable detect the special characters.
$(document).on('keypress', function (event) {
if (typeof(event.keyCode) == 'undefined') {
return;
}
// If carriage return was detected, search string and dump buffer.
if(event.keyCode == 13) {
var barcodeLookup = [
{key: "201|", type: "processorder"},
{key: "mon|", type: "mfgorder"},
{key: "SGNID|", type: "sgnid"},
{key: "BOATID|", type: "boatid"}
];
barcodeLookup.forEach(function(obj, index, array){
var code = parseBuffer(obj.key, inputBuffer);
if(code) {
barcodes[obj.type] = code;
}
});
// Access barcode value like so: barcodes[typename]
$('#barcodeOrderNo').val(barcodes["processorder"] || "");
$('#barcodeBoatID').val(barcodes["mfgorder"] || "");
// Dump buffer.
inputBuffer = "";
}
else {
// Fill the input buffer.
inputBuffer = inputBuffer + String.fromCharCode(event.keyCode);
}
});
@ewoo
Copy link
Author

ewoo commented Jul 10, 2015

Still to do is deciding which barcode is which, and then populating the proper target input--and swapping contents if necessary.

@ewoo
Copy link
Author

ewoo commented Jul 10, 2015

Updated to accommodate multiple barcodes read into a collection for easy access. Page level variable "barcodes" persists the valid barcodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment