[chuck-users] mouse position

Joel Matthys jwmatthys at yahoo.com
Thu Oct 23 04:05:48 EDT 2008


Actually, this is an interesting question. Hid tracks mouse motion 
rather than mouse position, so it's not so easy to create a ChucK patch 
that depends on the mouse's position on the screen. (ie trigger a sound 
when the mouse is in the upper left hand corner.)

I modified the mouse.ck example below. It prompts you to enter the 
screen size (one could easily set this up as an argument; is there any 
way for ChucK to access this property without user input?) and 
calibrates the mouse position from the upper left hand corner.

This works fine if I move the mouse slowly, but it's not absolutely 
correlative to the pointer position on the screen. Any suggestions on 
how to refine this?

Joel

Chuck code follows:

// mouse-pos.ck

// make HidIn and HidMsg
Hid hi;
HidMsg msg;
// make ConsoleInput
ConsoleInput in;

// which mouse
0 => int device;
// get from command line
if( me.args() ) me.arg(0) => Std.atoi => device;

// open mouse 0, exit on fail
if( !hi.openMouse( device ) ) me.exit();
<<< "mouse '" + hi.name() + "' ready", "" >>>;

int atZero, mouseX, mouseY, screenWidth, screenHeight;

// prompt user to enter screen size
in.prompt( "enter screen width:" ) => now;
in.getLine() => Std.atoi => screenWidth;
in.prompt( "enter screen height:" ) => now;
in.getLine() => Std.atoi => screenHeight;
in.prompt( "mouse mouse to upper left corner and press enter.") => now;

/* alternative method - calibrate via mouse click

<<< "Move mouse to upper left corner and click" >>>;

while (!atZero)
{
    hi => now;
    while( hi.recv( msg ) )
    {
        if( msg.isButtonDown() )
        {
            1 => atZero;
        }
    }
}*/

// infinite event loop
while( true )
{
    // wait on HidIn as event
    hi => now;

    // messages received
    while( hi.recv( msg ) )
    {
        // mouse motion
        if( msg.isMouseMotion() )
        {
            if( msg.deltaX )
            {
                msg.deltaX +=> mouseX;
            }
            if( msg.deltaY )
            {
                msg.deltaY +=> mouseY;
            }
        }
       
        // keep values within area of screen
        constrain(mouseX, 0, screenWidth) $ int => mouseX;
        constrain(mouseY, 0, screenHeight) $ int => mouseY;
       
        <<< "Mouse Position:",mouseX,mouseY>>>;
    }
}

fun float constrain (float test, float v1, float v2)
{
    Math.min (v1,v2) => float bottom;
    Math.max (v1,v2) => float top;
    Math.max (test, bottom) => test;
    Math.min (test, top) => test;
    return test;
}

// end of mouse-pos.ck

eduard aylon wrote:
> Hello list,
>
> is there a way to track y-position of the mouse pointer. It's not in 
> the manual.
> I know I can track the x-position with HidMsg.fdata
>
> thanks,
>
> eduard
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>




More information about the chuck-users mailing list