Pressing multiple keys at once on different rows

public class Main extends PApplet{

	public static void main(String[] args) {
		PApplet.main("Main");
		
	}
	
	
	Map<Character, Boolean> buttonsPressed = new HashMap<Character, Boolean>();
	Map<Character, Boolean> buttonsReleased = new HashMap<Character, Boolean>();
	
	public void settings(){
		size(1000, 700);
		
    }

    public void setup(){
    	setupMaps();
    	this.rectMode(CORNER);
    }

    public void draw(){
    	background(0);
    	if(buttonsReleased.get('w')) {
    		buttonsReleased.put('w', false);
    		buttonsPressed.put('w', false);
    	}
    	if(buttonsReleased.get('a')) {
    		buttonsReleased.put('a', false);
    		buttonsPressed.put('a', false);
    	}
    	if(buttonsReleased.get('s')) {
    		buttonsReleased.put('s', false);
    		buttonsPressed.put('s', false);
    	}
    	if(buttonsReleased.get('d')) {
    		buttonsReleased.put('d', false);
    		buttonsPressed.put('d', false);
    	}
    	if(buttonsReleased.get('e')) {
    		buttonsReleased.put('e', false);
    		buttonsPressed.put('e', false);
    	}
    	
    	if(buttonsPressed.get('s')) {
    		fill(255, 0, 0);
    		noStroke();
    		this.rect(100, 50, 50, 50);
    	}
    	if(buttonsPressed.get('w')) {
    		fill(0, 255, 0);
    		noStroke();
    		this.rect(100, 0, 50, 50);
    	}
    	if(buttonsPressed.get('a')) {
    		fill(0, 0, 255);
    		noStroke();
    		this.rect(50, 50, 50, 50);
    	}
    	if(buttonsPressed.get('d')) {
    		fill(255);
    		noStroke();
    		this.rect(150, 50, 50, 50);
    	}
    	
    	if(buttonsPressed.get('e')) {
    		fill(255, 255, 0);
    		noStroke();
    		this.rect(150, 0, 50, 50);
    	}
    	
    }
    
    public void keyPressed() {
    	PApplet.print(key);
    	if(isValidKey(key)) {
    		buttonsPressed.put(key, true);
    	}
    }
    
    public void keyReleased() {
    	if(isValidKey(key)) {
    		buttonsReleased.put(key, true);
    	}
    }
    
    private boolean isValidKey(Character key) {
    	for(Character c :buttonsPressed.keySet()) {
    		if(key == c) {
    			return true;
    		}
    	}
    	
    	return false;
    }
    private void setupMaps() {
    	buttonsPressed.put('w', false);
    	buttonsPressed.put('a', false);
    	buttonsPressed.put('s', false);
    	buttonsPressed.put('d', false);
    	buttonsPressed.put('e', false);
    	
    	buttonsReleased.put('w', false);
    	buttonsReleased.put('a', false);
    	buttonsReleased.put('s', false);
    	buttonsReleased.put('d', false);
    	buttonsReleased.put('e', false);
    }
}

I was trying to get the wasd keys to do something when pressed but things weren’t working as expected so I made this to test it. I found that once you pressed more than one key at a time on a row or column the keys directly above and below no longer register. For example, after pressing s and d down at the same time, w and e don’t work anymore (but if you release one or both of s and d they work fine), and if you press down s and w at the same time e and d don’t work.

At some point when I was trying to make the code smaller with the same error it started working fine all of a sudden so I undid (ctrl + z) it to see if I could learn what caused it, so I wouldn’t run into it again, but I don’t know what I did or how to get back to it.

Note: I am working on Windows and am using Eclipse with Processing 3.3.6

1 Like

http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G

2 Likes

Interesting issue. From the above article:

Due to the keyboard matrix most consumer keyboards use, jamming and ghosting often occur when three out of four keys in a square block on the matrix are pressed, such as QASW or JKUI.

Maybe you are trying to implement a new functionality in your code but you are limited by the current design of your hardware. For instance, if I am typing, I wouldn’t press “t” “h” “e” at the same time but one at the time. It makes sense just to handle one key at the time. Base on the wiki article, shift, alt, ctrl do not suffer jamming.

Probly you need to reconsider your strategy?

Kf

1 Like

Turns out it is the old keyboard I’m using. I’m using an old Apple keyboard for my laptop so I can raise my computer higher. The keys can’t be pressed at once on it but on my laptop’s built-in keyboard it works fine.

Thanks for your help

1 Like