3 Replies - 134 Views - Last Post: 31 May 2019 - 10:24 AM Rate Topic: -----

#1 lordvile   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-May 19

Prevent keyPressed from repeating when held down

Posted 31 May 2019 - 10:06 AM

I'm trying to get the keyPressed Event to perform only once when held down. I've tried creating a boolean and using boolean logic, but the action is still repeating when held. Is there any other way to do this?
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Protag extends Entity {

	private int dx;
	private int dy;
	//    private int x = -25;
	//    private int y = 250;
	//    private int width;
	//    private int height;
	private Image image;
	private Clip clip; 
	private boolean isPressed = false;

	public Protag(int x, int y,int health,int damage) {
		super(x,y,health,damage);
		loadImage();
	}

	private void loadImage() {

		ImageIcon proImage = new ImageIcon("yu.png");
		image = proImage.getImage(); 

		//        width = image.getWidth(null);
		width = 304;

		//        height = image.getHeight(null);
		height = 410;


	}

	public void move() {

		x += dx;
		y += dy;
	}



	public int getWidth() {

		return width;
	}

	public int getHeight() {

		return height;
	}    

	public Image getImage() {

		return image;
	}




	public void setImage(Image image) {
		this.image = image;
	}
	public void keyTyped(KeyEvent e) {
		if()
	}
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();

		if (key == KeyEvent.VK_A) {
			//System.out.print("lel");
			ImageIcon proImage = new ImageIcon("yuwalkback.png");
			image = proImage.getImage(); 
			width = 168;
			height = 392;
			dx = -12;
		}

		if (key == KeyEvent.VK_D) {

			ImageIcon proImage = new ImageIcon("yuwalk.png");
			image = proImage.getImage();
			width = 308;
			height = 391; 
			dx = 8;
		}
		if(key == KeyEvent.VK_S) {

			ImageIcon proImage = new ImageIcon("yudodge.png");
			image = proImage.getImage();
			width = 339;
			height = 256;

			playSound("bc320a.wav");


			// y=400;
		}
		if(key == KeyEvent.VK_F) {
			
			if(isPressed == true)
			
			return;
			isPressed = true;
			ImageIcon proImage = new ImageIcon("yuu.png");
			image = proImage.getImage();
			width = 505;
			height = 258;
			playSound("bc117b.wav");
			
	






			
			//y=350;
		}
		if(key == KeyEvent.VK_G) {

			ImageIcon proImage = new ImageIcon("yuatk2.png");
			image = proImage.getImage();
			width = 380;
			height = 364;



			playSound("bc310a.wav");
			//y=350;
		}
		if(key == KeyEvent.VK_H) {
			ImageIcon proImage = new ImageIcon("yublock.png");
			image = proImage.getImage();
			width = 219;
			height = 384;
		}

		if(key == KeyEvent.VK_C) {
			JOptionPane.showMessageDialog
			(null, "<html>Narukami Yu: A and D to move, S to dodge, G to light attack, , H to heavy attack, J to block.<br>  "
					+ "Hanamura Yosuke: Left and Right to move,  Down to dodge, COMMA to light attack, PERIOD to heavy attack,  SLASH to block.<br>"
					+ "", 
					"Controls: " , JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();

		if (key == KeyEvent.VK_A) {
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;
			dx = 0;
		}

		if (key == KeyEvent.VK_D) {
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;
			dx = 0;
		}
		if(key == KeyEvent.VK_S) {
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;

		}
		if(key == KeyEvent.VK_F) {
			
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;
			isPressed = false;


		}
		if(key == KeyEvent.VK_G) {
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;
		}
		if(key == KeyEvent.VK_H) {
			ImageIcon proImage = new ImageIcon("yu.png");
			image = proImage.getImage();
			width = 304;
			height = 410;
		}


	}
	
	public void playSound(String fileName) {
		// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
		try {
			File soundFile = new File(fileName); 
			AudioInputStream audio = AudioSystem.getAudioInputStream(soundFile);
			clip = AudioSystem.getClip(); 
			clip.open(audio); 
			FloatControl gainControl = 
					(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
			gainControl.setValue(-10.0f);
			clip.start();




		} 
		catch (Exception e) { 
			e.printStackTrace(); 
		} 
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Prevent keyPressed from repeating when held down

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Prevent keyPressed from repeating when held down

Posted 31 May 2019 - 10:13 AM

Quote

the action is still repeating

What key are you talking about? What action is repeating?
How are you trying to debug the code? Add some print statements that show the values of variables as the code executes so you can see what the computer sees when the code executes.
Was This Post Helpful? 0
  • +
  • -

#3 lordvile   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-May 19

Re: Prevent keyPressed from repeating when held down

Posted 31 May 2019 - 10:21 AM

View PostNormR, on 31 May 2019 - 10:13 AM, said:

Quote

the action is still repeating

What key are you talking about? What action is repeating?
How are you trying to debug the code? Add some print statements that show the values of variables as the code executes so you can see what the computer sees when the code executes.

Trying to prevent key F from repeating, and the image stays the same when I hold the key. I want the default image to change when the key is pressed to the image in F, and then have it revert back to the default image even when the F key is held down

View Postlordvile, on 31 May 2019 - 10:20 AM, said:

View PostNormR, on 31 May 2019 - 10:13 AM, said:

Quote

the action is still repeating

What key are you talking about? What action is repeating?
How are you trying to debug the code? Add some print statements that show the values of variables as the code executes so you can see what the computer sees when the code executes.

Trying to prevent key F from repeating, and the image stays the same when I hold the key. I want the default image to change when the key is pressed to the image in F, and then have it revert back to the default image even when the F key is held down

Also the print statement only prints out once each time the keyPressed is called, even when held down. However, the image stays as the image in F when the key is held down.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Prevent keyPressed from repeating when held down

Posted 31 May 2019 - 10:24 AM

Can you copy and paste here the debug print outs that shows when the key is first pressed, when the repeated press is detected and when the key is released so we can see what is happening?

I don't see any debug print statements in the code. Can you also post the code with the debug print statements?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1