Detecting a Double Click
Author |
Message |
Prince Pwn
|
Posted: Tue May 10, 2011 2:06 pm Post subject: Detecting a Double Click |
|
|
What is it you are trying to achieve?
Obtaining if a double click occurs
What is the problem you are having?
Cannot detect time since last click
Describe what you have tried to solve this problem
Creating a counter if button = 1 but continues counting as long as button is held. This is due to button always equalling 1 as long as button is held.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
View.Set ("graphics;offscreenonly")
Mouse.ButtonChoose ("multibutton")
var driveArray : string := ""
for i : 'C' .. 'Z'
if Dir.Exists (i + ':') then
driveArray + = i
end if
end for
var N : int := length (driveArray )
var SELECTED : array 1 .. N of boolean
for i : 1 .. N
SELECTED (i ) := false
end for
var folderX := 10
var folderY := maxy - 60
var folderWidth := 50
var folderHeight := 50
var selectionRadius : int := - 3
var clickTimer : boolean := false
var clickTimerCounter : int := 0
var mouseX, mouseY, button, left, middle, right : int
Mouse.Where (mouseX, mouseY, button )
var font := Font.New ("Serif:40")
loop
Mouse.Where (mouseX, mouseY, button )
for i : 0 .. N - 1
Draw.Box (folderX + (i * folderWidth ), folderY, folderX + folderWidth + (i * folderWidth ), folderY + folderHeight, black)
if mouseX >= folderX + (i * folderWidth ) and mouseX <= folderX + folderWidth + (i * folderWidth ) and mouseY >= folderY and mouseY <= folderY + folderHeight then
Draw.Box (folderX + (i * folderWidth ), folderY, folderX + folderWidth + (i * folderWidth ), folderY + folderHeight, brightred)
if button = 1 then
for s : 1 .. N
SELECTED (s ) := false
end for
SELECTED (i + 1) := true
end if
end if
if SELECTED (i + 1) then
Draw.Box (folderX + (i * folderWidth ) + selectionRadius, folderY + selectionRadius, folderX + folderWidth + (i * folderWidth ) - selectionRadius, folderY + folderHeight - selectionRadius,
black)
end if
Font.Draw (driveArray (i + 1), 5 + folderX + (i * folderWidth ), maxy - 53, font, black)
folderX + = 20
end for
if button = 1 then
clickTimerCounter + = 1
end if
put clickTimerCounter
folderX := 10
View.Update
cls
end loop
|
Please specify what version of Turing you are using
4.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Tue May 10, 2011 2:21 pm Post subject: RE:Detecting a Double Click |
|
|
If you sample the mouse in two consecutive steps, you get a total of 4 different mouse states. E.g: [Down, Down] is just the button held down; [Down, Up] is the instance when the button was released up; etc. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Prince Pwn
|
Posted: Tue May 10, 2011 2:57 pm Post subject: RE:Detecting a Double Click |
|
|
What do you mean by sampling it in two steps, like a mouse.where after a mouse.where? How does that work exactly. |
|
|
|
|
 |
Tony

|
Posted: Tue May 10, 2011 3:03 pm Post subject: RE:Detecting a Double Click |
|
|
Mouse.Where will give you the state of the mouse at some point in time. If you plot the mouse's state over a period of time, a click might look something like
To detect the | edge, you would need to consider more than a single point. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
tyuo9980
|
Posted: Tue May 10, 2011 4:07 pm Post subject: Re: Detecting a Double Click |
|
|
detect the time in between the clicks. if the delay is short enough then label it as a double click.
say if you want to detect a double click up to 1 second.
timelast := Time.Elapsed
loop
if clicked then
timenow := Time.Elapsed
end if
if timenow - timelast < 1000 then
timelast := timenow <- to reset time counter
*your button thing here*
end if
end loop |
|
|
|
|
 |
Raknarg

|
Posted: Tue May 10, 2011 6:11 pm Post subject: Re: Detecting a Double Click |
|
|
Why don't you try using a counter?
If it was me, I would have something like this:
Turing: |
if clicked then
startcounter
elsif startcounter = true and clicked and counter < (somenumber) then
doubleclicked
elsif start counter = true then
add to counter
end if
|
|
|
|
|
|
 |
Zren

|
Posted: Tue May 10, 2011 6:37 pm Post subject: Re: Detecting a Double Click |
|
|
Raknarg @ Tue May 10, 2011 6:11 pm wrote: Why don't you try using a counter?
If it was me, I would have something like this:
Turing: |
if clicked then
startcounter
elsif startcounter = true and clicked and counter < (somenumber) then
doubleclicked
elsif start counter = true then
add to counter
end if
|
Why count to infinity when you don't need to? That counter will add to the total CPU use. It's also in effect when you're not even clicking. All you really need is a timestamp. It'll also cause integer overflow if left long enough and a miraculously timed return could missfire. |
|
|
|
|
 |
Prince Pwn
|
Posted: Wed May 11, 2011 2:28 pm Post subject: RE:Detecting a Double Click |
|
|
The issue I'm encountering is when you click the mouse once, ie : if mousebutton = 1, that holds true even if you press mousebutton like half a second. So any code you put in there gets executed many times. And I don't want a delay to slow the entire program.
edit: messy but i think i got it:
Turing: |
if button = 1 then
oneClick := true
end if
if oneClick then
cnt += 1
end if
if cnt = 1000 then
cnt := 0
oneClick := false
button := 0
end if
put cnt
if oneClick = true and cnt > 250 and cnt < 1000 then
if button = 1 then
quit
end if
end if
|
problem is if you hold it down too long it bombs out. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Wed May 11, 2011 2:48 pm Post subject: Re: RE:Detecting a Double Click |
|
|
Prince Pwn @ Wed May 11, 2011 2:28 pm wrote: problem is if you hold it down too long it bombs out.
That's why mouse events are typically fired at MouseDown or MouseUp, as those are transitional states and cannot be held in place (as discussed above). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Zren

|
Posted: Thu May 12, 2011 6:07 am Post subject: RE:Detecting a Double Click |
|
|
Here's an example with keyboard input, extending it to mouse input shouldn't be too hard.
Turing: |
var fpsDelay : int
var charsLastUp : array char of int
var chars, charsLast : array char of boolean
proc initialize
Input.KeyDown (chars )
for c : char
charsLastUp (c ) := minint div 2
end for
end initialize
fcn doubleType (s : char, maxDelay : int) : boolean
result Time.Elapsed - charsLastUp (s ) <= maxDelay
end doubleType
proc updateCharsLastUp (s : char)
charsLastUp (s ) := Time.Elapsed
end updateCharsLastUp
fcn keyUp (s : char) : boolean
result ~chars (s ) and charsLast (s )
end keyUp
fcn keyDown (s : char) : boolean
result chars (s ) and ~charsLast (s )
end keyDown
proc input
for c : char
if keyDown (c ) then
put "KeyDown (", c, ")"
end if
if keyUp (c ) then
put "KeyUp (", c, ") (", Time.Elapsed - charsLastUp (c ), ")" ..
if doubleType (c, 1000) then
put " DOUBLE TYPE "
else
put ""
end if
updateCharsLastUp (c )
end if
end for
end input
proc setFPS (n : int)
fpsDelay := 1000 div n
end setFPS
proc run
initialize
loop
charsLast := chars
Input.KeyDown (chars )
exit when chars (KEY_ESC)
input
View.Update
delay (fpsDelay )
end loop
end run
setFPS (60)
run
|
|
|
|
|
|
 |
|
|