Results 1 to 4 of 4
  1. #1
    XxClouDxx is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Exclamation On Screen Keyboard help

    I am new to Java GUI and I wanted to make an onscreen keyboard.
    I just need help regarding the caps lock button and the shift button..

    any ideas on to how to implement the caps lock and shift function?

    here's the code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    @
    SuppressWarnings("serial")
    public class MainFrame extends JFrame {
            private JTextField textField;
            private PopUpKeyboard keyboard;
        public MainFrame() {
            super("pop-up keyboard");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            textField = new JTextField(20);
            keyboard = new PopUpKeyboard(textField);
     
            textField.addMouseListener(new MouseAdapter() {@
                Override
                public void mouseClicked(MouseEvent e) {
                    Point p = textField.getLocationOnScreen();
                    p.y += 30;
                    keyboard.setLocation(p);
                    keyboard.setVisible(true);
                }
            });
            setLayout(new FlowLayout());
            add(textField);
     
            pack();
            setLocationByPlatform(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {@
                Override
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
     
        private class PopUpKeyboard extends JDialog implements ActionListener {
            private JTextField textField;
     
            public PopUpKeyboard(JTextField textField) {
                    this.textField=textField;
                this.setResizable(false);
                this.getContentPane().setPreferredSize(new Dimension(1000, 250));
                this.setLocation(50, 50);
                keyboardVariables();
     
            }
     
            private void keyboardVariables() {
     
                        String firstRow[] = {
                            "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "Back Space"
                        };
                        String secondRow[] = {
                            "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "|"
                        };
                        String thirdRow[] = {
                            "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"
                        };
                        String fourthRow[] = {
                            "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?"
                        };
                        String fifthRow[] = {
                            "Space"
                        };
     
                        JButton first[];
                        JButton second[];
                        JButton third[];
                        JButton fourth[];
                        JButton fifth[];
     
                setLayout(new BorderLayout());
                Font font;
                float size;
                JPanel jpNorth = new JPanel();
                JPanel jpCenter = new JPanel();
                JPanel jpKeyboard = new JPanel();
                JPanel jpNote = new JPanel();
                add(jpNorth, BorderLayout.NORTH);
                add(jpCenter, BorderLayout.CENTER);
                add(jpKeyboard, BorderLayout.SOUTH);
                jpNorth.setLayout(new BorderLayout());
                jpCenter.setLayout(new BorderLayout());
                jpKeyboard.setLayout(new GridLayout(5, 1));
                pack();
     
                first = new JButton[firstRow.length];
                JPanel p = new JPanel(new GridLayout(1, firstRow.length));
                for (int i = 0; i < firstRow.length; i++) {
                    JButton b = new JButton(firstRow[i]);
                    b.setPreferredSize(new Dimension(100, 50));
                    b.addActionListener(this);
                    first[i] = b;
                    p.add(first[i]);
                }
                jpKeyboard.add(p);
     
                second = new JButton[secondRow.length];
                p = new JPanel(new GridLayout(1, secondRow.length));
                for (int i = 0; i < secondRow.length; ++i) {
                    second[i] = new JButton(secondRow[i]);
                    second[i].addActionListener(this);
                    p.add(second[i]);
     
                }
                jpKeyboard.add(p);
     
                third = new JButton[thirdRow.length];
                p = new JPanel(new GridLayout(1, thirdRow.length));
                JToggleButton toggleButton = new JToggleButton("CapsLock");
                toggleButton.addActionListener(this);
                p.add(toggleButton);
                for (int i = 0; i < thirdRow.length; ++i) {
                    third[i] = new JButton(thirdRow[i]);
                    third[i].addActionListener(this);
                    p.add(third[i]);
                }
                jpKeyboard.add(p);
     
                fourth = new JButton[fourthRow.length];
                p = new JPanel(new GridLayout(1, fourthRow.length));
                for (int i = 0; i < fourthRow.length; ++i) {
                    fourth[i] = new JButton(fourthRow[i]);
                    fourth[i].addActionListener(this);
                    p.add(fourth[i]);
                }
     
                p.add(new JPanel());
                jpKeyboard.add(p);
                fifth = new JButton[fifthRow.length];
                p = new JPanel(new GridLayout(1, fifthRow.length));
                for (int i = 0; i < fifthRow.length; ++i) {
                    JButton b = new JButton(fifthRow[i]);
                                    fifth[i] = b;
                    fifth[i].addActionListener(this);
                    b.setPreferredSize(new Dimension(20, 10));
                    p.add(fifth[i]);
                }
                jpKeyboard.add(p);
            }
     
                    public void actionPerformed(ActionEvent e) {
                            String action = e.getActionCommand();
                            boolean capslock = false;
                            boolean shift = true;
                            if(action == "Back Space"){
                                    textField.setText(textField.getText().substring(0, textField.getText().length()-1));
                            }else if (action == "Tab"){
                                    textField.setText(textField.getText() + "\t");
                            }else if (action == "Space"){
                                    textField.setText(textField.getText() + " ");
                            }else if (action == "Enter"){
                                    textField.setText(textField.getText() + "\n");
                            }else if(action == "Shift"){
                                    shift = false; //here
                            }else if(action == "CapsLock"){
                                    textField.setText(textField.getText().toUpperCase()); //here
                            }
                            else{
                                    textField.setText(textField.getText() + action.toLowerCase());
                            }
                    }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: On Screen Keyboard help

    Have a look at the Toolkit.getLockingKeyState(int keyCode) method for your caps lock problem. The shift key generates its own key event (VK_SHIFT).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    XxClouDxx is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: On Screen Keyboard help

    Quote Originally Posted by JosAH View Post
    Have a look at the Toolkit.getLockingKeyState(int keyCode) method for your caps lock problem. The shift key generates its own key event (VK_SHIFT).
    Can you give me a sample code on how to use it? I can't seem to implement it on my code. Sorry, I'm such a beginner at this.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: On Screen Keyboard help

    First off, do not compare Strings with ==:
    Java Code:
    action == "Back Space"
    use the equals() method instead.
    In this case it's working by fluke, rather than design.

    Second:
    Java Code:
    textField.setText(textField.getText() + action.toLowerCase());
    you should determine whether to use lower or upper case here based on some flag setting.
    You seem to have a similar flag for shift, but you need one to cover the caps lock.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. embed link to on-screen keyboard
    By nknigi in forum New To Java
    Replies: 2
    Last Post: 03-02-2013, 09:30 AM
  2. Replies: 1
    Last Post: 02-08-2012, 01:08 PM
  3. Printing keyboard input to screen.
    By adwodon in forum New To Java
    Replies: 4
    Last Post: 12-16-2010, 10:40 AM
  4. Help with on screen keyboard Capslock
    By linkinlog0123 in forum AWT / Swing
    Replies: 2
    Last Post: 11-17-2010, 05:46 AM
  5. Need help with on-screen keyboard (first post)
    By tom_k in forum New To Java
    Replies: 7
    Last Post: 07-31-2008, 02:07 AM

Tags for this Thread

Posting Permissions

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