Mouse lockt in window screen.

Just starting out? Need help? Post your questions and find answers here.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Mouse lockt in window screen.

Post by bfernhout »

When i start a code and make a window and in the window a windowedscreen. All is going perfect. Even the event to catch the close window button.
But then somewhere in the code i start using the mouse. Using e.g.
Pos_X = mouseX()
Pos_Y = mouseY()

This work all fine but the mouse pointer i used stay in that window. When i try to go outside that window there is no windows mouse showed. So i can not reach the close button anymore.

How can i make it so that i just can go outside the window. And the window can be dragged of closed again.

Bart.
From my first self made computer till now I stil like computers.
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mouse lockt in window screen.

Post by infratec »

Hi,

provide a working code which shows the behaviour and we can take a closer look :wink:
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Mouse lockt in window screen.

Post by #NULL »

There is ReleaseMouse().
You could trigger it via a key combination like in DOSBox to capture/release the mouse.
Or you auto-release the mouse if touching the screen edges, then recapture depending on WindowMouseX/Y().
I have some snippets here that regularly break, but the following seems to work with PB 5.62b2 x64, Windows 7

Code: Select all

InitSprite()
InitMouse()
InitKeyboard()

ww=800
wh=600
style | #PB_Window_ScreenCentered
style | #PB_Window_SystemMenu
style | #PB_Window_MinimizeGadget

If 01
  win=OpenWindow(#PB_Any, 50,100, ww,wh, "", style) :: AddKeyboardShortcut(win, #PB_Shortcut_Escape, 10)
  OpenWindowedScreen(WindowID(win), 0,0, ww,wh, 0,0,0)
Else
  OpenScreen(ww,wh,32,"", #PB_Screen_NoSynchronization)
  SetFrameRate(9999)
EndIf

mouseCaptured = 1

Repeat
  ExamineKeyboard()
  
  ExamineMouse()
  mx = MouseX()
  my = MouseY()
  mw = -MouseWheel()
  
  ; acceleration
;   mousespeed.f = 1.8
;   mx + MouseDeltaX()*mousespeed :: If mx<0 : mx=0 :: ElseIf mx>ww-1 : mx=ww-1 :: EndIf
;   my + MouseDeltaY()*mousespeed :: If my<0 : my=0 :: ElseIf my>wh-1 : my=wh-1 :: EndIf
;   MouseLocate(mx,my)
  
  If IsWindow(win) ;{
    If mouseCaptured
      If mx=0 Or mx=(ww-1) Or my=0 Or my=(wh-1)
;         Debug "----------------- releasing"
;         Debug "s x: " + mx
;         Debug "s y: " + my
;         Debug "w x: " + WindowMouseX(win)
;         Debug "w y: " + WindowMouseY(win)
        
        mouseCaptured=0
        ReleaseMouse(1)
      EndIf
    ElseIf Not mouseCaptured
      If WindowMouseX(win)>0 And WindowMouseX(win)<(ww-1) And WindowMouseY(win)>0 And WindowMouseY(win)<(wh-1)
;         Debug "----------------- capturing"
;         Debug "s x: " + mx
;         Debug "s y: " + my
;         Debug "w x: " + WindowMouseX(win)
;         Debug "w y: " + WindowMouseY(win)
        
        mx=WindowMouseX(win)
        my=WindowMouseY(win)
        mouseCaptured=1
        ReleaseMouse(0)
        MouseLocate(mx,my)
      EndIf
    EndIf
  EndIf
  
  If IsWindow(win) ;{
    Repeat
      event = WindowEvent()
      em = EventMenu()
      Select event
        Case #PB_Event_CloseWindow
          quit = #True
        Case #PB_Event_Menu
          Select em
          Case 10
            quit = #True
          EndSelect
      EndSelect
    Until Not event
    ;}
  EndIf
  
  StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_Transparent)
    Circle(mx ,my, 1)
  StopDrawing()
  
  FlipBuffers()
  ClearScreen($333333)
  If IsWindow(win)
    Delay(10)
  EndIf
Until quit Or KeyboardPushed(#PB_Key_Escape)
Might need some tweaking if used with a strechted/scaled screen.

In Linux (PB 5.62 x64, Ubuntu 16.04) the mouse is released to the center of the window, so to avoid immediate recapture you need a 3-stage check, enabling recapture only after the released mouse has leaved and reentered the window:

Code: Select all

InitSprite()
InitMouse()
InitKeyboard()

ww=800
wh=600

win=OpenWindow(#PB_Any, 50,100, ww,wh, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(win), 0,0, ww,wh, 1,0,0)

captured = 1

Repeat
  ExamineKeyboard()
  ExamineMouse()
  
;   Debug "------"
;   Debug MouseX()
;   Debug MouseY()
;   Debug WindowMouseX(win)
;   Debug WindowMouseY(win)
  
  If captured = 1
    If MouseX() = 0 Or MouseY() = 0 Or MouseX() = ww-1 Or MouseY() = wh-1
      Debug "-> -1"
      captured = -1
      ReleaseMouse(1)
      ExamineMouse()
    EndIf
  EndIf
  
  If captured = -1
    If WindowMouseX(win) < 0 Or WindowMouseY(win) < 0
      Debug "-> 0"
      captured = 0
    EndIf
  EndIf
  
  If captured = 0
    If WindowMouseX(win) >= 1 And WindowMouseY(win) >= 1
     Debug "-> 1"
     captured = 1
      ReleaseMouse(0)
      MouseLocate(WindowMouseX(win), WindowMouseY(win))
      Debug MouseX()
      Debug MouseY()
      ExamineMouse()
      Debug MouseX()
      Debug MouseY()
    EndIf
  EndIf
  
;   If KeyboardPushed(#PB_Key_C) And captured = 0
;     ReleaseMouse(0)
;     captured = 1
;   EndIf
;   If Not KeyboardPushed(#PB_Key_C) And captured = 1
;     ReleaseMouse(1)
;     captured = 0
;   EndIf
  
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = #True
    EndSelect
  Until Not event
  
  If captured
    StartDrawing(ScreenOutput())
      Box(MouseX(), MouseY(), 3, 3)
    StopDrawing()
  EndIf
  
  FlipBuffers()
  ClearScreen($333333)
Until quit Or KeyboardPushed(#PB_Key_Escape)
Its weird but at least you can leave the window. Maybe some GTK API might be able to correct the window mouse position to the edge of the screen after release.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: Mouse lockt in window screen.

Post by bfernhout »

Thanks #Null

That was the excact awnser i need. I do understand the code. But i have one little question. Why testing windowed X and Y mouse when i can get the x and y normal mouse position. Are they not the same when i enter the window again.

I did test the code it work as you written it. I did not test my theory. Busy to translate the PB 5.60 user manual. For some people (And me to) who can't read or write English. But want to try to work with PB. I did showed them how good it is. (Better then BB). They will buy it when the manual is ready. But that is out of this topic.

Anyway Thanks a lot.
From my first self made computer till now I stil like computers.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Mouse lockt in window screen.

Post by #NULL »

bfernhout wrote:Why testing windowed X and Y mouse when i can get the x and y normal mouse position. Are they not the same when i enter the window again.
At least here on Windows 7 the mouse lib (ExamineMouse, MouseX, MouseY etc.) does not report the correct mouse coordinates if the mouse is not captured / if it is released, so you need to query WindowMouseX/Y which are updated by the event loop and not by the mouse lib / DirectX, and store that back via MouseLocate(), if that makes sense. :)
Post Reply