QB64.org Forum

Active Forums => Programs => Topic started by: TempodiBasic on April 25, 2021, 04:58:53 pm

Title: How to move mouse cursor out of your window's application with QB64 keywords
Post by: TempodiBasic on April 25, 2021, 04:58:53 pm
Hi guys
talking with Ashish about how to move the mouse cursor , by code, out of window of our application
I have tried this workaround

Code: QB64: [Select]
  1. Screen _NewImage(300, 300, 32)
  2. waitScreen
  3. winX = _DesktopWidth / 2
  4. winY = _DesktopHeight / 2
  5. _ScreenMove winX, winY
  6. DMouseX = 100
  7. DmouseY = 100
  8.  
  9.     k$ = InKey$
  10.     If k$ = " " Then
  11.         MoveDesktopMouse DMouseX, DmouseY
  12.         Print " we move mouse pointer at "; DMouseX; " "; DmouseY
  13.         If DMouseX < _DesktopWidth - 100 Then DMouseX = DMouseX + 100 Else DMouseX = 100
  14.         If DmouseY < _DesktopHeight - 100 Then DmouseY = DmouseY + 100 Else DmouseY = 100
  15.     End If
  16.     _Limit 20
  17. Loop Until k$ = "m"
  18.  
  19. Sub MoveDesktopMouse (x As Integer, y As Integer)
  20.     _MouseMove Int((x * 300) / _DesktopWidth), Int((y * 300) / _DesktopHeight)
  21.     waitScreen
  22.  
  23. Sub waitScreen
  24.  

Well the code works but in the developing of application I have fallen into a strange issue (or bug?) about _FULLSCREEN.
Give a try to the code to see what happens.
( the code that stresses the issue/bug is on the discussion part of the forum)

while here at bottom I post the version of code using user32.dll that works very well but we must use a windows API call
Code: QB64: [Select]
  1.     Function SetCursorPos (ByVal x As Long, Byval y As Long)
  2.  
  3. Screen _NewImage(300, 300, 32)
  4. waitScreen
  5. winX = _DesktopWidth / 2
  6. winY = _DesktopHeight / 2
  7. _ScreenMove winX, winY
  8. DMouseX = 100
  9. DmouseY = 100
  10.  
  11.     k$ = InKey$
  12.     If k$ = " " Then
  13.         MoveDesktopMouse DMouseX, DmouseY
  14.         Print " we move mouse pointer at "; DMouseX; " "; DmouseY
  15.         If DMouseX < _DesktopWidth - 100 Then DMouseX = DMouseX + 100 Else DMouseX = 100
  16.         If DmouseY < _DesktopHeight - 100 Then DmouseY = DmouseY + 100 Else DmouseY = 100
  17.     End If
  18.     _Limit 20
  19. Loop Until k$ = "m"
  20.  
  21. Sub MoveDesktopMouse (x As Integer, y As Integer)
  22.     Print SetCursorPos(x, y)
  23.  
  24. Sub waitScreen
  25.  
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: AtomicSlaughter on April 27, 2021, 08:49:39 am
How about using _MOUSEHIDE and _MOUSESHOW and just hide it when you need to.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: TempodiBasic on April 27, 2021, 05:48:01 pm
Hi AtomicSlaughter
I can find interesting your proposal if you show how to get the movement out of the window of the mouse pointer. 
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SMcNeill on April 27, 2021, 07:16:01 pm
Here’s a question: can you mousemove to a non-visible screen?  I’m back down watching mom for the next ten days, so I’m not where I can test it, but something like:


Sub MoveDesktopMouse (x As Integer, y As Integer)
    STATIC tempscreen
    IF tempscreen = 0 THEN
        tempscreen = _NEWIMAGE(_DESKTOPWIDTH, _DESKTOPHEIGHT, 32)
        D = _DISPLAY
        _SCREENHIDE
        SCREEN tempscreen
        _SCREENMOVE (0,0)
        SCREEN D
        _SCREENSHOW
    END IF
    D = _DEST: S = _SOURCE
    _DEST tempscreen: _SOURCE tempscreen
    _MouseMove Int((x * 300) / _DesktopWidth), Int((y * 300) / _DesktopHeight)
    waitScreen
    _DEST D: _SOURCE S
End Sub

The idea would be to have a hidden screen the size of your desktop, set it as dest and source, and see if mousemove would work relative to that screen’s dimensions.


