KeyChain a named chain of actions.



A KeyChain is a named chain of actions. 

It can run these in sequence, or based on transition functions. 

It can be used nicely with KeyPlayer. 

// a single function

a = KeyChain($a, [(key: \x, act: { "act".scramble.postln })]);

a.next;

a.next;


// KeyChain($a) is a named proxy, so you either create or access it by name: 

KeyChain($a);


// You can change its list by accessing it with a second argument:

b = KeyChain($a, [(key: \x, act: { "bong".scramble.postln })]);


a === b; // true!

a = KeyChain($a);


// a, b, and equivalent now: 

b.next; ""

a.next; ""

KeyChain($a).next


a.dump



( // a simple sequence

KeyChain($a, [

(key: \x, act: { "xanadu".scramble.postln }), 

(key: \y, act: { "yakk".scramble.postln }), 

(key: \z, act: { "zonk".scramble.postln }), 

]);

)

b.next;

a.next;


KeyChain($a).next;



// a KeyPlayer for it: 

KeyPlayer(\chain);

KeyPlayer.gui;

KeyPlayer(\chain).putBoth($a, { KeyChain($a).next });


KeyPlayer(\chain).bothActions.postcs;


// tell it where to go next with 'to' key

// does this retrigger, even though it should not 

// (it was off earlier)? 

(

KeyChain($a, [

// name, what, what next

(key: \boing, act: { \boing.postln }, to: { "next: ".post; [\boing, \tak].choose.postln }), 

(key: \tak, act: { \tak.postln }, to: \boom), 

(key: \boom, act: { \boom.postln }), // default is next in chain, wraps 

], 0)

)


// get more info

KeyChain.verbose = true;

a.next; ""

a.next; ""

a.next; ""


KeyChain.verbose = false;



// more complex sequence:

(

KeyChain($a, [

// name, what, what next

(key: \boing, act: { \boing.postln }, to: { [\boing, \tak].choose }), 

(key: \tak, act: { \tak.postln }, to: \boom), 

(key: \boom, act: { \boom.postln }), // default is next in chain, wraps 

(key: \tsk, act: { \tsk.postln }), 

(key: \chak, act: { \chak.postln }, to: { a.chooseNext })

], 0)

)


a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""

a.next; ""


// do multiple actions at once

a.act(0)

a.act(\boom)

a.act(\chak)

a.act([\boom, \chak])

a.act([\boom, \chak, \tak])