QB64 Phoenix Edition
Detect when mouse leaves program window - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Detect when mouse leaves program window (/showthread.php?tid=1908)

Pages: 1 2 3


RE: Detect when mouse leaves program window - TerryRitchie - 08-18-2023

Thanks Steve, I'll take a look at these examples.


RE: Detect when mouse leaves program window - TempodiBasic - 09-09-2023

Hi Terry,
I have thought a snippet all in QB64 code... but it seems that I have fallen in an issue about _MOUSEMOVEMENTX/Y
These functions seem to loose some data while they are running.

Here the snippet in QB64 that looses data during the run!

Code: (Select All)
Rem the goal: we want to detect if mouse is out or in window's area

Rem the idea is to evaluate the position of the mouse at starting of the run of the application
Rem if QB64 does not set mouse in an that area at run time, we can set position of mouse with related functions

Option _Explicit

Dim S As Long
Dim As Integer W, H, Xms, Yms, XM, YM, Wrange, Hrange

W = 800: H = 600
S = _NewImage(W, H, 32)
_MouseMove 1, 1
Wrange = 1 'W - 1
Hrange = 1 'H - 1

Do
    Xms = _MouseInput
    Xms = _MouseX
    Yms = _MouseY
    XM = _MouseMovementX
    YM = _MouseMovementY
    Wrange = Wrange + XM ' -XM
    Hrange = Hrange + YM ' -YM
    Print Xms; " "; Yms, XM; " "; YM, Wrange; " "; Hrange
    '    _Limit 200 ' _delay 0.001
Loop Until InKey$ <> ""
End
the issue is clear running the snippet
moving to left and to right the mouse cursor we calculate its position on the global screen... but after some attempts you can see that the position is quite different at any attempt.
This can be possible only if _MOUSEMOVEMENTX/Y  check position of mouse each tick bigger than that used by _mouseinput.
what do you think about this?


RE: Detect when mouse leaves program window - grymmjack - 09-10-2023

Try putting `WHILE _MOUSEINPUT`: ... `WEND`

Inside inner loop, have tight mouse loop.


RE: Detect when mouse leaves program window - TempodiBasic - 09-10-2023

(09-10-2023, 12:28 AM)grymmjack Wrote: Try putting `WHILE _MOUSEINPUT`: ... `WEND`

Inside inner loop, have tight mouse loop.

Hi grymmjack, thank for suggestion.
I have done different attempts but they haven't solved the issue.

Code: (Select All)
Rem the goal: we wanto to detect if mouse is out o in window's area

Rem the idea is to evaluate the position of the mouse at starting of the run of the application
Rem if QB64 does not set mouse in an that area at run time, we can set position of mouse with related functions

Option _Explicit

Dim S As Long
Dim As Integer W, H, Xms, Yms, XM, YM, Wrange, Hrange

W = 800: H = 600
S = _NewImage(W, H, 32)
_MouseMove 1, 1
Wrange = 1 'W - 1
Hrange = 1 'H - 1

Do
    '  If _MouseInput Then
    While _MouseInput
        Xms = _MouseX
        Yms = _MouseY
        XM = _MouseMovementX
        YM = _MouseMovementY
        Wrange = Wrange + XM
        Hrange = Hrange + YM
        Print Xms; " "; Yms, XM; " "; YM, Wrange; " "; Hrange
        If (Wrange < 0) Then Print "Mouse out on the left"
        If (Wrange > W) Then Print "mouse out on the right"
        If (Hrange < 0) Then Print "mouse out on the top"
        If (Hrange > H) Then Print " mouse out on the bottom"
    Wend
    ''' a trick: if mouse is on the border and XM is not 0 it goes out of window
    ''If (Xms = 1 And XM) Then Print "Mouse out on the left"
    ''If (Xms = 80 And XM) Then Print "mouse out on the right"
    ''If (Yms = 1 And YM) Then Print "mouse out on the top"
    ''If (Yms = 25 And YM) Then Print " mouse out on the bottom"
    '   End If
    '    _Limit 200 ' _delay 0.001
Loop Until InKey$ <> ""
End
I have tried to use an IF _MOUSEINPUT THEN but the rsult is the same
I have evaluated different ways to calculate and adjourn the position o f mouse with the saame results.
I have used also your suggeston but I arrived at the the same point.

I'm going to do a study code of _MousemovementX & _mousemovemeentY.


RE: Detect when mouse leaves program window - TempodiBasic - 09-26-2023

here the link to my pure QB64 solution.
It works but it must be improved a little.

detecting mouse leaving window's area.


RE: Detect when mouse leaves program window - SMcNeill - 09-27-2023

Here's my solution for this (for Windows, at least): https://qb64phoenix.com/forum/showthread.php?tid=2041