简体   繁体   中英

Using MouseDown with SuperCollider: non-GUI implementation

I want to use MouseDown in SuperCollider and am having a helluva time. Is it the case that only mouseDownAction actually works with anything? I want to be able to click anywhere on the screen, and have the mouse coordinates print, eg, to the post window:

Server.default=s=Server.local;
s.boot;
s.mouseDownAction = { x = {|t_poll=0| var x_val, y_val;
    x_val = {MouseX.kr};
    y_val = {MouseY.kr};
    Poll.kr(t_poll, [x_val, y_val], ["x_val", "y_val"]);

}.play };
s.mouseUpAction = { x.set(\t_poll,1) };

Of course, this does not work, because mouseDownAction seems reserved as a property of the 'View' class ie only clicking within a specific window, as the below working [albeit not quite what I want] code:

w = Window.new("Mouse Coordinates", Rect(1300,600,50,50));
b = Button.new(w,Rect(10,10,40,25)).states_([["off"],["on"]]);
b.mouseDownAction = { x = {|t_poll=0| var x_val, y_val;
    x_val = {MouseX.kr};
    y_val = {MouseY.kr};
    Poll.kr(t_poll, [x_val, y_val], ["x_val", "y_val"]);
}.play };
b.mouseUpAction = { x.set(\t_poll,1) };
w.front;

Some things I want to know:

  1. Can I modify the first snippet to actually work?
  2. Is there a way to get MouseDown to work to give me these coordinates as I click anywhere on the screen?
  3. How can I figure out how to 'get' the mouse coordinates (calling on which functions [already tried 'output', 'postln', &.c])?

Thanks!!!

There is a Ugen for the mouse button https://doc.sccode.org/Classes/MouseButton.html

This is the example from the linked helpfile:

(
SynthDef( \mousexyb, { |out=0|
    var mousex, mousey, mousebutton;
    mousex = MouseX.kr( 500, 1000 ); // this will determine the frequency of the sound (minimum value, maximum value, warp, lag)
    mousey = MouseY.kr( 0, 0.3 ); // this will determine the amplitude of the sound
    mousebutton = MouseButton.kr( 0, 1, 2 ); // this will turn the sound on or off (minimum value, maximum value, lag)
    Out.ar( out, SinOsc.ar( mousex, 0, mousey ) * mousebutton );
}).add
)

What you could do is have the server generate an OSC message when the button is pressed and have the language listen for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM