Use the keyboard to control Supercollider

// NB Document, the class on which a lot of the below is based, has been significantly updated since this document was compiled (21/11/09).

.keyDownAction_

(
var doc, txt;
doc = Document.current;
doc.keyDownAction_({arg doc, key, modifiers, num;
	var mykey;
	mykey= num;
	[doc, key, modifiers, num].postln;
	mykey.postln;
	if(mykey == 113, // 113 is the letter 'q'
	{
				"hello".postln;
	},{
				"goodbye".postln;
	});
});

// and turn it off!
// command-period doesn't turn the keyDownAction off...

(
Document.current.keyDownAction_(nil);
)

// A basic example: 113 is 'q' and 119 is 'w'...

(
Document.current.keyDownAction_({arg doc, key, modifiers, num;
	var mykey;
//	[doc, key, modifiers].postln;
	mykey = num;
	if(mykey == 113,{
		{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
		"bong".postln;
	});
	if(mykey == 119,{
		{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * PinkNoise.ar([0.2,0.4,0.6].choose)}.play;
		"chhhhhh".postln;
	});

})
)

// and turn it off!

(
Document.current.keyDownAction_(nil);
)

// Now using TGrains:
// Here, 'w' just turns the sound on and off: notice the MouseX value has an effect on the sound here...

s.sendMsg(\b_allocRead, 10, "sounds/a11wlk01.wav");

(
Document.current.keyDownAction_({arg doc, key, modifiers, num;
	var mykey;
//	[doc, key, modifiers].postln;
	mykey = num;
	num.postln;
	if(mykey == 113,{
		{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
		"bong".postln;
	});
	if(mykey == 119,{

{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * TGrains.ar(2, Impulse.ar(num), 10, Dseq([10, 1, 1, 0.5, 0.5, 0.2, 0.1], inf), MouseX.kr(0,BufDur.kr(10)), 0.04, Dseq([-1, 1], inf), 0.1, 2);}.play;

	});

})
)

// and turn it off!
(
Document.current.keyDownAction_(nil);
)

// or this...
// here the keys actually set the impulse frequency.

(
Document.current.keyDownAction_({arg doc, key, modifiers, num;
	var mykey;
//	[doc, key, modifiers].postln;
	mykey = num;
	num.postln;
	if(mykey == 113,{
		{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
		"bong".postln;
	});
	if(mykey != 0,{

{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * TGrains.ar(2, Impulse.ar(num), 10, Dseq([10, 1, 1, 0.5, 0.5, 0.2, 0.1], inf), MouseX.kr(0,BufDur.kr(10)), 0.04, Dseq([-1, 1], inf), 0.1, 2);}.play;

	});

})
)

// and turn it off!
(
Document.current.keyDownAction_(nil);
)

// This use key values to control the impulses for TGrains...

(
Document.current.keyDownAction_({arg doc, key, modifiers, num;
	var mykey;
//	[doc, key, modifiers].postln;
	mykey = num;
	num.postln;
	if(mykey == 113,{
		{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
		"bong".postln;
	});
	if(mykey != 0,{

{EnvGen.kr(Env.perc([0.05,0.001,1].choose, [0.1,0.2,1].choose, 1, -4), doneAction:2) * TGrains.ar(2, Impulse.ar(num / 5), 10, Dseq([10, 1, 1, 0.5, 0.5, 0.2, 0.1], inf), num, 0.04, Dseq([-1, 1], inf), 0.1, 2);}.play;

	});

})
)

Document.current.keyDownAction_(nil);

(
{
	var b = 10, trate, dur, rate;
	trate = MouseY.kr(2,200,1);
	dur = 4 / trate;
	rate = Dseq([10, 1, 1, 0.5, 0.5, 0.2, 0.1], inf);
	TGrains.ar(2, Impulse.ar(trate), b, rate, MouseX.kr(0,BufDur.kr(b)), dur, Dseq([-1, 1], inf), 0.1, 2);
}.play;
)

// Display the character being typed:

Document.current.currentLine(2);

// or the document, the character and any modifier keys....

(
var doc, txt;
doc = Document.current;
doc.keyUpAction_({arg doc, key, modifiers, num;
	[doc, key, modifiers].postln
});
)

// and turn it off!

(
Document.current.keyUpAction_(nil);
)

// This starts the class browser - useful to get used to, although it's a little worrying at first...

Document.browse

mouseUpAction_
Note: The Mac OSX built-in editor supports only mouseUpAction. A mouseDownAction that you supply will be ignored.

(
			
			//add a mouse action to this document:
			//example: easy button:
			//when you click in front of a 17 a SinOsc will start up;
			s.waitForBoot({	
				Document.current.mouseUpAction_({arg doc;
					var char;
					char = doc.rangeText(doc.selectionStart, 2);
					if(char == "17",{
						{EnvGen.kr(Env.perc, doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
					});
					if(char == "23",{
						{EnvGen.kr(Env.perc, doneAction:2) * PinkNoise.ar(0.2)}.play;
					});
				})
			});
			)
			test here and click in front of the number:
					17
					23
			Document.current.mouseUpAction=nil; //clear mouseUpActiont