If it works, you’ll want to use this routine here for absolute screen positioning: https://www.qb64.org/forum/index.php?topic=3659.0
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: AtomicSlaughter on April 28, 2021, 09:06:37 am
from what ive noticed the mouse pointer is visable as soon as you move it out of the app window and dissapears again when you move it back in
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: TempodiBasic on April 28, 2021, 09:50:32 am
@SMcNeill
hi Steve , your idea of workaround is very fine that I have robbed 10 minutes to try it now...
but with this code the mouse has been moved into the area of the window of application
Code: QB64: [Select]
  1. Screen _NewImage(500, 400, 32)
  2.     k$ = InKey$
  3.     If k$ = " " Then MoveDesktopMouse Int(Rnd * (_DesktopWidth / 2) + 100), Int(Rnd * (_DesktopHeight / 2) + 100)
  4.     Locate 24, 1: Print " press space to move mouse on screen, m to quit";
  5. Loop Until k$ = "m"
  6.  
  7.  
  8. Sub MoveDesktopMouse (x As Integer, y As Integer)
  9.     Static tempscreen
  10.     If tempscreen = 0 Then
  11.         tempscreen = _NewImage(_DesktopWidth, _DesktopHeight, 32)
  12.         D = _Display
  13.         _ScreenHide
  14.         Screen tempscreen
  15.         _ScreenMove 0, 0
  16.         Screen D
  17.         _ScreenShow
  18.     End If
  19.     D = _Dest: S = _Source
  20.     _Dest tempscreen: _Source tempscreen
  21.     _MouseMove Int((x * 300) / _DesktopWidth), Int((y * 300) / _DesktopHeight)
  22.     '    waitscreen
  23.     _Dest D: _Source S
  24.  
  25. Sub waitscreen
  26.     Loop

but if I have time tonight I'll try in another way following your idea

@AtomicSlaughter
I think that there is a bad communication from me. The goal is to move the pointer of mouse out of the window of our program.
It doesn't matter if it is visible or no.  In the first my workaround I have used the tip to fullscreen window of application, calculate the position of the point out of our window, mousemove there, take back to original size the window of our application... and the hide/show window has the  goal to be not visible this trick.
In this attempt I fall in the _FULLSCREEN _OFF issue that works alternatively.

The second code demo that I have posted at beginning of the thread solve the issue not using a workaround but the API window that move the mouse at a point of the screen.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SMcNeill on April 28, 2021, 09:59:58 am
If setting DEST and SOURCE doesn’t work, you might try to use SCREEN to change the active screen, without changing the visible screen.

http://qb64.org/wiki/SCREEN (Look at the 3rd and 4th parameters which aren’t used very often anymore, but which might do what you’re looking for here.)
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: bplus on April 28, 2021, 04:00:38 pm
Sounds like a job for that API guy. ;)

There must be an API for mouse functions.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SMcNeill on April 28, 2021, 04:14:30 pm
Sounds like a job for that API guy. ;)

There must be an API for mouse functions.

Here is.  Just look at the 2nd demo of the first post.  Tempodi is just trying to do it with QB64 only.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: bplus on April 28, 2021, 04:24:24 pm
@SMcNeill

in post of yest 7:16 PM code

what is wait screen supposed to be?

Code: QB64: [Select]
  1.     Cls
  2.     MoveDesktopMouse mx, my
  3.     Print mx, my
  4.  
  5. Sub MoveDesktopMouse (x As Integer, y As Integer)
  6.     Static tempscreen
  7.     If tempscreen = 0 Then
  8.         tempscreen = _NewImage(_DesktopWidth, _DesktopHeight, 32)
  9.         D = _Display
  10.         _ScreenHide
  11.         Screen tempscreen
  12.         _ScreenMove 0, 0
  13.         Screen D
  14.         _ScreenShow
  15.     End If
  16.     D = _Dest: S = _Source
  17.     _Dest tempscreen: _Source tempscreen
  18.     _MouseMove Int((x * 300) / _DesktopWidth), Int((y * 300) / _DesktopHeight)
  19.     'waitScreen
  20.     _Dest D: _Source S
  21.  

Getting Invalid handle error for Screen D line.

Wait... want to move the mouse outside QB64 screen? Not just read it when it's out?!? Yikes!
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: bplus on April 29, 2021, 10:41:24 am
Testing Tempodi's test with my own main loop st
Code: QB64: [Select]
  1. Screen _NewImage(500, 400, 32)
  2. _Delay .25
  3.     k$ = InKey$
  4.     If k$ = " " Then MoveDesktopMouse 10, 10
  5.     Locate 24, 1: Print " press space to move mouse on screen, m to quit";
  6. Loop Until k$ = "m"
  7.  
  8.  
  9. Sub MoveDesktopMouse (x As Integer, y As Integer)
  10.     Static tempscreen
  11.     If tempscreen = 0 Then
  12.         tempscreen = _NewImage(_DesktopWidth, _DesktopHeight, 32)
  13.         D = _Display
  14.         _ScreenHide
  15.         Screen tempscreen
  16.         _ScreenMove 0, 0
  17.         Screen D
  18.         _ScreenShow
  19.     End If
  20.     D = _Dest: S = _Source
  21.     _Dest tempscreen: _Source tempscreen
  22.     _MouseMove Int((x * 300) / _DesktopWidth), Int((y * 300) / _DesktopHeight)
  23.     '    waitscreen
  24.     _Dest D: _Source S
  25.  
  26. Sub waitscreen
  27.     Loop
  28.  
  29.  

The first time I press space, the QB64 screen moves to 0,0 then when I drag it back in middle and press space the mouse curser moves to 0,0 inside QB64 screen not outside of it, so I'd say quit wasting time and call up that API guy if you really need to move mouse outside a QB64 screen (and can't do it yourself?!?)
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SMcNeill on April 29, 2021, 11:25:25 am
The API code was included at the bottom of the first post, as the second example:

