Author Topic: Locking the mouse to a program window  (Read 4431 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Locking the mouse to a program window
« on: September 03, 2018, 12:32:20 pm »
Is there a way within QB64 to lock the mouse to a program's window? so if a user moves the mouse to the extreme side of the window it won't automatically leave the window, so they could click on something near the edge without having to be worried about clicking off onto the desktop.
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: Locking the mouse to a program window
« Reply #1 on: September 03, 2018, 12:39:18 pm »
_FULLSCREEN.

Anything other than that sounds like a bad idea in today's operating systems.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Locking the mouse to a program window
« Reply #2 on: September 03, 2018, 12:44:38 pm »
I've did this with DECLARE LIBRARY and the Windows API before.  Check the Windows Libraries in the wiki for a demo.


Edit:  I think this may be what you're looking for: http://qb64.org/wiki/Windows_Libraries#Mouse_Area


Code: QB64: [Select]
  1.  
  2. TYPE Rectangle
  3.   left AS LONG
  4.   top AS LONG
  5.   right AS LONG
  6.   bottom AS LONG
  7. DIM Rec AS Rectangle
  8.  
  9.  
  10.   FUNCTION ClipCursor%% (Rect AS Rectangle) 'sets mouse box work area on desktop
  11.   SUB SetCursorPos (BYVAL x AS LONG, BYVAL y AS LONG) 'move cursor position
  12.  
  13. SCREEN _NEWIMAGE(320, 200, 32)
  14. SetCursorPos 40, 36  'move cursor to left side of desktop
  15.  
  16. PRINT "Press a key and the mouse is boxed in!"
  17. K$ = INPUT$(1)
  18.  
  19. Rec.left = 600
  20. Rec.top = 400
  21. Rec.bottom = 700
  22. Rec.right = 800
  23. work%% = ClipCursor(Rec)
  24.  
  25. PRINT work%%
  26. PRINT "Click the mouse and window to quit!"
  27.  
  28. SetCursorPos 40, 36 'attempts to move mouse to left
  29.  
  30. SYSTEM  [\code]
« Last Edit: September 03, 2018, 12:46:48 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Locking the mouse to a program window
« Reply #3 on: September 03, 2018, 03:30:39 pm »
SMcNeill,
yep that will work.
Whats not good is I can't use SCREENX or SCREENY with anything but windows limiting the game to that OS. So how to know where the window is at anyone time to set the limits to it becomes an issue.  I guess I could force the window to start in one location but I'd have to find a way to make it unmovable.
For as much of a pain Windows is anymore to use it allows a lot more flexibility in programing then Mac and Linux most of the time.

FellippeHeitor,
Full screen would be preferable but not everybody wants to run things in full screen.(as for myself, I prefer full screen modes)
Though I probably need a way to detect if someone has made the game full screen and turn off the mouse lock. Not sure if the FULLSCREEN function will give me what I'm looking for in that regard.
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: Locking the mouse to a program window
« Reply #4 on: September 03, 2018, 03:38:36 pm »
FellippeHeitor,
Full screen would be preferable but not everybody wants to run things in full screen.(as for myself, I prefer full screen modes)
Though I probably need a way to detect if someone has made the game full screen and turn off the mouse lock. Not sure if the FULLSCREEN function will give me what I'm looking for in that regard.

I'm certain those unwilling to have their screen taken up unwantedly by a surprise fullscreen program would also be unwilling to have their mouse trapped in a window; Maybe offer the choice to switch to full screen and design your interface so it keeps a safe distance from the window's edges.

And yes: the _FULLSCREEN function will tell you if the user switched to full screen; The _SMOOTH function in the latest dev build will tell you if the user chose an antialiased mode.

If you have the latest dev build, you can also prevent ALT+ENTER from entering full screen by using _ALLOWFULLSCREEN OFF.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Locking the mouse to a program window
« Reply #5 on: September 03, 2018, 04:03:50 pm »
FellippeHeitor,
Full screen would be preferable but not everybody wants to run things in full screen.(as for myself, I prefer full screen modes)
Though I probably need a way to detect if someone has made the game full screen and turn off the mouse lock. Not sure if the FULLSCREEN function will give me what I'm looking for in that regard.

I'm certain those unwilling to have their screen taken up unwantedly by a surprise fullscreen program would also be unwilling to have their mouse trapped in a window; Maybe offer the choice to switch to full screen and design your interface so it keeps a safe distance from the window's edges.

I agree with this 100%.  I really don't want programs which have to run FULLSCREEN, but I'd much prefer them to a program which traps my mouse.  Just because you CAN trap the mouse, doesn't mean you SHOULD.

A game that mouse locked my screen is a game I'd put in the "novelty" category; I'd try it out once, and then delete it, as it doesn't play nice with the rest of my stuff I have open and running all the time.

I think a much more elegant solution would be to wrap a border around your screen to prevent accidental clicks out-of-game.

For example:   Instead of a SCREEN _NEWIMAGE (800,600,32), make the game SCREEN _NEWIMAGE (820, 620, 32), wrap a 10-pixel thick design of roses/swords/hearts/whatever around the edges to give a decorative highlight, and then map the game from (10,10)-(810,610)...

You'd get the same playable area, some nice custom borders for decoration, and would prevent/reduce accidental off-screen clicks, all while playing nice with anything else the user has in the background.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Locking the mouse to a program window
« Reply #6 on: September 03, 2018, 04:17:49 pm »
You guys must not play many games, Minecraft, Might and Magic, anything in dosbox, Unreal, Unreal Tournament, StarCraft and a slew of others all capture the mouse to the play area in windowed mode.
 I kind of like the border idea, BUT in the game moving the mouse to the edges of the screen is one way of scrolling the map, which is the issue with locking the mouse, other wise I think a border could be cool for Warcraft, could use some of the units marching around it.
 Not to mention in a battle situation you may need to be rapidly moving the mouse around the screen controlling various units and it would be crazy easy to go off window and that could loose you the game.
Granted after becoming radioactive I only have a half-life!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Locking the mouse to a program window
« Reply #7 on: September 03, 2018, 04:59:29 pm »
It's been ages since I last played Starcraft, but from what I remember, it didn't trap the mouse while in windowed mode.  In fact, IIRC, there was even a script which folks developed to manually trap the mouse.

*Runs off and digs around in old folders for a bit*

Code: QB64: [Select]
  1. ;
  2. ;       Author:         Bkid <Bkid@bemaniso.ws>
  3. ;       Thanks to corrupt for the original mouse locking code.
  4. ;
  5. ;       This script finds the size of the SC2 window, its edges, and locks
  6. ;       the mouse into the window. This way, you can use the mouse scroll,
  7. ;       even if you're windowed. You can lock the mouse by turning CapsLock on,
  8. ;       and unlock it by turning CapsLock off.
  9.  
  10. #SingleInstance Force
  11. #Persistent
  12. Menu, Tray, NoStandard
  13. Menu, Tray, Add, Credits
  14. Menu, Tray, Add, Exit
  15. TrayTip, Mouse lock v1.0 for SC2, CapsLock on = Lock the mouse to the StarCraft 2 window.`nCapsLock off = Release the mouse., 5, 1
  16. SetCapsLockState, Off
  17. SetTimer, LockCheck, 5
  18.  
  19. LockCheck:
  20. GetKeyState, CapsState, CapsLock, T
  21. If CapsState = D
  22. {
  23. Lock("StarCraft II")
  24. Suspend, Off
  25. WinActivate, StarCraft II
  26. } else {
  27. Suspend, On
  28. }
  29.  
  30. Lock(llwindowname="")
  31. {
  32.   VarSetCapacity(llrectA, 16)
  33.   WinGetPos, llX, llY, llWidth, llHeight, %llwindowname%
  34.   If (!llWidth AND !llHeight) {
  35.     DllCall("ClipCursor")
  36.     Return, False
  37.   }
  38.   Loop, 4 {
  39.     DllCall("RtlFillMemory", UInt,&llrectA+0+A_Index-1, UInt,1, UChar,(llX + 4) >> 8*A_Index-8)
  40.     DllCall("RtlFillMemory", UInt,&llrectA+4+A_Index-1, UInt,1, UChar,(llY + 30) >> 8*A_Index-8)
  41.     DllCall("RtlFillMemory", UInt,&llrectA+8+A_Index-1, UInt,1, UChar,(llWidth + llX - 4)>> 8*A_Index-8)
  42.     DllCall("RtlFillMemory", UInt,&llrectA+12+A_Index-1, UInt,1, UChar,(llHeight + llY - 4) >> 8*A_Index-8)
  43.   }
  44.   DllCall("ClipCursor", "UInt", &llrectA)
  45. Return, True
  46. }
  47.  
  48. Credits:
  49. MsgBox, 262144,, Mouse lock v1.0 for StarCraft II by Bkid (Bkid@bemaniso.ws).`n`nThanks to corrupt on the AutoHotKey forums for the`ninitial mouse locking code.
  50.  
  51. exitapp

But, you're right; I don't play the games you mentioned a lot.  Maybe I'd play them more if they'd player nicer with everything else I have running 24/7 in the background.  ;D
« Last Edit: September 03, 2018, 05:02:47 pm by odin »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Locking the mouse to a program window
« Reply #8 on: September 03, 2018, 05:46:13 pm »
Hi Fellippe
you are right there is no way to lock mouse on an Inform Form...
I failed to get this result
1. both using mouse cohordinates in MouseLeave event that I have learned that is triggered only if mouse goes from form_window surface into/onto any item_component surface but not if mouse is out of edges of the form

2. both using mouse cohordinates in MouseEnter event that is triggered when mouse is on the surface of th form_window. More I have learned that _mouseX and _mouseY give back cohordinates relative to form_window surface and not to the whole screen of PC.
Here if I use a inner limit of mouse cohordinates to set mouse in the borders this not works also if event is triggered!
Why this trick of a inner border like a frame of a painting doesn't work?

I attach my badcode
Thanks to read

PS Are there some QB64 keywords to know cohordinates of mouse relative at screen of PC?
Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: Locking the mouse to a program window
« Reply #9 on: September 03, 2018, 06:01:06 pm »
Hi Fellippe
you are right there is no way to lock mouse on an Inform Form...
I failed to get this result
1. both using mouse cohordinates in MouseLeave event that I have learned that is triggered only if mouse goes from form_window surface into/onto any item_component surface but not if mouse is out of edges of the form

2. both using mouse cohordinates in MouseEnter event that is triggered when mouse is on the surface of th form_window. More I have learned that _mouseX and _mouseY give back cohordinates relative to form_window surface and not to the whole screen of PC.
Here if I use a inner limit of mouse cohordinates to set mouse in the borders this not works also if event is triggered!
Why this trick of a inner border like a frame of a painting doesn't work?

I attach my badcode
Thanks to read

PS Are there some QB64 keywords to know cohordinates of mouse relative at screen of PC?

Not the point of this thread, Tempodi. Please proceed to this thread: https://www.qb64.org/forum/index.php?topic=528.msg3811#msg3811
« Last Edit: September 03, 2018, 06:47:43 pm by FellippeHeitor »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Locking the mouse to a program window
« Reply #10 on: September 03, 2018, 06:06:39 pm »
PS Are there some QB64 keywords to know cohordinates of mouse relative at screen of PC?

**********************

(_MOUSEX + _SCREENX, _MOUSEY + _SCREENY)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Locking the mouse to a program window
« Reply #11 on: September 03, 2018, 06:50:15 pm »
Sorry Fellippe
In the last times I see anywhere InForm applications....
In the while I have tried another code using _mousemovementX and _mousemovementY but this attempt is worst of the previous ...
 so I come back to the point of the thread without InForm and this is one solution using a usually windows made with screen _newimage and the above keywords!
It work well.

Code: QB64: [Select]
  1. ' attempt to lock a mouse to a window
  2. DIM SHARED AbsMouseX AS INTEGER, AbsMouseY AS INTEGER
  3. SCREEN _NEWIMAGE(400, 400, 32)
  4. LOCATE 1, 1: PRINT "press ESC to Exit"
  5. AbsMouseX = 5: AbsMouseY = 5
  6.     DO
  7.         IF _KEYHIT = 27 THEN END
  8.     AbsMouseX = AbsMouseX + _MOUSEMOVEMENTX
  9.     AbsMouseY = AbsMouseY + _MOUSEMOVEMENTY
  10.     IF AbsMouseX < 0 THEN
  11.         AbsMouseX = 1
  12.     ELSEIF AbsMouseX > 400 - 1 THEN
  13.         AbsMouseX = 400 - 1
  14.     END IF
  15.     IF AbsMouseY < 0 THEN
  16.         AbsMouseY = 1
  17.     ELSEIF AbsMouseY > 400 - 1 THEN
  18.         AbsMouseY = 400 - 1
  19.     END IF
  20.     _MOUSEMOVE AbsMouseX, AbsMouseY
  21.     LOCATE 10, 1: PRINT STR$(AbsMouseX) + " " + STR$(AbsMouseY) + "     "
  22.  
  23.  

but it seems working well also using only _mouseX and _mouseY
Code: QB64: [Select]
  1. ' attempt to lock a mouse to a window
  2. DIM SHARED AbsMouseX AS INTEGER, AbsMouseY AS INTEGER
  3. SCREEN _NEWIMAGE(400, 400, 32)
  4. LOCATE 1, 1: PRINT "press ESC to Exit"
  5. AbsMouseX = 5: AbsMouseY = 5
  6.     DO
  7.         IF _KEYHIT = 27 THEN END
  8.     AbsMouseX = _MOUSEX
  9.     AbsMouseY = _MOUSEY
  10.     IF AbsMouseX < 0 THEN
  11.         AbsMouseX = 1
  12.     ELSEIF AbsMouseX > 400 - 1 THEN
  13.         AbsMouseX = 400 - 1
  14.     END IF
  15.     IF AbsMouseY < 0 THEN
  16.         AbsMouseY = 1
  17.     ELSEIF AbsMouseY > 400 - 1 THEN
  18.         AbsMouseY = 400 - 1
  19.     END IF
  20.     _MOUSEMOVE AbsMouseX, AbsMouseY
  21.     LOCATE 10, 1: PRINT STR$(AbsMouseX) + " " + STR$(AbsMouseY) + "     "
  22.  
  23.  
  24.  

In the two example title bar of window of program is off, so if you want closing the window you must give an option to the user or he must use SO hotkeys like Alt+F4 and Ctrl+Alt+Canc for Windows.
I thint that this can be useful for Cobalt
Programming isn't difficult, only it's  consuming time and coffee