Get canvas mouse position?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Get canvas mouse position?

Post 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?
User avatar
SPH
Enthusiast
Enthusiast
Posts: 572
Joined: Tue Jan 04, 2011 6:21 pm

Re: Get canvas mouse position?

Post by SPH »

Only a lot of "0"

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: Get canvas mouse position?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get canvas mouse position?

Post 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
Egypt my love
Axolotl
Addict
Addict
Posts: 838
Joined: Wed Dec 31, 2008 3:36 pm

Re: Get canvas mouse position?

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Get canvas mouse position?

Post 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).
Post Reply