Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:42:08+00:00 2026-06-09T13:42:08+00:00

I am developing a system that allows the user to scan barcodes. The barcode

  • 0

I am developing a system that allows the user to scan barcodes. The barcode scanner effectively behaves like a keyboard, “typing” each digit of the barcode at super-human speeds. For the sake of this example, let’s say that most amount of time between successive “key strokes” is 10 milliseconds.

I began by implementing an EventHandler that listens for numeric KeyEvents on the application’s Window. When a KeyEvent arrives, the handler does not yet know if it was entered by a human or by a barcode scanner (it will know 10 milliseconds from now). Unfortunately, I must make a decision now or risk locking up JavaFX’s main thread, so I automatically call keyEvent.consume() to prevent it from being handled.

After 10 milliseconds have elapsed, a timer wakes up and decides whether or not the KeyEvent was part of a barcode. If it was, the KeyEvents are concatenated together and handled by the barcode processing logic. Otherwise, I want to let the application handle the KeyEvent normally.

How can I force the application to handle a KeyEvent after I have already called keyEvent.consume() on it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:42:10+00:00Added an answer on June 9, 2026 at 1:42 pm

    Here is my take on how this might be done.

    The solution works by filtering the key events for the app, cloning them and placing the cloned events in a queue, then consuming the original events in the filter. The cloned event queue is processed at a later time. Events from the barcode reader are not refired. Events that are not from the barcode reader are refired so that the system can process them. Data structures keep track of whether the events have been processed already or not, so that the system can know in the event filter whether it truly has to intercept and consume the events or let them pass through to the standard JavaFX event handlers.

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    // delays event key press handling so that some events can be intercepted
    // and routed to a bar code a reader and others can be processed by the app.
    public class EventRefire extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(final Stage stage) throws Exception {
        // create the scene.
        final VBox layout = new VBox();
        final Scene scene = new Scene(layout);
    
        // create a queue to hold delayed events which have not yet been processed.
        final List<KeyEvent> unprocessedEventQueue = new ArrayList();
        // create a queue to hold delayed events which have already been processed.
        final List<KeyEvent> processedEventQueue = new ArrayList();
    
        // create some controls for the app.
        final TextField splitterField1 = new TextField(); splitterField1.setId("f1");
        final TextField splitterField2 = new TextField(); splitterField2.setId("f2");
        final Label forBarCode   = new Label();
        final Label forTextField = new Label();
    
        // filter key events on the textfield and don't process them straight away.
        stage.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {
          @Override public void handle(KeyEvent event) {
            if (event.getTarget() instanceof Node) {
              if (!processedEventQueue.contains(event)) {
                unprocessedEventQueue.add((KeyEvent) event.clone());
                event.consume();
              } else {
                processedEventQueue.remove(event);
              }  
            }  
          }
        });
    
        // set up a timeline to simulate handling delayed event processing from 
        // the barcode scanner.
        Timeline timeline = new Timeline(
          new KeyFrame(
            Duration.seconds(1), 
            new EventHandler() {
              @Override public void handle(Event timeEvent) {
                // process the unprocessed events, routing them to the barcode reader
                // or scheduling the for refiring as approriate.
                final Iterator<KeyEvent> uei = unprocessedEventQueue.iterator();
                final List<KeyEvent> refireEvents = new ArrayList();
                while (uei.hasNext()) {
                  KeyEvent event = uei.next();
                  String keychar = event.getCharacter();
                  if ("barcode".contains(keychar)) {
                    forBarCode.setText(forBarCode.getText() + keychar);
                  } else {
                    forTextField.setText(forTextField.getText() + keychar);
                    refireEvents.add(event);
                  }
                }
    
                // all events have now been processed - clear the unprocessed event queue.
                unprocessedEventQueue.clear();
    
                // refire all of the events scheduled to refire.
                final Iterator<KeyEvent> rei = refireEvents.iterator();
                while (rei.hasNext()) {
                  KeyEvent event = rei.next();
                  processedEventQueue.add(event);
                  if (event.getTarget() instanceof Node) {
                    ((Node) event.getTarget()).fireEvent(event);
                  }
                }
              }
            }
          )
        );
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();
    
        // layout the scene.
        final GridPane grid = new GridPane();
        grid.addRow(0, new Label("Input Field 1:"), splitterField1);
        grid.addRow(1, new Label("Input Field 2:"), splitterField2);
        grid.addRow(2, new Label("For App:"),       forTextField);
        grid.addRow(3, new Label("For BarCode:"),   forBarCode);
        grid.setStyle("-fx-padding: 10; -fx-vgap: 10; -fx-hgap: 10; -fx-background-color: cornsilk;");
    
        Label instructions = new Label("Type letters - key events which generate the lowercase letters b, a, r, c, o, d, e will be routed to the barcode input processor, other key events will be routed back to the app and processed normally.");
        instructions.setWrapText(true);
        layout.getChildren().addAll(grid, instructions);
        layout.setStyle("-fx-padding: 10; -fx-vgap: 10; -fx-background-color: cornsilk;");
        layout.setPrefWidth(300);
        stage.setScene(scene);
        stage.show();
      }
    }
    

    Sample program output:

    Sample program output

    Because I use a Timeline everything in my code runs on the FXApplicationThread, so I don’t have to worry about concurrency in my implementation. In implementation with a real barcode reader and barcode event processor, you may need some added concurrency protection as possibly multiple threads will be involved. Also you might not need the Timeline used in my code to simulate the delayed processing of the barcode system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a documents system that, each time that a new one is created,
I am developing a system that allows userwritten javascript in widgets. To keep things
I'm developing a system that allows developers to upload custom groovy scripts and freemarker
I'm developing on a system that was originally developed five years ago. I don't
I'm developing a system in Ruby that is going to make use of RabbitMQ
I'm developing the server part of a system that has to send messages to
I am developing a Django application, which is a large system that requires multiple
I'm developing a system, and I have build a code generator that emits a
I'm developing online examination system in php-mysql. That exam will consist of multiple-choice questions;
I'm developing an app that starts a System.Threading.Timer which does some fairly rapid reading/writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.