Mouse status in desktop

Just starting out? Need help? Post your questions and find answers here.
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Mouse status in desktop

Post by Allen »

Hi,

DesktopMouseX() and DesktopMouseY() can return the coordinate of the mouse in dektop. How can I check if any of the mouse button is pressed. The mouse will not be inside any window gadget.

I only run in window. 10

Thanks.

Allen
Axolotl
Enthusiast
Enthusiast
Posts: 798
Joined: Wed Dec 31, 2008 3:36 pm

Re: Mouse status in desktop

Post by Axolotl »

Hi,
on windows you can do the following: (I extended one of the help examples)

Code: Select all

Macro IsKeyPressed(VirtualKey)  ; BOOL .. on #VK_SHIFT, #VK_CONTROL, etc. 
  Bool(GetAsyncKeyState_(VirtualKey) & $8000) 
EndMacro 

Procedure main() 
  If OpenWindow(0, 0, 0, 300, 50, "Position of the mouse on the desktop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StickyWindow(0, 1) ; otherwise mouse clicks outside this window will move it below other windows ...... 
    TextGadget(0, 8,  8, 200, 18, "")
    TextGadget(1, 8, 28, 200, 18, "")

    Repeat
      Event = WaitWindowEvent(20)
      
      If Event = 0 ; No more events in the queue, so let's display the mouse coordinates
        SetGadgetText(0, "Coordinates: " + Str(DesktopMouseX()) + "," + Str(DesktopMouseY()))  
      EndIf

      If IsKeyPressed(#VK_LBUTTON) 
        SetGadgetText(1, "Mouse Buttons: Left ")  
      ElseIf IsKeyPressed(#VK_RBUTTON) 
        SetGadgetText(1, "Mouse Buttons: Right ")  
      Else 
        SetGadgetText(1, "Mouse Buttons: .... ")  
      EndIf 

    Until Event = #PB_Event_CloseWindow
  EndIf
EndProcedure 

End main() 

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

Re: Mouse status in desktop

Post by RASHAD »

Hi
If you want to detect the Mouse events on the desktop you can use Mouse Low Level Hook
Else
For your application only you can use Windows Callback() or Mouse Local Hook
Endif :D
Egypt my love
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Re: Mouse status in desktop

Post by Allen »

Hi Axolotl,

Thank you for your example. It surely gives me a direction where to look for further information.

Hi RASHAD,

As most of my programming knowledge only comes from using Purebasic (learn from examples in help doc) and infromation from this forum. I do not understand your advise. I appreciate if you could provide a demonstration example.

Thank you both for the help.

Allen
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Mouse status in desktop

Post by RASHAD »

Hi Allen
#1 :

Code: Select all

Global oTime,MLLHook

Procedure MouseHook(nCode, wParam, lParam)
  Select wParam      
    Case #WM_LBUTTONDOWN
      If GetTickCount_() < (oTime + GetDoubleClickTime_())
        SetGadgetText(0, "Left Mouse DblClicked")
      Else
        SetGadgetText(0, "Left Mouse Clicked")
      EndIf
      oTime = GetTickCount_()
      
    Case #WM_RBUTTONDOWN
      If GetTickCount_() < (oTime + GetDoubleClickTime_())
        SetGadgetText(0, "Right Mouse DblClicked")
      Else
        SetGadgetText(0, "Right Mouse Clicked")
      EndIf
      oTime = GetTickCount_()
      
    Case #WM_MBUTTONDOWN
      SetGadgetText(0, "Middle Mouse Clicked")
      
    Case #WM_MOUSEWHEEL
      dir = Sign(PeekI(lParam + 8))
      If dir = 1
        SetGadgetText(0, "Wheel UP") 
      Else
        SetGadgetText(0, "Wheel Down") 
      EndIf
      
  EndSelect
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0, 0, 0, 300, 140, "Mouse Hook", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, 180, 20, "")

MLLHook = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseHook(), GetModuleHandle_(0), 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

UnhookWindowsHookEx_(MLLHook)
#2 :
Enable Thread Safe
No Mouse Wheel

Code: Select all

Global Quit

Procedure MouseEvent(par)
  Repeat    
      If GetAsyncKeyState_(#VK_LBUTTON) & $8000
        SetGadgetText(0,"LButton Down")
      EndIf 
      If GetAsyncKeyState_(#VK_RBUTTON) & $8000
        SetGadgetText(0,"RButton Down")
      EndIf
      If GetAsyncKeyState_(#VK_MBUTTON) & $8000
        SetGadgetText(0,"MButton Down")
      EndIf
      Delay(10)
  Until Quit = 1

EndProcedure

OpenWindow(0, 0, 0, 300, 140, "Mouse Hook", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, 180, 20, "")
ButtonGadget(1,10,40,60,24,"Exit")

Thread = CreateThread(@MouseEvent(),30)

Repeat 
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Quit = 1
      EndSelect
  EndSelect
  Until Quit = 1
Egypt my love
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Re: Mouse status in desktop

Post by Allen »

Hi RASHAD,

Thank you for the example. Definitely able to learn new technique from them.

Thanks

Allen
Post Reply