KeyEvent listener: mapping physical keyboard to onscreen keyboard [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have an assignment to create a GUI with a keyboard and text area. Conceptually, the user would type on the physical keyboard and the respective key would change background on screen to reflect that key press. Pressing the onscreen keys yields no behaviour so I don't need to implement keyTyped(...) (I think).



For brevity and as per SO's axiomatic policies, here's some relevant code (let me know if there's something to add)



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class


How do I map the physicals key pressed with the on screen keyboard with KeyEvent?




Edit:



Full code



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class






share|improve this question













closed as off-topic by Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t Aug 1 at 7:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.












  • "How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
    – Ludisposed
    Jul 31 at 17:29










  • Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
    – I'll add comments tomorrow
    Jul 31 at 17:30










  • No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
    – legoMyEgo
    Jul 31 at 17:31










  • Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
    – Ludisposed
    Jul 31 at 17:46










  • It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
    – I'll add comments tomorrow
    Jul 31 at 17:50

















up vote
2
down vote

favorite












I have an assignment to create a GUI with a keyboard and text area. Conceptually, the user would type on the physical keyboard and the respective key would change background on screen to reflect that key press. Pressing the onscreen keys yields no behaviour so I don't need to implement keyTyped(...) (I think).



For brevity and as per SO's axiomatic policies, here's some relevant code (let me know if there's something to add)



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class


How do I map the physicals key pressed with the on screen keyboard with KeyEvent?




Edit:



Full code



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class






share|improve this question













closed as off-topic by Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t Aug 1 at 7:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.












  • "How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
    – Ludisposed
    Jul 31 at 17:29










  • Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
    – I'll add comments tomorrow
    Jul 31 at 17:30










  • No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
    – legoMyEgo
    Jul 31 at 17:31










  • Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
    – Ludisposed
    Jul 31 at 17:46










  • It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
    – I'll add comments tomorrow
    Jul 31 at 17:50













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have an assignment to create a GUI with a keyboard and text area. Conceptually, the user would type on the physical keyboard and the respective key would change background on screen to reflect that key press. Pressing the onscreen keys yields no behaviour so I don't need to implement keyTyped(...) (I think).



For brevity and as per SO's axiomatic policies, here's some relevant code (let me know if there's something to add)



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class


How do I map the physicals key pressed with the on screen keyboard with KeyEvent?




Edit:



Full code



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class






share|improve this question













I have an assignment to create a GUI with a keyboard and text area. Conceptually, the user would type on the physical keyboard and the respective key would change background on screen to reflect that key press. Pressing the onscreen keys yields no behaviour so I don't need to implement keyTyped(...) (I think).



For brevity and as per SO's axiomatic policies, here's some relevant code (let me know if there's something to add)



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class


How do I map the physicals key pressed with the on screen keyboard with KeyEvent?




Edit:



Full code



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ButtonInPane extends JFrame implements KeyListener
// input
String input;
//context
JLabel context1, context2;
// default color
Color defaultColor = new JButton().getBackground();
// main rows of keys
public String rowOne =
"~",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"+",
"h"
;
public String rowTwo =
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"\"
;
public String rowThree =
"Caps",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
":",
"'",
"Enter"
;
public String rowFour =
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"?",
" ^"
;
public String rowFive =
" ",
"<",
"v",
">"
;
/**
* Account for chars with no shift: Program toggles Shift key, meaning if a
* user clicks on it, all keys will be toggled to their respective shift
* value. The user can tap the shift key again to change back to regular
* value
*/
public String shiftless =
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"[",
"]",
"\",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
";",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
",",
".",
"/"
;
// Account for special chars
public String specialChars =
"~",
"-",
"+",
"[",
"]",
"\",
";",
".",
"?"
;

// declare rows of buttons
public JButton buttons_rowOne, buttons_rowTwo, buttons_rowThree, buttons_rowFour, buttons_rowFive;
private JTextArea body;
private JPanel top;
private JPanel middle;
private JPanel bottom;
private JPanel contextBox;

// ctor
public ButtonInPane()
super("Typing Tutor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 600));
this.setLocation(50, 50);
this.setVisible(true);
__init__();


public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox)
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(contextBox);
add(middle, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);


public void __init__body()
body = new JTextArea();
body.setPreferredSize(new Dimension(1000, 150));
body.addKeyListener(this);


public void __init__panels()
context1 = new JLabel("Type some text using your keyboard. " +
"The keys you press will be highlighed and the text will be displayed.");
context2 = new JLabel("nNote: Clicking the buttons with your mouse will not perform any action.");
context1.setFont(new Font("Verdana", Font.BOLD, 14));
context2.setFont(new Font("Verdana", Font.BOLD, 14));
top = new JPanel();
top.setSize(new Dimension(500, 500));
middle = new JPanel();
bottom = new JPanel();
contextBox = new JPanel();
__init__layout(top, middle, bottom, contextBox);
top.setLayout(new BorderLayout());
bottom.setLayout(new GridLayout(5, 5));
top.add(context1);
top.add(context2);
middle.setLayout(new BorderLayout());
middle.add(body, BorderLayout.WEST);
middle.add(body, BorderLayout.CENTER);


public void __init__()
// text area
__init__body();
// panels for layout
__init__panels();
pack();
// get length of row strings
int length_rowOne = rowOne.length;
int length_rowTwo = rowTwo.length;
int length_rowThree = rowThree.length;
int length_rowFour = rowFour.length;
int length_rowFive = rowFive.length;
// create array for each row of buttons
buttons_rowOne = new JButton[length_rowOne];
buttons_rowTwo = new JButton[length_rowTwo];
buttons_rowThree = new JButton[length_rowThree];
buttons_rowFour = new JButton[length_rowFour];
buttons_rowFive = new JButton[length_rowFive];
// create panel for each row of buttons
JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
// draw out the rows of buttons
draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);



// draw rows of buttons
public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5)
for (int i = 0; i < s1; i++)
JButton currentButton = new JButton(rowOne[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowOne[i] = currentButton;
r1.add(buttons_rowOne[i]);

for (int i = 0; i < s2; i++)
JButton currentButton = new JButton(rowTwo[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowTwo[i] = currentButton;
r2.add(buttons_rowTwo[i]);

for (int i = 0; i < s3; i++)
JButton currentButton = new JButton(rowThree[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowThree[i] = currentButton;
r3.add(buttons_rowThree[i]);

for (int i = 0; i < s4; i++)
JButton currentButton = new JButton(rowFour[i]);
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFour[i] = currentButton;
r4.add(buttons_rowFour[i]);

for (int i = 0; i < s5; i++)
JButton currentButton = new JButton(rowFive[i]);
// account for space bar
if (i == 1)
currentButton = new JButton(rowFive[i]);
currentButton.setPreferredSize(new Dimension(400, 10));
currentButton.setBounds(10, 10, 600, 100);
buttons_rowFive[i] = currentButton;
else
currentButton.setPreferredSize(new Dimension(100, 50));
buttons_rowFive[i] = currentButton;

r5.add(buttons_rowFive[i]);

bottom.add(r1);
bottom.add(r2);
bottom.add(r3);
bottom.add(r4);
bottom.add(r5);
// !draw(...)
// called when a button is pressed

@Override
public void keyPressed(KeyEvent press)
Object current = press.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(Color.BLACK);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(Color.BLACK);
repaint();


// !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release)
Object current = release.getSource();
for (int i = 0; i < 14; i++)
if (current.toString() == rowOne[i])
buttons_rowOne[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowTwo[i])
buttons_rowTwo[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowThree[i])
buttons_rowThree[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFour[i])
buttons_rowFour[i].setBackground(defaultColor);
repaint();
else if (current.toString() == rowFive[i])
buttons_rowFive[i].setBackground(defaultColor);
repaint();


// !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed)
// Object current = typed.getSource().toString();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 14; i++)
// if (current == rowOne[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowTwo[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowThree[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFour[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
// else if (current == rowFive[i])
// sb.append(typed.getKeyCode());
// body.append(sb.toString());
//
//

// main method
public static void main(String args)
new ButtonInPane();
// !main method

private static final long serialVersionUID = 999;
// !main class








share|improve this question












share|improve this question




share|improve this question








edited Jul 31 at 17:28
























asked Jul 31 at 17:13









legoMyEgo

112




112




closed as off-topic by Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t Aug 1 at 7:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t Aug 1 at 7:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Ludisposed, Stephen Rauch, yuri, Sam Onela, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.











  • "How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
    – Ludisposed
    Jul 31 at 17:29










  • Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
    – I'll add comments tomorrow
    Jul 31 at 17:30










  • No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
    – legoMyEgo
    Jul 31 at 17:31










  • Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
    – Ludisposed
    Jul 31 at 17:46










  • It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
    – I'll add comments tomorrow
    Jul 31 at 17:50

















  • "How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
    – Ludisposed
    Jul 31 at 17:29










  • Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
    – I'll add comments tomorrow
    Jul 31 at 17:30










  • No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
    – legoMyEgo
    Jul 31 at 17:31










  • Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
    – Ludisposed
    Jul 31 at 17:46










  • It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
    – I'll add comments tomorrow
    Jul 31 at 17:50
















"How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
– Ludisposed
Jul 31 at 17:29




"How do I map the physicals key pressed with the on screen keyboard with KeyEvent?" This would make it off topic. Is your code currently working as intended?
– Ludisposed
Jul 31 at 17:29












Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
– I'll add comments tomorrow
Jul 31 at 17:30




Links can go blant, I edited the full code in. Please also take note that we are very pedantic about code here. Your abreviated version had different formatting applied: rowOne was on a single line, whereas the pastebin version has it in several lines. This might be something a code reviewer would like to discuss.
– I'll add comments tomorrow
Jul 31 at 17:30












No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
– legoMyEgo
Jul 31 at 17:31




No it is not, I was referred here to CR from SO because it compiles and runs but doesn't work as intended. I made an edit with all of the code. Im trying to ensure that with each physical key press, the respective on-screen key gets its background changed once, and on release changed again.
– legoMyEgo
Jul 31 at 17:31












Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
– Ludisposed
Jul 31 at 17:46




Code Review is a community where programmers peer-review your working code.This is off topic on Code-Review, I don't get why you got sent here for.
– Ludisposed
Jul 31 at 17:46












It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
– I'll add comments tomorrow
Jul 31 at 17:50





It's unfortunate that you have been misguided by a user of Stack Overflow. To quote our help center: "Code Review is for open-ended questions about code that already works correctly (to the best of your knowledge)." Sorry, code that merely compiles but does not work as intended does not qualify. =( You question here will likely get closed. Asking on Stack Overflow was the right choice. Good luck. Feel free to come back when your code is working to find out how to improve it even more. =)
– I'll add comments tomorrow
Jul 31 at 17:50
















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation