I am not sure where to go with shift and caps key, I don't know whether I can make different values on what I am already working on for this or not. I am new to Jpanel, so any help would be much appreciated.
This is my current code in VSCode right now. It will pull up a 5 by 12 keyboard that reads inputs and will output into the console on enter. Tab, Caps, Shift, and delete currently don't work. I am mostly focused on caps and shift right now, as I can get the other two pretty easily.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.lang.model.util.ElementScanner6;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.plaf.ComponentUI;
public class Keyboard extends JFrame implements ActionListener {
private JButton[] buttons;
private JPanel keyboardPanel;
public String userInput = "";
public ArrayList<String> keyboardKeys = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=",
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "\\", "delete",
"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "enter",
"shift", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "shift",
"caps", "tab", "space", "`", "~", "!", "@", "(", ")", "?", "[", "]"));
public static ComponentUI newUI = new ComponentUI() {
};
public Keyboard() {
super("Keyboard");
keyboardPanel = new JPanel();
keyboardPanel.setLayout(new GridLayout(5, 12));
buttons = new JButton[60];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(keyboardKeys.get(i));
buttons[i].addActionListener(this);
keyboardPanel.add(buttons[i]);
}
add(keyboardPanel, BorderLayout.CENTER);
setSize(900, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
JButton source = (JButton) event.getSource();
//System.out.println(source.getText());
//collect inputs into a String
if(source.getText() == "shift")
{
//change keyboard for shift and caps keystrokes
System.out.println("shift key");
}
else if(source.getText() == "caps")
{
//change keyboard for caps keystrokes
System.out.println("caps key");
}
else if((source.getText() == "enter"))
{
//on enter, send the input array out
//System.out.println("enter key");
System.out.println(userInput);
}
else if(source.getText() == "space")
{
System.out.println("space bar");
userInput = userInput + " ";
}
else
{
userInput = userInput + source.getText();
}
// Do something with the button press, such as sending a value to the PLC or
// updating a display
}
public static void main(String[] args) {
new Keyboard();
}
}