I am working on a calculator project on computer with jdk8 library. My goal is to get a working calculator from its interaction with the user through the computer keyboard. For this I was advised to use the keyPressed method from the standard library of the java in order to be able to use the keyboard keys for the operation of my software. I have successfully implemented the keypressed method. Then, I wrote a code so that when I press a key on the computer keyboard, for example b, that it activates a key on the keyboard of said calculator which will in turn display on the screen the calculator, the number it carries for example 2.
But problem, whenever I press the b key, it always returns 2b on my calculator screen.
Here is my code so that the b key on the computer can return the number 2 on my calculator screen with "screen" the name of my JTextField and "buttonTwo" the name of my Jbutton for key 2:
public void keyPressed ( KeyEvent e ) {
super.keyPressed(e) ;
int key = e.getKeyCode() ;
if ( key == KeyEvent.VK_B ) {
String buttonTwoText = Screen.getText() + buttonTwo.getText() ;
Screen.setText(buttonTwoText);
}
}