Is there any way in the IDE of defining a shortcut key to execute a given SC-Lang statement? For instance, defining function key F1 to do interpret(“MyApp.run”). It would be useful for things that one does often.
http://doc.sccode.org/Classes/Document.html#*globalKeyDownAction
In practice, it’s necessary to wait 1 or 2 seconds after the interpreter starts up before installing a globalKeyDownAction, but it does work.
Avoid .interpret
though – to create a package of sclang behavior to run on demand, write it into a { function }
.
hjh
EDIT: PS, here’s the ctrl-esc shortcut I have in my startup.scd (minus one thing that’s too long to repost here):
if(Platform.ideName == "scqt") {
StartUp.add({
var e = Environment.make {
~cmds = (
// each action is a function, not .interpret
$q: "Query all nodes" -> { Server.default.queryAllNodes },
$s: "Browse SynthDescLib" -> { SynthDescLib.global.browse },
$b: "Class browser" -> { Object.browse },
$d: "Dismiss" -> { nil }, // .action func closes the window, no need here
$p: "Free 57120" -> { "fuser -k 57120/udp".unixCmd; "Please reboot interpreter".postln }
);
~makeWin = {
~win = Window("util", Rect(800, 679, ~cmds.size * 30 + 10, 70));
~win.layout = HLayout(*
~cmds.keys.asArray.sort.collect { |key|
Button().fixedWidth_(20)
.states_([[key]])
.action_(inEnvir { ~win.close; ~cmds[key].value.value; })
.toolTip_(~cmds[key].key)
.keyDownAction_(false);
}
);
~win.view.keyDownAction = inEnvir { |view, char|
if(~cmds[char].notNil) {
~win.close;
~cmds[char].value.value;
};
};
~win.front;
};
};
{
if('Document'.asClass.respondsTo(\globalKeyDownAction_)) {
'Document'.asClass.globalKeyDownAction = 'Document'.asClass.globalKeyDownAction.addFunc({ |doc, char, modifiers, ascii|
// recognizing the keystroke is platform-dependent!
if(ascii == 27 and: { modifiers.bitAnd(262144) != 0 }) {
~makeWin.value;
};
}.inEnvir(e));
};
}.defer(3.0);
Library.put(\utilityEnvir, e);
});
};
Thanks for your quick and generous answer!
It almost works - except all the function keys are passing the same argument values. I was hoping to map F9 and F10 to functions.
passes me doc=a Document, char=-3, modifiers=0, ascii=65533 for all function keys and then some. When using the normal keyDownAction, the argument list is a little different, and that works with function keys inside my views. Nothing is simple.
Got it now, one last arg key is 120 for F9 and 121 for F10. All done.