
-----------------------------------
ans4
Fri Oct 27, 2006 11:49 pm

Clicking mouse button
-----------------------------------
I'm new to Turing and I need some help. How can I exit a program after entering a variable and then clicking anywhere on the screen? I've tried doing this but it doesn't work.

var a : string

loop
    put "Enter a"
    get a
    if Mouse.ButtonMoved ("down") then
        exit
    end if
end loop


-----------------------------------
jamonathin
Sat Oct 28, 2006 12:20 am


-----------------------------------
Since you are nwe to Turing, you should realize that F10 will be your new best friend.  Here is an example from it


% The "buttonwait" program.
var x, y, buttonnumber, buttonupdown, buttons : int
var nx, ny : int
loop
    buttonwait ("down", x, y, buttonnumber, buttonupdown)
    nx := x
    ny := y
    loop
        drawline (x, y, nx, ny, 7)          % Erase previous line
        exit when buttonmoved ("up")
        mousewhere (nx, ny, buttons)
        drawline (x, y, nx, ny, brightred)  % Draw line to position
    end loop
    buttonwait ("up", nx, ny, buttonnumber, buttonupdown)
    drawline (x, y, nx, ny, brightgreen)    % Draw line to final position
end loop

A personal preference, i use mousewhere.
var mx, my, mz : int

loop
    mousewhere (mx, my, mz)
    drawline (maxx div 2, maxy div 2, Rand.Int (0, maxx), Rand.Int (0, maxy), Rand.Int (1, maxcolor))
    exit when mz = 1
end loop


-----------------------------------
ans4
Sat Oct 28, 2006 12:33 am


-----------------------------------
Thanks and I have another question.
It is possible to exit  a loop with a mouse click after a get command right?

-----------------------------------
TokenHerbz
Sat Oct 28, 2006 2:36 am


-----------------------------------
yea!

-----------------------------------
richcash
Sat Oct 28, 2006 11:52 am


-----------------------------------
Yes, but you should put your button-detecting in a separate loop, because otherwise (if you stick your get statement in the loop) it will skip right past your checking of the mouse clicked and have another get.

Do this :
var a : string
var x, y, button : int
get a
loop
    Mouse.Where (x, y, button)
    exit when button not= 0
end loop


-----------------------------------
ans4
Sat Oct 28, 2006 12:18 pm


-----------------------------------
But how do I exit the loop with get statement in it by clicking it?

-----------------------------------
richcash
Sat Oct 28, 2006 12:34 pm


-----------------------------------
I'm not too sure what you're talking about. If you want several get statements to be executed separated by mouse clicks, do this :
var a : string
var x, y, button : int
loop
    get a
    loop
        Mouse.Where (x, y, button)
        if button not= 0 then
            exit when button not= 0
            exit
        end if
    end loop
end loop

-----------------------------------
ans4
Sat Oct 28, 2006 12:41 pm


-----------------------------------
I'm not too sure what you're talking about. If you want several get statements to be executed separated by mouse clicks, do this :
var a : string
var x, y, button : int
loop
    get a
    loop
        Mouse.Where (x, y, button)
        if button not= 0 then
            exit when button not= 0
            exit
        end if
    end loop
end loop

Could you get out of the get statement just by clicking the mouse instead of pressing the enter key?

-----------------------------------
richcash
Sat Oct 28, 2006 12:58 pm


-----------------------------------
Oh, no, you can't use the get statement then. The get statement stops everything and waits for input (and for you to press enter). You'll need to use getch or Input.KeyDown. Something like this :
var ch : string (1)
var a : string := ""
var x, y, button : int
loop
    if hasch then
        getch (ch)
        a += ch
        locate (1, 1)
        put a
    end if
    Mouse.Where (x, y, button)
    exit when button not= 0
end loop

-----------------------------------
ans4
Sat Oct 28, 2006 1:24 pm


-----------------------------------
Thanks, that one worked. Now is there a way to only exit when you click on a specific place on the screen?

-----------------------------------
richcash
Sat Oct 28, 2006 1:38 pm


-----------------------------------
Use the x and y passed to Mouse.Where, they are the coordinates of where the mouse is (which pixel the mouse is at). For a rectangular section (as I suspect you want), you need to check if the x of the mouse is between the x's of the box and the y's is between the y's of the box. If you don't know about drawing rectangles, look up Draw.FillBox in F10.
Sample code :
var ch : string (1)
var a : string := ""
var x, y, button : int
Draw.FillBox (100, 100, 200, 150, 4)    %= 100 and y >= 100 and x 