Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code that basically reads if a key needs to be pressed in a game but

ID: 3872691 • Letter: I

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 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*/ );

}

}

Explanation / Answer

Instead of creating an exe file, you can create a JAR file by using the command -

jar -cvf IsKeyPressed.jar IsKeyPressed.java

The jar file can be executed by using the command -

java -jar IsKeyPressed.jar

Note : If jar needs to be executed by using a mouse (double click). Follow the additional steps below -

Create a .bat file by typing the above command "java -jar IsKeyPressed.jar" in a notepad and saving it as "IsKeyPressed.bat". This file should be in the same directory as the IsKeyPressed.jar file.

The program for the on/off button is -

/*IMPORTING VARIOUS MODULE FOR UTILITY FUNCTIONS*/

import java.awt.event.KeyEvent;

import java.awt.KeyboardFocusManager;

import java.awt.KeyEventDispatcher;

import javax.swing.AbstractButton;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

// Extend JPanel for the placing the button on a container  

public class IsKeyPressed extends JPanel {

  

protected static JButton jbn;

/*INTIALLY THE KEY PRESS BOOLEAN IS TAKEN TO BE FALSE*/

private static volatile boolean wPressed = false;

  

public IsKeyPressed() {

// Display current state of the button

jbn = new JButton("Released");

jbn.setVerticalTextPosition(AbstractButton.CENTER);

jbn.setHorizontalTextPosition(AbstractButton.LEADING);

// Alt-W clicks the button

jbn.setMnemonic(KeyEvent.VK_W);

jbn.setEnabled(true);

jbn.setToolTipText("Press Alt W for button to be PRESSED");

// Add Component to the frame, using the default FlowLayout.

add(jbn);

wPressed = false;

}

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

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

return wPressed;

}

}

  

// Create a GUI for display.

private static void createGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

// Create and set up the frame.

JFrame frame = new JFrame("Switch Press Detection");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create and set up the content pane.

IsKeyPressed pane = new IsKeyPressed();

pane.setOpaque(true); // content panes must be opaque

frame.getRootPane().setDefaultButton(jbn);

frame.setContentPane(pane);

// Display the window.

frame.pack();

frame.setVisible(true);

}

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;

jbn.setToolTipText("Press Alt W for button to be RELEASED");

jbn.setText("Pressed");

}

break;

/*CHECKING IF KEY IS RELEASED*/

case KeyEvent.KEY_RELEASED:

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

wPressed = false;

jbn.setToolTipText("Press Alt W for button to be PRESSED");

jbn.setText("Released");

}

/*COMING OUT OF SWITCH*/

break;

}

return false;

}

} /*OVERIDING INHERITED CLASS FUNCTION*/ );

  

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createGUI();

}

});

}

  

}