0

I want allow to append char only from keyboard text not a magnetic strip text.

setfocusable(false) 

setEnabled(false)

above both options are not allowed also keyboard inputs.

Here is my code

  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.event.KeyListener;
  import javax.swing.JDialog;
  import javax.swing.JTextArea;

  public class JTextAreaMSR extends JDialog {
        private JTextArea input;

        JTextAreaMSR() {
            super();
            setModal(true);
        }

        public static void main(String[] args) {
            JTextAreaMSR jtA = new JTextAreaMSR();
            jtA.input = new JTextArea();
            jtA.input.setFont(new java.awt.Font("Arial", Font.BOLD, 24));
            jtA.setLayout(new BorderLayout());
            jtA.add(jtA.input);
            jtA.setSize(new Dimension(400, 500));
            jtA.setLocationRelativeTo(null);
            jtA.input.addKeyListener(jtA.new MyKeyListener());
            jtA.setVisible(true);
        }

        class MyKeyListener implements KeyListener {
            @Override
            public void keyTyped(java.awt.event.KeyEvent e) {
                System.out.println("keyTyped : " + e.getKeyChar());
            }

            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                System.out.println("keyPressed : " + e.getKeyChar());
            }

            @Override
            public void keyReleased(java.awt.event.KeyEvent e) {
                System.out.println("keyReleased : " + e.getKeyChar());
            }
        }

    }

How can I restrict magnetic strip inputs only ?

Siva
  • 112
  • 11
  • How you add the text to your text area? Please provide a [mcve] so we can better understand, what's wrong in your code. – Sergiy Medvynskyy Mar 21 '18 at 08:25
  • @SergiyMedvynskyy How you add the text to your text area? Whenever swipe a card through magnetic strip , it will append char to current focus area(Similar keyboard event) Is possible to identify where to come inputs (keyboard or magnetic strip) ? – Siva Mar 21 '18 at 09:28
  • I still don't see your code. Please provide [mcve]. – Sergiy Medvynskyy Mar 21 '18 at 11:42
  • I think the card reader acts as a keyboard for the OS. That might be the problem... – keuleJ Mar 21 '18 at 12:14
  • @keuleJ Exactly card reader acts as a keyboard. But we need to differentiate input type. There is any possibilities ? Time different fail some cases. – Siva Mar 21 '18 at 14:24
  • Well i don't think you can decide from which keyboard the input came. Maybe you can identify the format of the string or something? – keuleJ Mar 21 '18 at 14:42
  • @keuleJ Input text via MSR start char '%' and end char '\n'. But , able to type those char keyboard also – Siva Mar 21 '18 at 14:56
  • @SergiyMedvynskyy Am updated my question with code. KeyListener (all methods )called both keyboard and magnetic strip reader – Siva Mar 21 '18 at 14:59
  • Maybe check the KeyEvent, perhaps it has a value to indicate the source. – matt Mar 21 '18 at 14:59
  • @matt e.getSource() return JTextArea – Siva Mar 21 '18 at 15:04
  • What about getLocation, or getModifiers. There are a bunch of fields/values that can be checked. – matt Mar 21 '18 at 15:07
  • @matt e.getModifiers return 0/1. Both msr and keyboard – Siva Mar 21 '18 at 15:23

0 Answers0