How can I Listen for Keyboard input without Text Field Java

烈酒焚心 提交于 2019-12-06 19:51:31
MadProgrammer

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

If the RFID reader acts as a keyboard input device, try with key events:

JFrame frame = new JFrame();

// frame setup

frame.addKeyListener(new KeyAdapter(){

    public void KeyPressed(KeyEvent ke)
    {

        System.out.println(ke);

    }

});

Otherwise you have to check which kind of event it fires.

little.hiti

I was in a similar situation but with a bar-code scanner.

I really worked hard on a custom DocumentListener that would take care of all scenarios, and none of it worked.

Out of desperation, I added an ActionListener and it then worked very well.

Here is a code snap-shot :

try {
    txtStockItemRef.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(txtStockItemRef.getText()); 
            DatabaseManager.peformStockItemLookupByBarcodeRef(txtStockItemRef.getText());
        } 
    }); 
} catch(Exception exc) {
    LiveCityRetailTools.handleExceptionWithUserMessage(exc);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!