5 Triggering functions from the Keyboard
7 Drag Functions to keys in the window.
8 Typing keys will execute the Function.
10 If the Function returns another Function, the new Function will replace the action of a key.
14 GUI.cocoa; // use CocoaGUI (Mac OS X native)
15 GUI.swing; // use SwingGUI (Java based GUI)
18 var w; // window object
19 var courier; // font object
21 // an Array of Strings representing the key layout.
22 var keyboard = #["`1234567890-=", "QWERTYUIOP[]\\",
23 "ASDFGHJKL;'", "ZXCVBNM,./"];
25 // horizontal offsets for keys.
26 var offsets = #[42, 48, 57, 117];
28 var actions; // an IdentityDictionary mapping keys to action functions.
29 var makeKey; // function to create an SCDragSink for a key.
31 courier = Font("Courier", 14).boldVariant;
33 // an IdentityDictionary is used to map keys to functions so that
34 // we can look up the action for a key
35 actions = IdentityDictionary.new; // create actions dictionary
37 // define a function that will create an SCDragSink for a key.
38 makeKey = {|char, keyname, bounds|
41 keyname = keyname ? char.asString;
42 bounds = bounds ? (24 @ 24);
44 v = DragBoth(w, bounds);
50 v.canReceiveDragHandler = {
51 clz.currentDrag.isKindOf(String)
54 var function = v.object.interpret;
55 ("added key action : " ++ keyname).postln;
57 actions[char.toUpper] = function;
58 actions[char.toLower] = function;
60 actions[char] = function;
66 w = Window("keyboard", Rect(128, 320, 420, 150));
68 w.view.decorator = FlowLayout(w.view.bounds);
70 // define a function to handle key downs.
71 w.view.keyDownAction = {|view, char, modifiers, unicode, keycode|
75 result = actions[char].value(char, modifiers);
77 // if the result is a function, that function becomes the
78 // new action for the key
79 if (result.isKindOf(Function)) {
80 actions[char] = result;
84 // make the rows of the keyboard
86 row.do {|key| makeKey.(key) };
87 if (i==0) { makeKey.(127.asAscii, "del", 38 @ 24) };
88 if (i==2) { makeKey.($\r, "rtrn", 46 @ 24) };
89 w.view.decorator.nextLine;
90 w.view.decorator.shift(offsets[i]);
94 makeKey.($ , "space", 150 @ 24);
95 makeKey.(3.asAscii, "entr", 48 @ 24);
105 // Drag these things to the keyboard to test it.
110 original = thisFunction;
111 synth = { SinOsc.ar(exprand(500,1200),0,0.2) }.play;
112 { synth.free; original }
124 SinOsc.kr(ExpRand(1,15),0,0.05).max(0)),
130 { s.sendMsg(\n_free, \h, 0); } // kill head
132 { s.sendMsg(\n_free, \t, 0); } // kill tail
136 var eg, o, freq, noise;
137 eg = EnvGen.kr(Env.linen(0.1,2,0.4,0.2), doneAction: 2);
138 freq = Rand(600,1200);
139 noise = {LFNoise2.ar(freq*0.1, eg)}.dup;
140 o = SinOsc.ar(freq,0,noise);
148 in = LFSaw.ar([21000,21001], 0, LFPulse.kr(ExpRand(0.1,1),0,0.3,0.2,0.02));
149 sr = ExpRand(300,3000) + [-0.6,0.6];
150 Out.ar(0, RLPF.ar(in * LFPulse.ar(sr, 0, MouseY.kr(0.01, 0.99)), sr * (LFPulse.kr(ExpRand(0.1,12),0,0.4,0.2,0.2) + LFPulse.kr(ExpRand(0.1,12),0,0.7,0.2)), 0.1));
156 ReplaceOut.ar(0, CombN.ar(in, 0.24, 0.24, 8, 1, in.reverse).distort);
162 ReplaceOut.ar(0, in * SinOsc.ar(MouseX.kr(2,2000,1)));