Search code examples
javakeyboardrfid

How can I Listen for Keyboard input without Text Field Java


I am working on a Java application and interfacing with an RFID reader that acts as a keyboard input device.

The application will be used for employee time tracking, so the employee should not see the code that his/her RFID tag contains.

Currently, the application opens a jFrame that asks the employee to scan their tag. This is where I would like to listen for the keyboard input.

All of the RFID tags are 10 digits, so I would like to use some kind of regex to detect when a card is scanned if possible.

If someone could point me in the right direction, or contribute some code I would be grateful.

Thanks in advance.

UPDATE:

I was able to read the input of the scanner by adding the following to the constructor of my JFrame.

addKeyListener(new KeyListener(){
    @Override
    public void keyPressed(KeyEvent e){ System.out.print(e.getKeyChar());}
    @Override
    public void keyReleased(KeyEvent e) { }
    @Override
    public void keyTyped(KeyEvent e) { }
});

So it is now confirmed that the Reader is just standard Keyboard input.

Here is an example of what I get for a tag: 0006459027

Now, the big question is, how do I take the characters that I got, and detect that it is a 10 digit string, and from there trigger an event to open a new frame?


Solution

  • First, I'd see if the RFID reader is triggering an ActionEvent to be fired when the tag is scanned. This would be the simplest approach.

    Failing that, you would need to attach a DocumentListener to the fields underlying document and monitor for changes.

    You'll need to decide how best to interrupt the results (as you're likely to get each letter of the RFID at a time). You could monitor the length of the document or have a javax.swing.Timer which triggers after a short delay (you'd reset the timer on each update event triggered by the DocumentListener)

    Check out

    I'd suggest taking a look at DocumentFilter as well, but your interested in the final result, not modifying it.

    UPDATED with DocumentListener Example

    // In the classes variable decleration section...
    private JTextField rfidField;
    
    // In the classes constructor or UI init method...
    rfidField = new JTextField(12);
    rfidField.getDocument().addDocumentListener(new DocumentListener() {
    
        public void handleUpdate(DocumentEvent e) {
    
            if (e.getDocument().getLength() == 10) {
    
                System.out.println("Trigger me happy...");
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        rfidField.setText(null);
                    }
                });
    
            }
    
        }
    
        @Override
        public void insertUpdate(DocumentEvent e) {
            handleUpdate(e);
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
            handleUpdate(e);
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {
            handleUpdate(e);
        }
    });
    
    // Now don't forget to add the field to your forms container ;)
    //////
    

    One of things I would be do when you "trigger" the code event is, once you've read it from the text field, is clear the text field (JTextField.setText(null)) - IMHO