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