Author Topic: How to move mouse cursor out of your window's application with QB64 keywords  (Read 5102 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
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.  
Programming isn't difficult, only it's  consuming time and coffee

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
How about using _MOUSEHIDE and _MOUSESHOW and just hide it when you need to.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi AtomicSlaughter
I can find interesting your proposal if you show how to get the movement out of the window of the mouse pointer. 
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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
« Last Edit: April 27, 2021, 07:19:36 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
@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.
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Sounds like a job for that API guy. ;)

There must be an API for mouse functions.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
@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!
« Last Edit: April 28, 2021, 04:35:32 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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?!?)
« Last Edit: April 29, 2021, 10:43:53 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
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
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
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
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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!