From 9d9c80dd6a09e98e7ed55dd709d209d5f6e1fc8c Mon Sep 17 00:00:00 2001 From: s72785 Date: Tue, 21 Jul 2015 21:22:06 +0200 Subject: [PATCH 1/2] fix escaping focus fix #67 --- static/js/barcode.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/static/js/barcode.js b/static/js/barcode.js index cd71ad8..9cd6353 100644 --- a/static/js/barcode.js +++ b/static/js/barcode.js @@ -20,16 +20,17 @@ function hideBarcode() { function barcodeKeyPress(event) { var key = String.fromCharCode(event.charCode) var input = document.getElementById('crement') - if ( input ) { + var inputcount = document.querySelectorAll('[type="text"]').length + document.querySelectorAll('[type="number"]').length + if ( input && inputcount == 1 ) { input.focus() } var focused = document.activeElement - if (! focused || focused == document.body) { + if ( !focused || focused == document.body ) { focused = null - } else if (document.querySelector) { + } else if ( document.querySelector ) { focused = document.querySelector(":focus") } - if ( focused == null || focused.tagName != "INPUT" ) { + if ( inputcount <= 1 && ( focused == null || focused.tagName != "INPUT" ) ) { if ( event.keyCode === 13 ) { var input = document.getElementById('barcodeInput') if (input && barcodeBuf.length > 0) { From a4b42c99c97b6bc6c95c1e211206b89c29d36a95 Mon Sep 17 00:00:00 2001 From: s72785 Date: Wed, 22 Jul 2015 00:57:50 +0200 Subject: [PATCH 2/2] fix backspace, limit input to non control/alt key, so shortcuts can still be used in browser also F-Keys now are useable again --- static/js/barcode.js | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/static/js/barcode.js b/static/js/barcode.js index 9cd6353..4a249db 100644 --- a/static/js/barcode.js +++ b/static/js/barcode.js @@ -17,41 +17,48 @@ function hideBarcode() { return "" } -function barcodeKeyPress(event) { - var key = String.fromCharCode(event.charCode) - var input = document.getElementById('crement') - var inputcount = document.querySelectorAll('[type="text"]').length + document.querySelectorAll('[type="number"]').length - if ( input && inputcount == 1 ) { +function barcodeKeyPress( event ) { // takes input from either keyboard or barcode scanner + var key = String.fromCharCode( event.charCode ) + var input = document.getElementById( 'crement' ) + var inputcount = document.querySelectorAll( '[type="text"]' ).length + document.querySelectorAll( '[type="number"]' ).length + if ( input && inputcount == 1 ) { // focus in 'crement' when no other input fields only input.focus() } var focused = document.activeElement if ( !focused || focused == document.body ) { focused = null } else if ( document.querySelector ) { - focused = document.querySelector(":focus") + focused = document.querySelector( ":focus" ) } - if ( inputcount <= 1 && ( focused == null || focused.tagName != "INPUT" ) ) { - if ( event.keyCode === 13 ) { - var input = document.getElementById('barcodeInput') - if (input && barcodeBuf.length > 0) { - input.setAttribute('value', barcodeBuf) + if ( + !event.ctrlKey && !event.altKey // no hotkeys used + && ( focused == null || focused.tagName != "INPUT" ) // focus not in input fielf for manual input + ) { + if ( event.keyCode === 13 ) { // carriage return + var input = document.getElementById( 'barcodeInput' ) + if ( input && barcodeBuf.length > 0 ) { + input.setAttribute( 'value', barcodeBuf ) input.parentNode.submit() return } barcodeBuf = "" event.preventDefault() - } else if ( event.keyCode === 27 ){ + } else if ( event.keyCode === 27 ) { // escape + console.log( "escape" ) barcodeBuf=hideBarcode() event.preventDefault() - } else if ( event.keyCode === 9 ){ + } else if ( event.keyCode === 8 ) { // backspace + console.log( "backspace" ) barcodeBuf = barcodeBuf.substring( 0, barcodeBuf.length - 1 ) + showBarcode( barcodeBuf ) if ( barcodeBuf.length <= 0 ) { - barcodeBuf=hideBarcode() + barcodeBuf = hideBarcode() } event.preventDefault() - } else { + } else if ( event.keyCode == 0 ) { // e.g. F-Keys are 112 to 123, A-Za-z0-9 all are 0. + console.log( "some input: " + barcodeBuf + "[" + key + "] <= {" + event.keyCode + "}" ) barcodeBuf += key - showBarcode(barcodeBuf) + showBarcode( barcodeBuf ) event.preventDefault() } }