-
-
Notifications
You must be signed in to change notification settings - Fork 953
getMouseLocation? #3170
-
My request is a possibility/method to get the coordinates of the mouse pointer in the coding area or the sonic-pi window or screen. Maybe a method already exists.
I think this would be nice to do some cool stuff when live coding.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
Here is my way:
First try
In a Forum i found this
require 'rbconfig' ## # Returns an array [x,y] containing the mouse coordinates # Be aware that the coordinate system is OS dependent. def getMouseLocation def windows require "Win32API" getCursorPos = Win32API.new("user32", "GetCursorPos", 'P', 'L') # point is a Long,Long-struct point = "0円" * 8 if getCursorPos(point) a.unpack('LL') else [nil,nil] end end def linux loc_string = `xdotool getmouselocation --shell`[/X=(\d+)\nY=(\d+)/] loc_string.lines.map {|s| s[/.=(\d+)/, 1].to_i} end def osx # if we are running in RubyCocoa, we can access objective-c libraries require "osx/cocoa" OSX::NSEvent.mouseLocation.to_a rescue LoadError # we are not running in ruby cocoa, but it should be preinstalled on every system coords = `/usr/bin/ruby -e 'require "osx/cocoa"; puts OSX::NSEvent.mouseLocation.to_a'` coords.lines.map {|s| s.to_f } end case RbConfig::CONFIG['host_os'] when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ windows when /darwin|mac os/ osx when /linux|solaris|bsd/ linux else raise Error, "unknown os: #{host_os.inspect}" end rescue Exception => e [nil,nil] end
but for me (Mac OS 10.10.5) it doesn't work. Maybe it's useful for someone else.
Second try
With the gem mouse it works in the terminal but binding and requiring mouse in Sonic-Pi, unfortunately leads to an error.
Solution using mouse gem and DRuby (thanks to rbnrpi)
creating an DRuby Server and run it in the terminal.
require 'drb/drb' require 'mouse' # The URI for the server to connect to URI="druby://localhost:8787" class MouseServer def get_MouseLocation return Mouse.current_position.to_a end def get_MouseX return Mouse.current_position.to_a[0].to_i end def get_MouseY return Mouse.current_position.to_a[1].to_i end end FRONT_OBJECT=MouseServer.new $SAFE = 1 DRb.start_service(URI, FRONT_OBJECT) DRb.thread.join
And in Sonic-Pi i use it that way
require 'drb/drb' ######## talk to the server ######## SERVER_URI="druby://localhost:8787" DRb.start_service mouse = DRbObject.new_with_uri(SERVER_URI) #mouseLocation = mouse.get_MouseLocation #mouseX = mouse.get_MouseX #mouseY = mouse.get_MouseY ### Using Mousemovement to control the cutoff-value ### live_loop :mouse_cutoff do mouseX = mouse.get_MouseX s = sample :loop_amen, cutoff: ((130.0/1279.0)*mouseX) 100.times do sleep 0.01 mouseX = mouse.get_MouseX s.control cutoff: ((130.0/1279.0)*mouseX) end end
But maybe there is a better solution.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi,
I'm happy for you to explore ways of getting mouse movement into Sonic Pi - but for now this won't be supported until the planned IO infrastructure has landed. That will likely require a C library which talks OSC. If you're interested in looking at creating such a thing, then please let me know!
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi! Is this still something that could be looked at?
A quick google found some things that may achieve some of this - the first is "vanilla c" with "some osc functionality (not 'all') , not sure what else is required, or how incompatible / inappropriate this is https://github.com/mhroth/tinyosc
Just a thought... can't even remember how I stumbled upon this thread! think I was looking for references to 'mingw' to see if I needed that to build sp_things!
Ps: Thank you! 🙏
Beta Was this translation helpful? Give feedback.