Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect mouse leaving application's window in pure QB64
#1
Hi Terry
Hi Steve
Hi all folks

I need an help to be working well to 100% this my QB64 snippet that detects when mouse leaves the area of the application.

Thanks to Steve, Bplus, GrymmJack and all you QB64 coders with your feedbacks and suggestions!

Code: (Select All)
Rem it is a code test to detect mouse out of window of application using only QB64 code
Option _Explicit

Dim As Long S1, W1, H1
Dim As Integer Xm, Ym, Dxm, Dym, Im, Ox, Oy
W1 = 800: H1 = 600
S1 = _NewImage(W1, H1, 32)

Screen S1


Do
    Im = _MouseInput
    If Im Then
        Dxm = _MouseMovementX
        Dym = _MouseMovementY
        If Dxm Then Xm = IsOutWindow%(0, _Width - 1, Dxm, _MouseX, Ox)
        If Dym Then Ym = IsOutWindow%(0, _Height - 1, Dym, _MouseY, Oy)

        Select Case Xm
            Case 0:
                If Ym = 0 Then Print " mouse in window"
            Case 1:
                Print "mouse out on the right of window"
            Case -1:
                Print "mouse out on the left of window"
            Case Else
        End Select

        Select Case Ym
            Case 1:
                Print "mouse out on the bottom of window"
            Case -1:
                Print " mouse out on the top of window"
            Case Else
        End Select
        Ox = Xm
        Oy = Ym
    End If
    _Limit 300

Loop Until InKey$ <> ""
End

Function IsOutWindow% (Min As Integer, Max As Integer, DeltaMove As Integer, MousePos As Integer, OldMode As Integer)

    If MousePos > Min And MousePos < Max Then
        ' is it in the range of the window?
        IsOutWindow% = 0 ': Exit Function
    ElseIf DeltaMove > 0 And MousePos = Max Then
        ' is it moving towards right and it is on the right edge?
        IsOutWindow% = 1 ': Exit Function
    ElseIf DeltaMove < 0 And MousePos = Min Then
        ' is it moving towards left and it is on the left edge?
        IsOutWindow% = -1 ': Exit Function
    ElseIf MousePos = Max And DeltaMove < 0 And OldMode = 1 Then
        ' is it on the right edge and it is moving to left and it was out on the right?
        IsOutWindow% = 1 ': Exit Function
    ElseIf MousePos = Min And DeltaMove > 0 And OldMode = -1 Then
        ' is it on the left edge and it is moving on the right and it was out on the left?
        IsOutWindow% = -1 ': Exit Function
    End If
End Function
Generally it works ok, but it sometimes lasts stuck and you must repeat the action to get the detection of mouse out of window.
How can it be improved?

Thanks for feedbacks
Reply
#2
Here's my solution for this (for Windows, at least): https://qb64phoenix.com/forum/showthread.php?tid=2041
Reply
#3
Isn't the lack of mouseinput the easiest way to tell the mouse isn't in the window?  In the image linked here there are 3 runs of the same little scribble application overlapping. The pixels only get drawn when the mouse is in the top overlapping window.  I'm just a little confused on the reason to track the lack of mouse  input in the window when ... none gets reported if the mouse isn't in the window. Am I missing something?


[Image: image.png]
Reply
#4
That is interesting seeing 3 exe's running.

Is there code to detect other exe's running, get them talking to each other... Keypress responds to correct window chosen by mouse?

You could have a Color Picker program up with a Paint One. You could have a file selector up with something that needs file names.
b = b + ...
Reply
#5
(09-27-2023, 02:37 PM)bplus Wrote: That is interesting seeing 3 exe's running.

Is there code to detect other exe's running, get them talking to each other... Keypress responds to correct window chosen by mouse?

You could have a Color Picker program up with a Paint One. You could have a file selector up with something that needs file names.

I posted something like that last year.
Reply
#6
(09-27-2023, 02:39 PM)James D Jarvis Wrote:
(09-27-2023, 02:37 PM)bplus Wrote: That is interesting seeing 3 exe's running.

