Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: key input handler

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default key input handler

    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferStrategy;
     
    import javax.swing.JFrame;
     
    public class Main extends Canvas implements Runnable {
     
    	private static final long serialVersionUID = 1L;
    	public boolean running = false;
    	public int width;
    	public int height;
    	public Thread thread;
        inputHandler input;
     
    	public Main(int width, int height) {
     
    		this.width = width;
    		this.height = height;
     
    		Dimension size = new Dimension(width, height);
    		setPreferredSize(size);
    		setMinimumSize(size);
    		setMaximumSize(size);
     
    		input = new inputHandler();
    		addKeyListener(input);
     
    	}
     
    	private void init() {
     
     
    	}
     
    	public synchronized void start() {
    		thread = new Thread(this, "main thread");
    		running = true;
    		thread.start();
    	}
     
    	public void run() {
    		while (running) {
    			render();
    			update();
    		}
    	}
     
    	public void render() {
    		BufferStrategy bs = this.getBufferStrategy();
    		if (bs == null) {
    			createBufferStrategy(2);
    			return;
    		}
    		Graphics g = bs.getDrawGraphics();
    		g.setColor(Color.black);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.dispose();
    		bs.show();
    	}
     
    	public void update() {
    		System.out.println(input.keys[KeyEvent.VK_UP]);
    	}
     
    	public static void main(String[] args) {
    		Main main = new Main(640, 480);
    		JFrame window = new JFrame();
    		window.add(main);
    		window.pack();
    		window.setVisible(true);
    		window.setResizable(false);
    		window.setTitle("buffer Strategy test");
    		window.setLocationRelativeTo(null);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		main.start();
    	}
    }

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class inputHandler implements KeyListener {
     
    	public boolean keys[] = new boolean[65536];
     
    	public void keyPressed(KeyEvent e) {
    		int keycode = e.getKeyCode();
    		keys[keycode] = true;
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int keycode = e.getKeyCode();
    		keys[keycode] = false;
    	}
     
    	public void keyTyped(KeyEvent e) {
     
    	}
    }

    please note this is not my code i am following a youtube tutorial. so basicaly im trying to use booleans to save the key strokes as true using the key event method getKeyCode() rather than using many if statements to what key has been pressed. when i put a system println in my update method telling it to print the boolean value of the key up it is constantly false even if i press it. thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: key input handler

    please note this is not my code i am following a youtube tutorial
    My first thought is to ditch the code as well as the tutorial. There is a lot that is incorrect - mixing Swing and AWT, not following the Single threaded model of Swing/AWT, naming conventions are incorrect (and very close to clashing with the naming of the API methods)...all this being said, I do not see where the listener is ever created/instantiated

  3. The Following User Says Thank You to copeg For This Useful Post:

    DanielJamesCollier (May 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: key input handler

    how would you recomend i learn, by reading the oracle tutorials ? thanks

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: key input handler

    Quote Originally Posted by DanielJamesCollier View Post
    how would you recomend i learn, by reading the oracle tutorials ? thanks
    Yes. They can seem overwhelming, so here's my advice for a beginner - read through them at a high level. Get an idea of the different GUI components available, the different types of listeners you can use to interact with those components (ActionListener, MouseListener, etc...), and options available for customization (Layouts, Borders, etc...). You do not need to know how to code them at that point - just what you could do. Then when it comes down to writing code for a specific project, break the problem down and get into the specifics of the tutorials for each problem.
    Trail: Creating a GUI With JFC/Swing: Table of Contents (The Java™ Tutorials)

Similar Threads

  1. JButton event handler
    By Jsri in forum AWT / Java Swing
    Replies: 1
    Last Post: October 25th, 2011, 07:02 AM
  2. Input
    By DeFactos in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 18th, 2011, 04:05 AM
  3. Getting input
    By BuhRock in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2010, 10:30 AM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM

Tags for this Thread