Question: I have this code that basically reads if a key needs to be pressed in a game but im not sure how to make it

I have this code that basically reads if a key needs to be pressed in a game but im not sure how to make it into a exe file with a on and off button. can you make this code have and on and off button and upload the exe to a drop box and send me the link

import java.awt.KeyboardFocusManager;

import java.awt.event.KeyEvent;

/*IMPORTING VARIOUS MODULE FOR UTILITY FUNCTIONS*/

import java.awt.KeyEventDispatcher;

public class IsKeyPressed {

/*INTIALLY THE KEY PREES BOOLEAN IS TAKEN TO BE ALSE*/

private static volatile boolean wPressed = false;

public static boolean isWPressed() {/*USING SYNCHRONISATION CHECKING KEY PRESS*/

synchronized (IsKeyPressed.class) {/*RETURNNING BOOLEAN FOR KEY PRESS*/

return wPressed;

}

}

public static void main(String[] args) {

/*USING INBUILT FUNCTION FOR GETTING KEY EVENTS*/

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher((KeyEvent ke) -> {

/*CHECKING KEY PRESS*/

synchronized (IsKeyPressed.class) {

switch (ke.getID()) {/*CHECKING IF KEY IS PRESSED*/

case KeyEvent.KEY_PRESSED:

/*IF KEY PRESSED GETTING ITS CODE*/

if (ke.getKeyCode() == KeyEvent.VK_W) {

wPressed = true;

}

break;

/*CHECKING IF KEY IS RELEASED*/

case KeyEvent.KEY_RELEASED:

if (ke.getKeyCode() == KeyEvent.VK_W) {/*KEY RELEASED EVENT IS FALSE*/

wPressed = false;

}

/*COMING OUT OF SWITCH*/

break;

}

return false;

}

} /*OVERIDING INHERITED CLASS FUNCTION*/ );

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!