How to read data from a barcode scanner using JavaFX

I have a barcode scanner that I can read when I focus on the text field without any problems, like on a keyboard.

My question is, how can I read the input of a barcode, if I do not focus on the text field in another word, how to get an event listener to listen to a barcode scanner.

+4
source share
2 answers

It should work in the analog Java Swing solution presented here .

, , -, - ENTER. , , , - , . ..

- , . , StringBuffer reset, .

KeyListener :

private final StringBuffer barcode = new StringBuffer();
private long lastEventTimeStamp = 0L;

// ...

public void keyTyped(KeyEvent keyEvent) {
    long now = Instant.now().toEpochMilli();

    // events must come fast enough to separate from manual input
    if (now - this.lastEventTimeStamp > this.threshold) {
        barcode.delete(0, barcode.length());
    }
    this.lastEventTimeStamp = now;

    // ENTER comes as 0x000d
    if (keyEvent.getCharacter().charAt(0) == (char) 0x000d) {
        if (barcode.length() >= this.minBarcodeLength) {
            System.out.println("barcode: " + barcode);
        }
        barcode.delete(0, barcode.length());
    } else {
        barcode.append(keyEvent.getCharacter());
    }
    keyEvent.consume();
}

, , , , FXML GridPane -. .

. KeyEvent.KEY_TYPED KeyCode, :

if (event.getCode().equals(KeyCode.ENTER)) {
//...
}
+3

@l00tr

public final class BarcodeAccumulator {

    public interface OnBarcodeListener {

        void onBarcodeScanned(String barcode);
    }

    private static final char BACKSPACE = '\b';
    private static final char CONTROL_V = 0x0016;
    private static final char CONTROL_Z = 0x001A;
    private static final char CARRIAGE_RETURN = 0x000d;

    private final StringBuffer buffer = new StringBuffer();

    private OnBarcodeListener barcodeListener;

    public void setBarcodeListener(OnBarcodeListener barcodeListener) {
        this.barcodeListener = barcodeListener;
    }

    public void accumulate(final TextField textField) {
        if (Objects.nonNull(textField)) {
            textField.setOnKeyTyped(event -> {
                for (char character : event.getCharacter().toCharArray()) {
                    if (Character.isISOControl(character)) {
                        if (textField.isFocused()) {
                            if (character == BACKSPACE) {
                                if (buffer.length() > textField.getCaretPosition()) {
                                    remove(textField.getCaretPosition());
                                }
                                break;
                            }
                            if (character == CONTROL_V) {
                                add(textField.getText());
                                break;
                            }
                            if (character == CONTROL_Z) {
                                clear();
                                add(textField.getText());
                                break;
                            }
                            if (character == CARRIAGE_RETURN) {
                                notifyListener(buffer.toString());
                                clear();
                                break;
                            }
                        }
                        continue;
                    }
                    add(String.valueOf(character));
                }
            });
        }
    }

    public void add(String character) {
        buffer.append(character);
    }

    public void remove(int position) {
        buffer.deleteCharAt(position);
    }

    public void clear() {
        buffer.delete(0, buffer.length());
    }

    private void notifyListener(String barcode) {
        if (Objects.nonNull(barcodeListener)) barcodeListener.onBarcodeScanned(barcode);
    }

, ctrl + v .., . , "backspace", , - , . , 3 , , , - =)

0

Source: https://habr.com/ru/post/1598668/


All Articles