Results 1 to 2 of 2
  1. #1
    gszauer is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Assign a keyboard key to a JButton.

    So, i need some help :)
    I want to press a key, and invoke the action of a coresponding button.
    So, for instance, if i press the key 1, i would like my jbutton (button[1]) to "be pressed"

    Here is the keyboard scanning code i wrote
    Java Code:
    public void keyReleased(KeyEvent e){
     }
     
     public void keyTyped(KeyEvent e){
     }
     
     public void keyPressed (KeyEvent e){
       keyID = e.getID();
       if (keyID == 97 || keyID == 49){
         // 1
       } else if (keyID == 98 || keyID == 50){
         // 2
       } else if (keyID == 99 || keyID == 51){
         // 3
       } else if (keyID == 100 || keyID == 52){
         // 4
       } else if (keyID == 101 || keyID == 53){
         // 5
       } else if (keyID == 102 || keyID == 54){
         // 6
       } else if (keyID == 103 || keyID == 55){
         // 7
       } else if (keyID == 104 || keyID == 56){
         // 8 
       } else if (keyID == 105 || keyID == 57){
         // 9
       } else if (keyID == 110 || keyID == 46){
         // .
       } else if (keyID == 10){
         // =
       } else if (keyID == 107){
         // +
       } else if (keyID == 109){
         // -
       } else if (keyID == 106){
         // *
       } else if (keyID == 111){
         // /
       } else if (keyID == 67){
         // C
       }
       JOptionPane.showMessageDialog(null, keyID+" Was Pressed", "Exception Error",  JOptionPane.INFORMATION_MESSAGE);
     }
    My class is
    Java Code:
    public class CalClass extends JFrame implements ActionListener, KeyListener {
    So i have
    Java Code:
    this.addKeyListener(this);
    But when i press a key i am not informed that a key was pressed by the JOptionPane, so i take im not doing something right.

    ALSO, once i have it so where its listening for keypresses correctly, how can i get it to "Press" certin buttons for me?
    For instance, where my code has //C I would like for it to execute the action that i execute when the JButton calculate = new JButton("C"); button is pressed....

    Can anyone offer any guidance?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonKeys {
        private JPanel getContent() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            for(int j = 0; j < 4; j++) {
                JButton button = new JButton("button " + (j+1));
                button.setActionCommand(String.valueOf(j+1));
                button.addActionListener(action);
                gbc.gridwidth = (j % 2 == 0) ? GridBagConstraints.RELATIVE
                                             : GridBagConstraints.REMAINDER;
                panel.add(button, gbc);
            }
            return panel;
        }
    
        private void registerKeys(JFrame f) {
            JRootPane rootPane = f.getRootPane();
            int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
            for(int j = 0; j < 4; j++) {
                String s = String.valueOf(j+1);
                rootPane.getInputMap(c).put(KeyStroke.getKeyStroke(s), s);
                rootPane.getActionMap().put(s, action);
            }
        }
    
        private Action action = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                String ac = e.getActionCommand();
                System.out.println("ac = " + ac);
                if(ac.equals("1"))
                    System.out.println("do something for button 1");
            }
        };
    
        public static void main(String[] args) {
            ButtonKeys test = new ButtonKeys();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test.getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.registerKeys(f);
        }
    }

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  2. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 PM
  3. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 04:05 PM
  4. Keyboard key are not working in solaris and linux.
    By dinesh kaushik in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2007, 04:18 AM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •