Clicking mouse button
Author |
Message |
ans4
|
Posted: Fri Oct 27, 2006 11:49 pm Post subject: 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.
Quote: var a : string
loop
put "Enter a"
get a
if Mouse.ButtonMoved ("down") then
exit
end if
end loop
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Sat Oct 28, 2006 12:20 am Post subject: (No subject) |
|
|
Since you are nwe to Turing, you should realize that F10 will be your new best friend. Here is an example from it
code: |
% 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.
code: | 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
|
|
|
|
|
|
![](images/spacer.gif) |
ans4
|
Posted: Sat Oct 28, 2006 12:33 am Post subject: (No subject) |
|
|
Thanks and I have another question.
It is possible to exit a loop with a mouse click after a get command right? |
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Sat Oct 28, 2006 2:36 am Post subject: (No subject) |
|
|
yea! |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat Oct 28, 2006 11:52 am Post subject: (No subject) |
|
|
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 :
code: | var a : string
var x, y, button : int
get a
loop
Mouse.Where (x, y, button)
exit when button not= 0
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
ans4
|
Posted: Sat Oct 28, 2006 12:18 pm Post subject: (No subject) |
|
|
But how do I exit the loop with get statement in it by clicking it? |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat Oct 28, 2006 12:34 pm Post subject: (No subject) |
|
|
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 :
code: | 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 |
|
|
|
|
|
![](images/spacer.gif) |
ans4
|
Posted: Sat Oct 28, 2006 12:41 pm Post subject: (No subject) |
|
|
richcash wrote: 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 :
code: | 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat Oct 28, 2006 12:58 pm Post subject: (No subject) |
|
|
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 :
code: | 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 |
|
|
|
|
|
![](images/spacer.gif) |
ans4
|
Posted: Sat Oct 28, 2006 1:24 pm Post subject: (No subject) |
|
|
Thanks, that one worked. Now is there a way to only exit when you click on a specific place on the screen? |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat Oct 28, 2006 1:38 pm Post subject: (No subject) |
|
|
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 :
code: | var ch : string (1)
var a : string := ""
var x, y, button : int
Draw.FillBox (100, 100, 200, 150, 4) %<- here is my box
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 and x >= 100 and y >= 100 and x <= 200 and y <= 150
%Notice the numbers used here and the numbers for drawing the box
end loop |
Oh, and the enter key is chr (10), so if you want it to exit when the user clicks or presses enter, then add that in. Right now the enter key goes to the next line like on a word processor. I'll see if you can do that on your own to make sure you understand. |
|
|
|
|
![](images/spacer.gif) |
ans4
|
Posted: Sat Oct 28, 2006 2:30 pm Post subject: (No subject) |
|
|
Thanks for the help. And to exit the program you could also use KEY_ENTER right?
[/code]var ch : string (1)
var a : string := ""
var x, y, button : int
Draw.FillBox (100, 100, 200, 150, 4) %<- here is my box
loop
if hasch then
getch (ch)
a += ch
locate (1, 1)
put a
exit when ch = chr (10)
end if
Mouse.Where (x, y, button)
exit when button not= 0 and x >= 100 and y >= 100 and x <= 200 and y <= 150
%Notice the numbers used here and the numbers for drawing the box
end loop |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat Oct 28, 2006 2:39 pm Post subject: (No subject) |
|
|
Yap, KEY_ENTER can be used instead of chr (10).
Nice code, I think you understand. |
|
|
|
|
![](images/spacer.gif) |
|
|