Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Virtual Keyboard in Java - Variable value enhancement button

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Virtual Keyboard in Java - Variable value enhancement button

    Goodmorning everyone

    I suggest that my knowledge in Java is minimal so I apologize in advance if the question is posed bad or it may be trivial to the most experienced.

    on the net I recovered the source code of a virtual keyboard in Java.

    I've customized all the buttons but I would now be able to insert buttons that instead of simulating the key pressed and sending the key command, valorizing the variables that will then use to have different viewing modes

    below part of the code that I modified

    VirtualKeypadPanel.java:


    int modality = 0;
    setLayout (GridBagLayout);
    setBorder (BorderFactory.createEmptyBorder (4, 4, 4, 4));
    // column, row, size.
    if (modalita == 0) {
    addKeyButton (0, 1, 1, "q", KeyAction.forKeyType (KeyEvent.VK_Q));
    addKeyButton (1, 1, 1, "w", KeyAction.forKeyType (KeyEvent.VK_W));
    addKeyButton (2, 1, 1, "e", KeyAction.forKeyType (KeyEvent.VK_E));
    addKeyButton (3, 1, 1, "r", KeyAction.forKeyType (KeyEvent.VK_R));
    addKeyButton (4, 1, 1, "t", KeyAction.forKeyType (KeyEvent.VK_T));
    addKeyButton (5, 1, 1, "y", KeyAction.forKeyType (KeyEvent.VK_Y));
    addKeyButton (6, 1, 1, "u", KeyAction.forKeyType (KeyEvent.VK_U));
    addKeyButton (7, 1, 1, "i", KeyAction.forKeyType (KeyEvent.VK_I));
    addKeyButton (8, 1, 1, "o", KeyAction.forKeyType (KeyEvent.VK_O));
    addKeyButton (9, 1, 1, "p", KeyAction.forKeyType (KeyEvent.VK_P));


    addKeyButton (10, 4, 1, "Mode", ???????????)
    }


    if (modalita == 1) {

    addKeyButton (0, 1, 1, "Q", KeyAction.forKeyType (KeyEvent.VK_Q));
    addKeyButton (1, 1, 1, "W", KeyAction.forKeyType (KeyEvent.VK_W));
    addKeyButton (2, 1, 1, "E", KeyAction.forKeyType (KeyEvent.VK_E));
    addKeyButton (3, 1, 1, "R", KeyAction.forKeyType (KeyEvent.VK_R));
    addKeyButton (4, 1, 1, "T", KeyAction.forKeyType (KeyEvent.VK_T));
    addKeyButton (5, 1, 1, "Y", KeyAction.forKeyType (KeyEvent.VK_Y));
    addKeyButton (6, 1, 1, "U", KeyAction.forKeyType (KeyEvent.VK_U));
    addKeyButton (7, 1, 1, "I", KeyAction.forKeyType (KeyEvent.VK_I));
    addKeyButton (8, 1, 1, "O", KeyAction.forKeyType (KeyEvent.VK_O));
    addKeyButton (9, 1, 1, "P", KeyAction.forKeyType (KeyEvent.VK_P));


    addKeyButton (10, 4, 1, "Mode", ???????????)
    }


    private void addKeyButton (int gridx, int gridy, int columns,
    String buttonText, KeyAction ... keyActions) {
    KeyButton keyButton = new KeyButton (buttonText, keyActions);
    keyButton.setFont (buttonFont); // Same Font for all buttons.

    addButton (gridx, gridy, columns, keyButton);
    }

    private void addButton (int gridx, int grid, int columns, keybutton keyButton) {
    keyButton.addActionListener (ActionListener); // Same ActionListener for all buttons.

    GridBagConstraints gbc = new GridBagConstraints ();
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.gridwidth = columns;
    gbc.weightx = 3.0;
    gbc.weighty = 3.0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets (4, 4, 4, 4);

    add (keyButton, gbc);
    }


    for convenience I did not put all the keys in the post.

    by manually setting the modifier variable to 0 or 1, I see the keyboard with uppercase and lowercase characters, the last button, and the mode button that must highlight the mode variable

    this is the code of the other classes

    KeyButton.java



    import javax.swing.JButton;

    / *
    * KeyButton is a JButton with one or more KeyAction objects.
    * /
    public class KeyButton extends JButton {
    private static final long serialVersionUID = 1L;

    private KeyAction [] keyActions;

    public KeyButton (String text, KeyAction keyAction) {
    super (text);
    keyActions = new KeyAction [] {keyAction};
    }

    public KeyButton (String text, KeyAction [] keyActions) {
    super (text);
    this.keyActions = keyActions.clone ();
    }

    public KeyAction [] getKeyActions () {
    return keyActions.clone ();
    }
    }



    KeyAction.java:


    / *
    * KeyAction defines immutable objects representing a "action" of a keyboard key.
    * /
    public class KeyAction {
    private final int code;
    private final Mode mode;

    public KeyAction (int code, mode mode) {
    if (mode == null) {
    throw new IllegalArgumentException ("mode can not be null");
    }

    this.code = code;
    this.mode = mode;
    }

    public int getCode () {
    return code;
    }

    public mode getMode () {
    return mode;
    }

    // Factory method for a "press" action.
    public static KeyAction forKeyPress (int code) {
    return new KeyAction (code, Mode.PRESS);
    }

    // Factory method for a "release" action.
    public static KeyAction forKeyRelease (int code) {
    return new KeyAction (code, Mode.RELEASE);
    }

    // Factory method for a "type" action.
    public static KeyAction forKeyType (int code) {
    return new KeyAction (code, Mode.TYPE);
    }


    public enum Mode {
    PRESS, // press of a key
    RELEASE, // release of a key
    TYPE // type (press & release) of a key
    }
    }




    Can anybody tell me (and where) should i put on the mode button to set the mode variable?

    Thank you 1000 for the collaboration

    Andrew

  2. #2

    Default Re: Virtual Keyboard in Java - Variable value enhancement button

    In one technology, the keyboard is projected optically on a flat surface and, as the user touches the image of a key, the optical device detects the stroke and sends it to the computer. Very nyc topic, well explained , thanks for sharing.

  3. #3

    Default Re: Virtual Keyboard in Java - Variable value enhancement button

    In one technology, the keyboard is projected optically on a flat surface and, as the user touches the image of a key, the optical device detects the stroke and sends it to the computer. Very nyc topic, well explained , thanks for sharing.

  4. #4
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Virtual Keyboard in Java - Variable value enhancement button

    Generics. So key buttons don't need sequential order. Interface classes are API and AWT buttons fall under public packaging. Pressing on the buttons in an applet returns some text message. This is called encpsulation. A value of -1 or 1, as an example on button1 variable.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Virtual Keyboard in Java - Variable value enhancement button

    Quote Originally Posted by planetHollywood View Post
    Generics. So key buttons don't need sequential order. Interface classes are API and AWT buttons fall under public packaging. Pressing on the buttons in an applet returns some text message. This is called encpsulation. A value of -1 or 1, as an example on button1 variable.
    I'm sorry, but I don't really see how "Generics" is an answer to the question the OP asked: "Can anybody tell me (and where) should i put on the mode button to set the mode variable?"

    In fact, it's not clear what the question meant in the first place. A better approach would be to ask the OP what they meant and, if they don't respond, let the matter drop.

    ---

    In general posting somebody else's code (with typos, and which does not compile) is simply not a good place for the OP to start. I would point that out to the OP along with a link to a good tutorial and advise them to write their own code, and post a specific question about it. Except that they left the forum a couple of months ago.

Similar Threads

  1. image enhancement project for java beginner?
    By Sanika.P in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 27th, 2014, 03:59 AM
  2. Keyboard Driven Automation using Java - Store Vector data into variable
    By danielcatanoro in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2013, 12:41 PM
  3. Replies: 2
    Last Post: May 2nd, 2013, 03:35 AM
  4. Assigning a variable through a radio button selection
    By smarmy_cheauffeur in forum AWT / Java Swing
    Replies: 1
    Last Post: October 17th, 2011, 12:52 PM
  5. Virtual Keyboard in Java
    By Brian in forum Java Theory & Questions
    Replies: 2
    Last Post: June 2nd, 2010, 11:35 PM