Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Aritz Lopez
   *
   *    Licensed under the Apache License, Version 2.0 (the "License");
   *    you may not use this file except in compliance with the License.
   *    You may obtain a copy of the License at
   *
   *        http://www.apache.org/licenses/LICENSE-2.0
   *
  *    Unless required by applicable law or agreed to in writing, software
  *    distributed under the License is distributed on an "AS IS" BASIS,
  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
 
 package io.github.aritzhack.aritzh.awt.gameEngine.input;
 
 import java.awt.*;
 import java.util.*;
 import java.util.List;
Input handler for the io.github.aritzhack.aritzh.awt.gameEngine.CanvasEngine class.

Author(s):
Aritz Lopez
 
 public class InputHandler implements KeyListenerFocusListenerMouseInputListener {
 
 	private final boolean[] keys = new boolean[65536];
 	private final List<Integerchecked = new ArrayList<Integer>();
 	private final Queue<MouseInputEventmouseEvents = new LinkedList<>();
 	private boolean focus;
 	private Point lastMousePos = new Point();
 
 	// region ... "Getter" methods...
 
True if the canvas is focused

Returns:
True if the canvas is focused
 
 	public boolean hasFocus() {
 		return ;
 	}

True if the key was pressed, but hadn't been previously checked. Subsequent calls to this method will, therefore, return always false, as long as the key hasn't been released and pressed again.

Parameters:
keyCode The code that corresponds to the key, according to java.awt.event.KeyEvent
Returns:
True if the key was pressed, but hadn't been previously checked.
 
 	public boolean wasKeyTyped(int keyCode) {
 		if (this.isKeyDown(keyCode) && !this..contains(keyCode)) {
 			this..add(keyCode);
 			return true;
 		}
 		return false;
 	}

Returns true if the key is being pressed.

Parameters:
keyCode The code that corresponds to the key, according to java.awt.event.KeyEvent
Returns:
true if the key is being pressed.
 
 	public boolean isKeyDown(int keyCode) {
 		return this.[keyCode];
 	}

Returns the position of the mouse.

Returns:
the position of the mouse.
 
 	public Point getMousePos() {
 		return this.;
 	}

Returns a stack of MouseInputEvents that represent the events the mouse had since it was last pressed.

Returns:
a stack of mouse events
 
 		return this.;
 	}
 
 	//endregion
 
 	// region ...Focus listener methods...
 
 	public void focusGained(FocusEvent e) {
		this. = true;
	}
	public void focusLost(FocusEvent e) {
		this. = false;
		Arrays.fill(this.false);
	}
	// endregion
	// region ...Keyboard listener methods...
	public void keyTyped(KeyEvent e) {
	}
	public void keyPressed(KeyEvent e) {
		this.[e.getKeyCode()] = true;
	}
	public void keyReleased(KeyEvent e) {
		this.[e.getKeyCode()] = false;
	}
	// endregion
	// region ...Mouse listener methods...
	public void mouseClicked(MouseEvent e) {
	}
	public void mousePressed(MouseEvent e) {
	}
	public void clearMouseEvents() {
	}
	public void mouseReleased(MouseEvent e) {
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public void mouseDragged(MouseEvent e) {
	}
	public void mouseMoved(MouseEvent e) {
	}
	// endregion
	public static enum MouseButton {
		LEFT, MIDDLE, RIGHT, NONE;
		public static MouseButton getFromEvent(int eventButton) {
			switch (eventButton) {
					return ;
					return ;
					return ;
				default:
					return ;
			}
		}
	}
	public static enum MouseAction {
		PRESSED, RELEASED, DRAGGED, CLICKED
	}
	public static class MouseInputEvent {
		private final MouseButton button;
		private final MouseAction action;
		private final Point position;
		public MouseInputEvent(MouseButton buttonMouseAction actionPoint position) {
			this. = button;
			this. = action;
			this. = position;
		}
		public static MouseInputEvent fromMouseEvent(MouseAction actionMouseEvent e) {
			return new MouseInputEvent(MouseButton.getFromEvent(e.getButton()), actionnew Point(e.getX(), e.getY()));
		}
		public MouseButton getButton() {
			return ;
		}
		public MouseAction getAction() {
			return ;
		}
		public Point getPosition() {
			return ;
		}
	}
New to GrepCode? Check out our FAQ X