KeyPlayer a simple keyboard interface


KeyPlayer allows using the computer keyboard keys for 

executing functions when given keys are pressed or released 

(keyDown and keyUp).


see also KeyPlayerGui, KeyPlayerRec, KeyChain


class methods:



k = KeyPlayer(\k); // create an instance

KeyPlayer(\k) == k; // same name accesses same instance


KeyPlayer.all; // all instances live here


KeyPlayer.verbose_(true); // debugging, posts:

// [KeyPlayer, char, modifiers, unicode, keycode]


KeyPlayer.verbose_(false);


KeyPlayer.gui; // shows up to 10 keyplayers


instance methods: 


// put in some functions

k.put($a, { "A down!".postln }); // or equivalent:

k.putDown($a, { "A down!".postln });

k.actions; // they are stored here by unicode numbers


// upActions are separate

k.putUp($a, { "A up!".postln });

k.upActions;


// both dictionaries are kept here: 

k.bothActions.postcs; "";


// you can put things in both:

k.putBoth($a, { "A upOrDown!".postln });


// execute keyActions by hand 

// - also try them when the window is in front, 

// and one of the buttons focused.

k.keyDown($a);

k.keyUp($a);

k.keyDown($a.asUnicode);

k.keyUp($a.asUnicode);


KeyPlayer.verbose_(false);


// avoid retriggering keyDowns: try holding the s-key down 

k.putDown($s, { "yo again".postln }, noRep: true);

k.putUp($s, { "yo again".reverse.postln });


k.putDown($s, { "yo again".postln }); // retriggers by default


g = k.gui;

// remove actions:

k.removeAt($a);

k.removeAt($a, \up);

k.upActions;

k.removeAt($a, \down);

k.actions;


// make a KeyPlayerRec for it ...  see KeyPlayerRec.help for details

k.makeRec;

k.rec; // and it is here


examples:


// the random keyboard: 

(

s.latency = nil; // make sure we respond quickly

thisThread.randSeed = 2008;

"qwertzuiopasdfghjklyxcvbnm".do { |char| 

var note = (midinote: rrand(36, 96), dur: rrand(0.1, 1.0));

KeyPlayer(\rand).put(char, { |char| char.postln; note.postln.play });

};

KeyPlayer.gui.front;

)


KeyPlayer(\rand).bothActions


// random keyboard, no retrig, hold until release with keyUp

(

q = q ? (); // create a dict to keep things: 

q.notes = ();

q.synths = ();

thisThread.randSeed = 2008; // it is not that new


"qwertzuiopasdfghjklyxcvbnm".do { |char| 

q.notes.put(char, rrand(36, 96));

KeyPlayer(\hold).putDown(char, { |char| 

char.post; 

q.synths[char].release;

q.synths.put(char, Synth(\default, [\freq, q.notes[char].postln.midicps]));

}, noRep: true);

KeyPlayer(\hold).putUp(char.postln, { |char| q.synths.removeAt(char).release });

};

KeyPlayer.gui.front;

)


KeyPlayer(\px);

// stop and resume a proxyspace:

p = ProxySpace.push;

KeyPlayer(\px).put($p, { p.do(_.pause) });

KeyPlayer(\px).put($r, { p.do(_.resume) });


// David Cottle example IIRC 

~test = { SinOsc.ar(LFNoise0.kr([10, 15], 200, [300, 500])) * 0.2 };

~test.play;


// re-send or rebuild the sound

(

~test = { 

SinOsc.ar(

LFNoise0.kr([10, 15] * exprand(0.5, 4.0), 200, [300, 500]) 

* exprand(1.0, 4.0)

) * XLine.kr(0.5, 0.005, exprand(0.1, 10), doneAction: 2);

};

KeyPlayer(\px).put($t, { ~test.send });

KeyPlayer(\px).put($u, { ~test.rebuild });

)