We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › multiple key presses not working 100%
Page Index Toggle Pages: 1
multiple key presses not working 100% (Read 436 times)
multiple key presses not working 100%
Dec 16th, 2008, 8:44am
 
Hi,

Sorry if this is the wrong place; I couldn't find a forum on hacks.  This is regarding the multi-key press hack: http://www.processing.org/hacks/hacks:multiplekeys

I used the first example and it works MOST of the time.  The problem comes when holding down many keys.  It seems like if you hold down about 4 or 5 keys, it stops reading in the new presses until other keys are released.  Here is an example I made to illustrate that:

Code:

PFont font;

void setup() {
size(200,200);
font = loadFont("ArialMT-12.vlw");
textFont(font,12);
stroke(255);
}

void draw(){
background(0);
checkKeys();
}

boolean[] keys = new boolean[526];
void checkKeys()
{
for(int i = 0; i < keys.length; i++)
if (keys[i] == true){
text(KeyEvent.getKeyText(i),(i*8)%width,int(i*8/width)*8 + 20);
}
}

void keyPressed()
{
keys[keyCode] = true;
println(KeyEvent.getKeyText(keyCode));
}

void keyReleased()
{
keys[keyCode] = false;
}


Try holding down many keys, 1 at a time and watch what happens.


I tried to fix this by using the second example which is supposedly "snappier".  When I first ran the example I got "Cannot parse error text: C:\.....\HelloWorld.java is missing"  I then converted this code into Processing code like this:

Code:

import bak.pcj.set.IntSet;
import bak.pcj.set.IntOpenHashSet;

IntSet keysheld;

void setup() {
IntSet = new IntOpenHashSet();
/**
* @param keycode key integer code, the value are constants defined in KeyEvent Class
* http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html
* e.g. KeyEvent.VK_A for letter A
* KeyEvent.VK_0 for number 0
* KeyEvent.VK_SHIFT for shift button
* @return true if the key is currently held down, false otherwise
*/


}

boolean checkKeysHeld(int keycode){
keysheld.contains(keycode);
}

void keyPressed(){
// add key to the list of keys held down
// with processing, the KeyEvent object is always available as "keyEvent",
// the getKeyChar() is already in the variable 'key', and getKeyCode() is in the variable 'keyCode'.
keysheld.add(keyEvent.getKeyCode());
println("key pressed: "+KeyEvent.getKeyText(keyEvent.getKeyCode()));
println("keys in current held list: "+keysheld.toString());
}
void keyReleased(){
// remove key from the list of keys held down
keysheld.remove(keyEvent.getKeyCode());
}


I now get the error "The package "bak" does not exist. You might be missing a library.

So I guess I have a few questions:
1. Why doesn't the first example work all the time?
2. The 2nd example seems to be Java code, I think. Should this compile in Processing? If (yes) {why doesn't it?} else {why did someone post it in the Processing hacks section?}
3.Where can I obtain the "bak" library to compile this code?

Sorry this was loooong, but more information is better than less right?
Re: multiple key presses not working 100%
Reply #1 - Dec 16th, 2008, 9:35am
 
re 1) this is just the nature of keyboards. these use grids of connections (keyboard matrix), so sometimes one key will hide others.

re 2) you can add .java files to your code by adding them as a .java tab (place the file into your sketches folder then reopen the sketch. or, in case you're starting with a blank .java file: add a tab with ending ".java" and paste your code in there.

re 3) try your favorite search engine for "bak.pcj.set"?

F
Re: multiple key presses not working 100%
Reply #2 - Dec 16th, 2008, 9:52am
 
fjen wrote on Dec 16th, 2008, 9:35am:
re 1) this is just the nature of keyboards. these use grids of connections (keyboard matrix), so sometimes one key will hide others.


thanks for your help. follow up question on #1.

are these grids the same on all keyboards? i'm making a 2 player, 1 keyboard game which involves holding down a lot of keys.  i think my only hope is to find sets of keys that do not interfere which each other. but this will only work if the connections are standard. thanks!
Re: multiple key presses not working 100%
Reply #3 - Dec 17th, 2008, 8:51am
 
well to be honest i don't know ... but i don't think they are standardized.

you could add a step to startup of your project that makes the user press the needed key and the you could check if these work as you want ... sort a like woth game consoles where you have to press controlelr buttons to let the game know you're ready.

F
Page Index Toggle Pages: 1