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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started