Page 1 of 1

Get canvas mouse position?

Posted: Fri May 23, 2025 9:34 am
by Joubarbe

Code: Select all

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, 500, 500)

SetActiveGadget(0)

Procedure DisplayMouseX()
  Debug GetGadgetAttribute(0, #PB_Canvas_MouseX)
;   Debug WindowMouseX(0) ; Works fine.
EndProcedure

BindEvent(#PB_Event_Timer, @DisplayMouseX())
BindEvent(#PB_Event_Menu, @DisplayMouseX())
AddWindowTimer(0, 0, 1000)

AddKeyboardShortcut(0, #PB_Shortcut_Space, 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Hmm?

Re: Get canvas mouse position?

Posted: Fri May 23, 2025 9:45 am
by SPH
Only a lot of "0"

Re: Get canvas mouse position?

Posted: Fri May 23, 2025 9:56 am
by pjay
The MouseX / MouseY attributes should be funneled through the MouseMove event:

Code: Select all

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, 500, 500)

SetActiveGadget(0)

Procedure DisplayMouseX()
  Debug GetGadgetAttribute(0, #PB_Canvas_MouseX)
EndProcedure

BindEvent(#PB_Event_Gadget, @DisplayMouseX(), 0, 0, #PB_EventType_MouseMove)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Get canvas mouse position?

Posted: Fri May 23, 2025 10:13 am
by RASHAD
Hi
Maybe 3 or more ways to get the canvas gadget mouse positions
But you lost your way :wink:

Code: Select all

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, 500, 500)

SetActiveGadget(0)

Procedure DisplayMouseX()
  Debug GetGadgetAttribute(0,#PB_Canvas_MouseX)
  ;Debug WindowMouseX(0) ; Works fine.
EndProcedure

;AddWindowTimer(0, 0, 10)

;BindEvent(#PB_Event_Timer, @DisplayMouseX())
BindEvent(#PB_Event_Gadget, @DisplayMouseX())

AddKeyboardShortcut(0, #PB_Shortcut_Space, 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
;     Case #PB_Event_Gadget
;       Select EventGadget()
;         Case 0
;           Select EventType()
;             Case #PB_EventType_MouseMove
;               x =  GetGadgetAttribute(0,#PB_Canvas_MouseX)
;               y =  GetGadgetAttribute(0,#PB_Canvas_MouseY)
;               Debug x
;               Debug y
;           EndSelect
;       EndSelect
  EndSelect
Until Quit = 1

Re: Get canvas mouse position?

Posted: Fri May 23, 2025 10:28 am
by Axolotl
Joubarbe wrote: Fri May 23, 2025 9:34 am .....
Hmm?
Well, just one F1 on

Code: Select all

CanvasGadget(0, 0, 0, 500, 500)
and a little down scrolling will show you a working example.
Online CanvasGadget Documentation

Re: Get canvas mouse position?

Posted: Fri May 23, 2025 11:08 am
by Joubarbe
Okay, sorry then! :D

My example was quite bad, I was trying to reproduce a situation in my main project where, sometimes, I couldn't get the mouse position. Although I realise now that it only works through the gadget event, and what I was trying to do is to get it when another event was triggered (returned 0).