Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 7 developers from Canada and 17 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!

Dev Journal: Detecting Tapping and Pressing inputs using Java.

Posted by tom_mai78101, 22 March 2014 · 597 views

inputtapping tap pressing press detection detect keylistener java
Dev Journal: Detecting Tapping and Pressing inputs using Java. I'll be showing you a simple way of detecting different input keystrokes using Java. There are two different keystrokes for a computer keyboard, tapping and pressing. Tapping a key means the user is pressing the key and letting go immediately after. Pressing a key means the user is pressing and holding down the key until the user wishes to let go.

In Java, the fastest way of detecting key inputs is to use a class implementing the KeyListener interface, and then adding the listener to the Swing component. Fastest, but not exactly feasible for some others.

The key to detecting inputs to determine if it's tapping or pressing is by using threads. I use a thread pool for easier thread handling. Below shows the codes, while I try to explain how it works.
public class NewInputHandler implements KeyListener {
	public Map<Key, Integer> mappings = new HashMap<Key, Integer>();
	private ExecutorService threadPool = Executors.newCachedThreadPool();
	private Keys keys;
First, we need to create a thread pool in order to manage threads easily. You can tell that I used a hashmap, this is for holding the key codes per key. If the Key is, for example, the "A" key, the Integer portion will contain the key code of A, which you can obtain via
method. I also created a new class object, Keys, which holds key states, "tapped" or "pressed". More on that later on.
	public NewInputHandler(Keys keys) {
		this.keys = keys;
		mappings.put(keys.up, KeyEvent.VK_UP);
		mappings.put(keys.down, KeyEvent.VK_DOWN);
		mappings.put(keys.left, KeyEvent.VK_LEFT);
		mappings.put(keys.right, KeyEvent.VK_RIGHT);
		mappings.put(keys.W, KeyEvent.VK_W);
		mappings.put(keys.S, KeyEvent.VK_S);
		mappings.put(keys.A, KeyEvent.VK_A);
		mappings.put(keys.D, KeyEvent.VK_D);
In the code above, I pass in the Keys object, and then put all available key controls into the hashmap. This part is a bit self-explanatory, but basically the hashmap now contains the key codes for the corresponding keys.
	public void keyPressed(KeyEvent event) {
		for (Key v : mappings.keySet()) {
			if (mappings.get(v) == event.getKeyCode()) {
				if (!v.keyStateDown) {
					final Key key = v;
					key.isTappedDown = true;
					key.isPressedDown = false;
					key.keyStateDown = true;
					this.threadPool.execute(new Runnable() {
						public void run() {
							try {
							catch (InterruptedException e) {
							if (key.keyStateDown) {
								key.isPressedDown = true;
								key.isTappedDown = false;
Now, this is one half of the core of detecting tapping and pressing keys. By navigating through the hashmap, then finding the key that has been pressed, we can then be sure to edit its key states. After that, we create a new thread worker that helps determine when the user is actually tapping the keyboard, or actually pressing it. I let it sleep for 100 milliseconds, as this given value is enough to detect and tell the difference both tapping and pressing. Finally, we edit the properties of the key that was selected and active.
	public void keyReleased(KeyEvent event) {
		for (Key k : mappings.keySet()) {
			if (mappings.get(k) == event.getKeyCode()) {
				k.isPressedDown = false;
				k.isTappedDown = false;
				k.keyStateDown = false;
	public void keyTyped(KeyEvent arg0) {
		//Ignore. Used for sending Unicode character mapped as a system input.
This is the other half of the core. When a key has been released, we have to mark all occurrences of the key as false, in order to prevent input overlapping issues. Just be wary that there's a missing bracket, so people there may be anything but quiet.

So now, when you start the game, tapping (or quickly press) the touch screen will say that you have tapped a key, or pressed a key. That is it for today.