1

I have a cheap magnetic card reader that acts like a keyboard. I connect it and swipe a card, and it types all the card data into the computer. But for some reason, it doesn't work in Java. It does work in everything non-Java I have tried.

What is on the card (actually swiped card in SO editor):

;00548757802?

When using Java:

;0◘)P87┼7\02?
_005487578D♥
;005487G802?
_005*J657802{
♣00548I5♣802?
;≥T548757♠╢·?
≥T54875I↑:?

As you can see, it's different every time. However, the reader sends a newline after the data, which Java does appear to recognize.

My code for reading input:

// Bound to a JTextField
public void keyTyped(KeyEvent e) {
    System.out.print(e.getKeyChar());
}

The JTextField also shows the text garbled.

What is wrong here, and how can I fix it?

Skylar Ittner
  • 802
  • 11
  • 26

2 Answers2

1

It looks like you've added a KeyListener to a JTextField, which uses Key Bindings specific to each platform. Instead, use a BufferedReader to read System.in; the readLine() method should be able to detect the line ending.

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

To avoid blocking the event dispatch thread, do the I/O in the background of a SwingWorker, publish() results as they become available, and update the JTextField in your implementation of process().

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Alright, I figured it out. My cheap $15 card reader was set to use alt-codes for data input, not plain keystrokes. I was able to reprogram it with a utility I found online to emulate a normal keyboard instead of typing alt codes for every character.

Skylar Ittner
  • 802
  • 11
  • 26