Handle barcode scan in Java

前端 未结 4 680
长情又很酷
长情又很酷 2021-01-16 06:57

I want to have my application react to barcodes being scanned to trigger button presses. For example the user could scan the ((PRINT)) barcode to activate the print button.<

4条回答
  •  庸人自扰
    2021-01-16 07:44

    This is my approach. It's working. Just get the miliseconds for ensure doesn't read twice. Just add a Key Listener (implemented in the same JFrame).

      @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            logger().info("keytyped" + e.getKeyChar() + " code "+e.getKeyCode());
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                logger().info("all keys " + keyspressed);
                return;
            }
            // will not last more than a second...
            if (keyspressed == null || System.currentTimeMillis() - currentTimeMillis > 1000) {
                keyspressed = e.getKeyChar()+"";
                currentTimeMillis = System.currentTimeMillis();
            } else {
                keyspressed = keyspressed + e.getKeyChar();
                currentTimeMillis = System.currentTimeMillis();
            }
        }
    
        private String keyspressed = null;
        private long currentTimeMillis = System.currentTimeMillis();
    

提交回复
热议问题