ElapsedMilliseconds( )

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

ElapsedMilliseconds( )

Post by mestnyi »

is this how it should be?

Code: Select all

Debug ElapsedMilliseconds( )
; result window = 0
; result macos = 830
// Moved from "Off Topic" to "Coding Questions" (Kiffi)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ElapsedMilliseconds( )

Post by Little John »

mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

I needed it for this, but in windows it doesn’t work the way I expected, maybe I’m doing something wrong.

Code: Select all

Static ClickTime.q, ClickCount
Protected DoubleClickTime
;CompilerIf #PB_Compiler_OS = #PB_OS_Windows
 ; DoubleClickTime = 10
;CompilerElse
  DoubleClickTime = DoubleClickTime( )
;CompilerEndIf
If ClickTime And
   DoubleClickTime > ( ElapsedMilliseconds( ) - ClickTime )
  ClickCount + 1
Else
  ClickCount = 1
EndIf
ClickTime = ElapsedMilliseconds( )
Last edited by mestnyi on Fri Feb 03, 2023 9:22 am, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by infratec »

You need at least 2 calls to ElapsedMilliseconds().
Else the start value maybe undefined.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ElapsedMilliseconds( )

Post by Little John »

mestnyi wrote: Fri Feb 03, 2023 9:18 am maybe I’m doing something wrong.
I already posted the link to the documentation for ElapsedMilliseconds().
What exactly is still unclear after reading it :?:
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by infratec »

This seams to work:

Code: Select all

#DoubleClickTime = 500

Structure MouseButton_Structure
  Pressed.i
  ReleaseTime.i
EndStructure

Global Dim MouseButtonArray.MouseButton_Structure(3)


Procedure.i IsMouseDoubleClick(MouseButton.i)
  
  Protected Result.i
  
  
  If MouseButtonArray(MouseButton)\Pressed
    If MouseButton(MouseButton) = 0
      MouseButtonArray(MouseButton)\Pressed = #False
      MouseTime = ElapsedMilliseconds()
      If MouseButtonArray(MouseButton)\ReleaseTime > -1
        If MouseButtonArray(MouseButton)\ReleaseTime + #DoubleClickTime > MouseTime
          Result = #True
        EndIf
      EndIf
      MouseButtonArray(MouseButton)\ReleaseTime = MouseTime
    EndIf
  Else
    If MouseButton(MouseButton)
      MouseButtonArray(MouseButton)\Pressed = #True
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