Code: [Select]
Declare Dynamic Library "user32"
    Function SetCursorPos (ByVal x As Long, Byval y As Long)
End Declare
 
Screen _NewImage(300, 300, 32)
waitScreen
winX = _DesktopWidth / 2
winY = _DesktopHeight / 2
_ScreenMove winX, winY
DMouseX = 100
DmouseY = 100
 
Do
    k$ = InKey$
    If k$ = " " Then
        MoveDesktopMouse DMouseX, DmouseY
        Print " we move mouse pointer at "; DMouseX; " "; DmouseY
        If DMouseX < _DesktopWidth - 100 Then DMouseX = DMouseX + 100 Else DMouseX = 100
        If DmouseY < _DesktopHeight - 100 Then DmouseY = DmouseY + 100 Else DmouseY = 100
    End If
    _Limit 20
Loop Until k$ = "m"
End
 
Sub MoveDesktopMouse (x As Integer, y As Integer)
    Print SetCursorPos(x, y)
End Sub
 
Sub waitScreen
    Do While Not _ScreenExists: Loop
End Sub
 

Sometimes it's just fun to try and think outside the box and try to find a method to use normal commands in abnormal ways -- which is what Tempodi is playing around here with.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SpriggsySpriggs on April 29, 2021, 11:33:08 am
I prefer using the Win32 API SetCursorPos function because it definitely does move the cursor exactly where you want it and doesn't require anything more than:

Code: QB64: [Select]
  1.     Function SetCursorPos& (ByVal x As Long, Byval y As Long)
  2.  
  3. Dim As Long move
  4. move = SetCursorPos(200, 400) 'or whichever coordinate you want
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SpriggsySpriggs on April 29, 2021, 11:34:39 am
One could also even use _ScreenClick as a way to move the mouse wherever you want as it does move the mouse in order to click
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: bplus on April 29, 2021, 11:47:02 am
One could also even use _ScreenClick as a way to move the mouse wherever you want as it does move the mouse in order to click

Well that looks good!
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: TempodiBasic on April 30, 2021, 06:45:21 pm
Hi guys and gals
Thanks for your contribute to the  thread.

@SMcNeill
Hi Stevw. I have tryed to  use only  _Dest  and _Source  without change the acreen ... and it doesnt  work because _Mousemove acts on active screen

So I have tried using Screen  in order to  change window dimensions  and then _mousemove. But each time you call Screen it resets the output and mouse moves itself in the window.

I'll get a try to use the pageactive and the pageshowed  parametwrs of Screen.

@SpriggsySpriggs
Fine suggestion _mouseclick  I  know it mow.
And I'll try it.
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: SMcNeill on May 01, 2021, 09:56:42 am
I finally had a change to head home for just a few minutes today, so here's my take for this type of problem:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(500, 400, 32)
  2.     k$ = INKEY$
  3.     IF k$ = " " THEN MoveDesktopMouse RND * _DESKTOPWIDTH, RND * _DESKTOPHEIGHT
  4.     LOCATE 24, 1: PRINT " press space to move mouse on screen, m to quit";
  5.     _LIMIT 30
  6. LOOP UNTIL k$ = "m"
  7.  
  8.  
  9. SUB MoveDesktopMouse (x AS INTEGER, y AS INTEGER)
  10.  
  11.     STATIC tempscreen
  12.     SX = _SCREENX: SY = _SCREENY
  13.     D = _DEST
  14.     IF tempscreen = 0 THEN
  15.         tempscreen = _NEWIMAGE(_DESKTOPWIDTH, _DESKTOPHEIGHT, 32)
  16.     END IF
  17.     SCREEN tempscreen
  18.     ScreenMove 0, 0
  19.     _MOUSEMOVE x, y
  20.     SCREEN D
  21.     _SCREENMOVE SX, SY
  22.  
  23.  
  24. SUB ScreenMove (x, y)
  25.     $IF BORDERDEC = UNDEFINED THEN
  26.         $LET BORDERDEC = TRUE
  27.         DECLARE LIBRARY
  28.             FUNCTION glutGet& (BYVAL what&)
  29.         END DECLARE
  30.     $END IF
  31.     BorderWidth = glutGet(506)
  32.     TitleBarHeight = glutGet(507)
  33.     _SCREENMOVE x - BorderWidth, y - BorderWidth - TitleBarHeight
  34.  
  35.  
  36.  

I'm using just QB64 commands to move our mouse to random spots outside our window.  The screen DOES have a slight flicker to it when this happens, but I consider this a FEATURE rather than a BUG, as I'd consider it to be a way to notify the end user of such an odd action.  It's not every day where you want to toss your cursor to someplace random across the screen after all!

;)
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: bplus on May 01, 2021, 10:07:22 am
@SMcNeill Works for me, don't see ever needing it but fun little challenge. :)
Title: Re: How to move mouse cursor out of your window's application with QB64 keywords
Post by: TempodiBasic on May 02, 2021, 01:26:41 pm
@SMcNeill
Yes Steve you got it. You have avoid the quicksand of _FULLSCREEN and it works with no issue.
Grat Job!