Is there code to detect other exe's running, get them talking to each other... Keypress responds to correct window chosen by mouse?

You could have a Color Picker program up with a Paint One. You could have a file selector up with something that needs file names.

I posted something like that last year.

Come to think, I posted both of those also as independents for Clip Board values.
b = b + ...
Reply
#7
(09-27-2023, 02:42 PM)bplus Wrote:
(09-27-2023, 02:39 PM)James D Jarvis Wrote:
(09-27-2023, 02:37 PM)bplus Wrote: That is interesting seeing 3 exe's running.

Is there code to detect other exe's running, get them talking to each other... Keypress responds to correct window chosen by mouse?

You could have a Color Picker program up with a Paint One. You could have a file selector up with something that needs file names.

I posted something like that last year.

Come to think, I posted both of those also as independents for Clip Board values.
 here's one I did:   ClipScribble (qb64phoenix.com)
Reply
#8
(09-27-2023, 01:53 PM)James D Jarvis Wrote: Isn't the lack of mouseinput the easiest way to tell the mouse isn't in the window?  In the image linked here there are 3 runs of the same little scribble application overlapping. The pixels only get drawn when the mouse is in the top overlapping window.  I'm just a little confused on the reason to track the lack of mouse  input in the window when ... none gets reported if the mouse isn't in the window. Am I missing something?


[Image: image.png]

Hi James D Jarvis
thanks for your feedback!

about 
Quote:Isn't the lack of mouseinput the easiest way to tell the mouse isn't in the window?
I think it is not so simple. Here in the bottom I post an image of  3 different apps (run) of the same program that I have posted here as a program that detects if mouse pointer is out or in the area of the window.
As you can see, all three applications in running show a correct result about the position of the pointer of the mouse. So it seems that mouse information are shared among them.

[Image: image.png]

Moreover if you run this code you can see that also when mouse is out of window of the program after a call to _MouseInput you get back mouse informations like _MouseX and _MouseY, _MouseMovementX and _MouseMovementY.
run this code to verify
Code: (Select All)
Rem it is a code test to detect mouse out of window of application
Option _Explicit

Dim As Long S1, W1, H1
Dim As Integer Xm, Ym, Dxm, Dym, Im, Ox, Oy
W1 = 800: H1 = 600
S1 = _NewImage(W1, H1, 32)

Screen S1

Print "  Dxm             Dym           Xm         Ym"
View Print 2 To 30
Do
    Im = _MouseInput ' here we call a mouseinput queque
    If Im Then ' if mouseinput is true
        Dxm = _MouseMovementX ' we store mousemovementX
        Dym = _MouseMovementY ' we store mousemovementY
        Xm = _MouseX
        Ym = _MouseY
        Print Dxm, Dym, Xm, Ym
    End If
    _Limit 300

Loop Until InKey$ <> ""
End
Now move the mouse out of the window and continue moving mouse ... you'll get back MouseInput informations!

Here a screenshot

[Image: image.png]


I dunno if it is right to get back info about mouse when it leaves the area of window's application. 
I think that it is the actual behaviour of the OS (MS Window). But Someoneelse can test the code in Linux or Mac platforms.
Waiting more feedbacks
Reply
#9
When I run that example program I noticed it does track mousemovement on my windows machine but does not report the mouseX or mouseY unless it's in the program window. I also noted (by adding the command) it only reports _mousebutton() clicks  within the respective program window when the mouse is in them.
Reply
#10
@James D Jarvis

yes if you track _mousebutton() it can give the input only at the position where mouse is . So it gives input only for window pointed by mouse cursor.

MouseX and MouseY give the exact position of the mouse in the window that is monitoring the mouse, but if the pointer of the mouse is out o f the window they return the last position on that window.

MousemovementX and MousemovementY return the relative movement of mouse along X and Y axises.
Reply




Users browsing this thread: 8 Guest(s)