MouseButtonArray(#PB_MouseButton_Left)\ReleaseTime = -1
MouseButtonArray(#PB_MouseButton_Right)\ReleaseTime = -1
MouseButtonArray(#PB_MouseButton_Middle)\ReleaseTime = -1

InitSprite()
InitMouse()
InitKeyboard()

OpenWindow(0, 0, 0, 220, 160, "Press ESC to end", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  
OpenWindowedScreen(WindowID(0), 0, 0, 220, 160)

Repeat
  
  Repeat
    Event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  FlipBuffers() 
  ClearScreen(RGB(0, 0, 0))
  
  ExamineMouse()
  
  If IsMouseDoubleClick(#PB_MouseButton_Left)
    Debug "DoubleClick"
  EndIf
  
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Escape)
    End
  EndIf
  
  Delay(1)
ForEver
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

that's what i mean.
it works fine on mac os and Linux not windows.

Code: Select all

Procedure MouseState( )
  Static press.b, ClickTime.q, ClickCount
  Protected DoubleClickTime, state.b
  
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      Protected desktop_x, desktop_y, handle, *GdkWindow.GdkWindowObject = gdk_window_at_pointer_( @desktop_x, @desktop_y )
      
      If *GdkWindow
        gdk_window_get_pointer_(*GdkWindow, @desktop_x, @desktop_y, @mask)
      EndIf
      
      If mask & 256; #GDK_BUTTON1_MASK
        state = 1
      EndIf
      If mask & 512 ; #GDK_BUTTON3_MASK
        state = 3
      EndIf
      If mask & 1024 ; #GDK_BUTTON2_MASK
        state = 2
      EndIf
      
    CompilerCase #PB_OS_Windows
      state = GetAsyncKeyState_(#VK_LBUTTON) >> 15 & 1 + 
              GetAsyncKeyState_(#VK_RBUTTON) >> 15 & 2 + 
              GetAsyncKeyState_(#VK_MBUTTON) >> 15 & 3 
    CompilerCase #PB_OS_MacOS
      state = CocoaMessage(0, 0, "NSEvent pressedMouseButtons")
  CompilerEndSelect
  
  If press <> state
    If state
      If DoubleClickTime( ) > ( ElapsedMilliseconds( ) - ClickTime )
        ClickCount + 1
      Else
        ClickCount = 1
      EndIf
      ClickTime = ElapsedMilliseconds( )
      
      If ClickCount = 1
        If state = 1
          Debug "LeftDown - "
        ElseIf state = 2
          Debug "RightDown - "
        EndIf
      EndIf
      
    Else
      If ClickCount = 1
        If press = 1
          Debug "LeftUp - "
        ElseIf press = 2
          Debug "RightUp - "
        EndIf
      EndIf
      
      ;\\ do 3click events
      If ClickCount = 3
        If press = 1
          Debug "   Left3Click - "
        ElseIf press = 2
          Debug "   Right3Click - "
        EndIf
        
        ;\\ do 2click events
      ElseIf ClickCount = 2
        If press = 1
          Debug "  Left2Click - "
        ElseIf press = 2
          Debug "  Right2Click - "
        EndIf
        
        
        ;\\ do 1click events
      Else
        ;         If Not PressedWidget( )\state\drag
        ;           If PressedWidget( ) = EnteredWidget( )
        If press = 1
          Debug " LeftClick - "
        ElseIf press = 2
          Debug " RightClick - "
        EndIf
        
        ;           EndIf
        ;         EndIf
      EndIf
      
    EndIf
    press = state
  EndIf
  
EndProcedure


OpenWindow(0, 0, 0, 220, 160, "Press ESC to end", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  


Repeat
  Event = WaitWindowEvent()
  MouseState( )
  
Until Event = #PB_Event_CloseWindow
Last edited by mestnyi on Mon Feb 06, 2023 8:53 pm, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ElapsedMilliseconds( )

Post by infratec »

I'm on win10 x64 with PB 6.01b2 x86

If I click double, I get
LeftDown -
LeftUp -
LeftClick -
Left2Click -
A single click resulte in
LeftDown -
LeftUp -
LeftClick -
LeftDown -
Only if I start with a double click it fails once.

What do you expect?
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

That's what I expect
and the same behavior on all three wasps (windows, mac os, linux)

Code: Select all

1click
LeftDown - 
LeftUp - 
 LeftClick - 
 
 2click
 LeftDown - 
LeftUp - 
 LeftClick - 
  Left2Click - 

3click
LeftDown - 
LeftUp - 
 LeftClick - 
  Left2Click - 
   Left3Click - 
 
4>=click
LeftDown - 
LeftUp - 
 LeftClick - 
  Left2Click - 
   Left3Click - 
 LeftClick - 
 LeftClick - 
 LeftClick - 
 LeftClick - 
 LeftClick - 
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

Well, what do you say, is this how it should be? Works on Mac OS and Linux but does not work on Windows.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds( )

Post by Olli »

Source code of DoubleClickTime() ? (actual code not complete). Imagining the source code, I suggest you to add a binary AND mask ('&' symbol) anywhere
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

Olli wrote: Sat Feb 04, 2023 3:53 pm Source code of DoubleClickTime() ? (actual code not complete). Imagining the source code, I suggest you to add a binary AND mask ('&' symbol) anywhere
I honestly did not understand anything, what do you mean can you show the code? :(
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds( )

Post by Olli »

mestnyi wrote: Sat Feb 04, 2023 9:19 pm
Olli wrote: Sat Feb 04, 2023 3:53 pm Source code of DoubleClickTime() ? (actual code not complete). Imagining the source code, I suggest you to add a binary AND mask ('&' symbol) anywhere
I honestly did not understand anything, what do you mean can you show the code? :(
Sorry I looped this native function DoubleClickTime().

Also... Here :

Code: Select all

state = GetAsyncKeyState_(#VK_LBUTTON) >> 15 & 1 + 
              GetAsyncKeyState_(#VK_RBUTTON) >> 15 & 2 + 
              GetAsyncKeyState_(#VK_MBUTTON) >> 15 & 3
... I doubt & 3 is the good mask, and the add op surprises me. I d prefer this :

Code: Select all

state = (GetAsyncKeyState_(#VK_LBUTTON) >> 16 & 1) | 
              2*(GetAsyncKeyState_(#VK_RBUTTON) >> 16 & 1) | 
              3*(GetAsyncKeyState_(#VK_MBUTTON) >> 16 & 1)
Other thing :

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        DoubleClickTime = 10
      CompilerElse
        DoubleClickTime = DoubleClickTime( )
      CompilerEndIf
You should just type this :

Code: Select all

DoubleClickTime = DoubleClickTime( )
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ElapsedMilliseconds( )

Post by mestnyi »

Olli wrote: Sun Feb 05, 2023 10:23 am You should just type this :

Code: Select all

DoubleClickTime = DoubleClickTime( )
For what? On windows it doesn't work that way.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: ElapsedMilliseconds( )

Post by Olli »

mestnyi wrote: Sun Feb 05, 2023 7:41 pm
Olli wrote: Sun Feb 05, 2023 10:23 am You should just type this :

Code: Select all

DoubleClickTime = DoubleClickTime( )
For what? On windows it doesn't work that way.
If you are sure about this bug :

Code: Select all

DoubleClickTime = 300
(keeping your OS compiling directives)
Post Reply