Page 1 of 1

Mouse status in desktop

Posted: Fri Sep 13, 2024 8:52 am
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

Re: Mouse status in desktop

Posted: Fri Sep 13, 2024 1:03 pm
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() 


Re: Mouse status in desktop

Posted: Fri Sep 13, 2024 2:12 pm
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

Re: Mouse status in desktop

Posted: Fri Sep 13, 2024 4:18 pm
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

Re: Mouse status in desktop

Posted: Fri Sep 13, 2024 8:43 pm
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

Re: Mouse status in desktop

Posted: Sat Sep 14, 2024 4:24 am
by Allen
Hi RASHAD,

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

Thanks

Allen