Prevent open downloads window in chrome with barcode reader

白昼怎懂夜的黑 提交于 2019-12-04 05:25:38

i found this link but the only solution was to change your scanner's default character... i'm not sure I can change mine though so , like you, I'd be looking for a browser-related fix too.. maybe javascript. i'll try handling the characters with javascript to prevent that from happening... if I have any success i'll try and remember to come back here and tell you hehehehehe

i think this solves the issue....

$(document).ready(function(){
    $("#input").keydown(function(e){
        if(e.which==17 || e.which==74){
            e.preventDefault();
        }else{
            console.log(e.which);
        }
    })
});

lemme know whether his works for you too.. make sure you empty the cache too...

this code works for me

$(document).ready(function(){
    $("#input").keydown(function(e){
        if(e.which==17 || e.which==74 || e.keyCode == 13){
            e.preventDefault();
        }
    })
});

My scanner (Intermec SR30) is set up to apply 3 new line characters after the bar code. I found this by opening Vim, insert, then scanned the bar code. Then I cat'ed the file to od -ax:

0000000   3   1   2   2   1   0   9   9   9   4   8   5   2   8  nl  nl
       3133    3232    3031    3939    3439    3538    3832    0a0a
0000020  nl
           000a
0000021

I can trap the 'nl's with:

$(document).ready(function(){
    $("#barcode").keypress(function(e){
        console.log('"' + e.keyCode + '"\n');
        if(e.keyCode == 13){
            e.preventDefault();
        }
    })
});

but the download-window-open event (Ctrl+j from the keyboard) is slurped up by the browser before it gets to the open page. This problem also plagues Firefox 30.0.

The code on the approved answer is blocking CTRL and J keys. This would block only CTRL + J

$("#barcode").keypress(function(event){
    if(event.keyCode == 74 && event.ctrlKey){
        event.preventDefault();
    }
});

This code below won't work. Because if barcode value has 'J' character in it, you won't be able to get the correct result. We try to add e.ctrlkey control but this time, only one character of the barcode will be retrieved. Solution via js seems to be hard. Maybe the best option is changing scanner settings.

$(document).ready(function(){
    $("#input").keydown(function(e){
        if(e.which==17 || e.which==74 || e.keyCode == 13){
            e.preventDefault();
        }
    })
});
sarika patil

This works for me.

<script>
  document.addEventListener('keydown', function(event) {
    if( event.keyCode == 13 || event.keyCode == 17 || event.keyCode == 74 )
      event.preventDefault();
  });
</script>

in our angularjs application we solved this issue with this directive:

(function () {
    "use strict";

    angular
        .module("Xeon.core")
        .directive("xnOnBarcodeKeydown", xnOnBarcodeKeydown);

    var whiteList = [67, 86, 65, 88];

    /** @ngInject */
    function xnOnBarcodeKeydown() {
        return function (scope, element, attrs) {
            element.bind("keydown",
                function (event) {

                    if (event.ctrlKey && whiteList.indexOf(event.which) === -1) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                        event.stopPropagation();
                        return false;
                    }

                    if (event.which === 13) {
                        scope.$apply(function () {
                            scope.$eval(attrs.xnOnBarcodeKeydown, { 'event': event });
                        });

                        event.preventDefault();
                    }
                });
        };
    }
})();

Angular version:

import { Directive, EventEmitter, HostListener, Output } from '@angular/core';

@Directive({ selector: '[appXnOnBarcodeKeydown]' })
export class XnOnBarcodeKeydownDirective{

  @Output() onEnter = new EventEmitter();

  private whiteList = [67, 86, 65, 88];

  @HostListener('keydown', ['$event'])
  keyDown(event) {

    if (event.ctrlKey && this.whiteList.indexOf(event.which) === -1) {
      event.stopImmediatePropagation();
      event.preventDefault();
      event.stopPropagation();
      return false;
    }

    if (event.which === 13) {
      this.onEnter.emit(event);
      event.preventDefault();
    }
  }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!