xxxxxxxxxx
/**
* Map Combo Key to Phoneme (v1.1.1) [Java/Pjs]
* GoToLoop (2022/Jun/07)
*
* https://Discourse.Processing.org/t/
* need-help-w-keyboard-that-take-multiple-inputs-at-once/37323/18
*
* OpenProcessing.org/sketch/1593616
*/
import java.util.Map;
final Map<Integer, String> phs = new HashMap<Integer, String>();
static final int SHIFTS = 5;
static final String ERROR = " combo key not found!";
String phoneme = "Hold some keys & release them";
int heldKeys, keyShifts, prevKey;
void setup() {
size(400, 150);
noLoop();
fill(#FFFF00); // yellow
textSize(030);
textAlign(CENTER, CENTER);
createPhonemes();
println(phs);
println("Size: " + phs.size());
}
void draw() {
background(#0000FF); // blue
text(phoneme, width >> 1, height >> 1);
}
void keyPressed() {
if (keyCode != prevKey && checkAbcRange(keyCode)) {
heldKeys |= abc(prevKey = keyCode, keyShifts++);
println(key + "\t" + heldKeys + "\t" + keyShifts);
}
}
void keyReleased() {
if (heldKeys > 0) {
phoneme = phs.get(heldKeys);
if (phoneme == null) phoneme = heldKeys + ERROR;
heldKeys = keyShifts = prevKey = 0;
redraw();
}
}
static final boolean checkAbcRange(final int keyValue) {
return keyValue >= 'A' && keyValue <= 'Z';
}
static final int abc(final int keyValue) {
return abc(keyValue, 0); // no left-shiftings
}
static final int abc(final int keyValue, final int shifts) {
return keyValue - 'A' + 1 << shifts * SHIFTS; // range 1 to 26
}