Gadget longpress detection

Just starting out? Need help? Post your questions and find answers here.
Dirk Geppert
Posts: 334
Joined: Fri Sep 22, 2017 7:02 am

Gadget longpress detection

Post by Dirk Geppert »

Hey guys,

do you have any idea how a long press on a ListIconGadget entry can be detected?

Unfortunately, there are no events like #PB_EventType_DragStart or
#PB_EventType_LeftButtonDown, #PB_EventType_LeftButtonUp to determine the duration of the press.

I hoped it would be as easy as this one:

Code: Select all

Procedure onLongPress()
   ; do here the stuff after long press
EndProcedure

If OpenWindow(0, 100, 100, 300, 100, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ListIconGadget(0, 5, 5, 290, 90, "Name", 100)
   AddGadgetColumn(0, 1, "Address", 250)
   AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
   AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 SpiderBasic Road, BigTown, CodeCity")
   
   BindGadgetEvent(0, @onLongPress(), #PB_EventType_LeftLongClick)
 EndIf
falsam
Posts: 288
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: Gadget longpress detection

Post by falsam »

I like this question. Here is one possible answer using a little JavaScript to handle the “mousedown” and “mouseup” events.

The long press is set to 3 seconds, which you can change in the onMouseDown() procedure.

For fun, after pressing and holding for 3 seconds, the code displays a window showing information about the current item.

Code: Select all

EnableExplicit

Enumeration 
  #timer
  #toolTip
EndEnumeration

Global currentItem

Procedure onClose()
  If EventWindow() = #toolTip
    CloseWindow(#toolTip)
  EndIf
EndProcedure

; Cancel timer after 3 seconds
Procedure onMouseLongpress()
  Protected buffer.s
  
  Debug "Item " + Str(currentItem) + " Longpress"  
  RemoveWindowTimer(0, #timer)
  
  ; Show record
  buffer = "<b>" + GetGadgetItemText(0, currentItem, 0) + "</b>" + "<br>" + GetGadgetItemText(0, currentItem, 1) 
  OpenWindow(#toolTip, WindowX(0) + 100, WindowY(0) + 100 , 400, 200, "Information")
  TextGadget(#PB_Any, 10, 10, 380, 180, buffer)
  
  BindEvent(#PB_Event_CloseWindow, @onClose(), #toolTip)
EndProcedure

; Remove timer if mouse up
; Remove current selection
Procedure onMouseUp()
  If currentItem <> -1
    Debug "Item " + Str(currentItem) + " Mouse Up"
  EndIf 
  RemoveWindowTimer(0, #timer)
  SetGadgetState(0, -1)
EndProcedure

; Add timer if mouse down
Procedure onMouseDown()
  currentItem = GetGadgetState(0)
  
  If currentItem <> -1
    Debug "Item " + currentItem + " Mouse Down : Wait longpress 3 seconds"
    
    ; Add timer 3 seconds
    AddWindowTimer(0, #timer, 3000)
    BindEvent(#PB_Event_Timer, @onMouseLongpress())
    
    If IsWindow(#toolTip)
      CloseWindow(#toolTip)
    EndIf
    
  EndIf 
EndProcedure


If OpenWindow(0, 100, 100, 300, 120, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListIconGadget(0, 5, 5, 290, 90, "Name", 100)
  AddGadgetColumn(0, 1, "Address", 250)
  AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
  AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 SpiderBasic Road, BigTown, CodeCity")
  
  ; Add JavaScript mousedown and mouseup events
  EnableJS
   addEventListener("mousedown", f_onmousedown)
   addEventListener("mouseup", f_onmouseup)
  DisableJS
  
EndIf

It was fun 🤪

➽ Windows 11 - jdk-11.0.2 - SB 3.10 - Android 16
https://falsam.com

Sorry for my poor english
Dirk Geppert
Posts: 334
Joined: Fri Sep 22, 2017 7:02 am

Re: Gadget longpress detection

Post by Dirk Geppert »

Thank you very much falsam! That's a great implementation. Works perfectly. 😊👍👍👍
Post Reply