Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Constraining the mouse within a window

Author
Message
Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 13:03
Hey guys,

I am looking for a way to constrain the mouse to the program window.

I tried checking its position using MOUSEX() and MOUSEY() and setting the mouse back to the window via POSITION MOUSE if it's outside the window, but that doesn't work. The program loses track of the mouse after it leaves the window and MOUSEX() and MOUSEY() will report the last known position of the mouse within the window, even though it's no longer there.

I need a way to detect when the mouse has left the program window, and a way to bring it back if it's gone outside.

Even better, a way to prevent the mouse from leaving the window at all would be ideal.

Can anybody help?
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 21st Oct 2010 13:35
Why not just use MOUSEMOVEX and MOUSEMOVEY to affect your own mouse position, then put a sprite there, or paste a mouse cursor. With that, you could take over mouse handling. If you hide the mouse, then the only visible mouse cursor would be your own - when you move the mouse outside the window it won't be visible, so might give the effect your after.

Health, Ammo, and bacon and eggs!
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Oct 2010 13:40
Just what Van B said. Use a sprite as the mouse and move it using mousemovex() and mousemovey(), and hide the original mouse. Then just put this in your main loop:

position mouse screen width()/2,screen height()/2

Although I'm sure it's possible to directly set a boundary for the real mouse, I just don't see how to do it... Just like in the ICCUP launcher for StarCraft, you can set the game to run in window mode, and the mouse will not leave that screen, no matter what you do.

TheComet

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 14:03
Thanks. I'll use this technique in the meantime, but I'm still looking for a way to do this the properly. I want to truly constrain the mouse to the window.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Oct 2010 14:26 Edited at: 21st Oct 2010 14:27


EDIT: Just make sure you put it at the start of each loop...

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Oct 2010 14:48
I think that's what Agent used in the beginning, although I could be wrong

TheComet

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 14:52
baxslash, thanks for taking the time to respond to a post, but it ends up a waste of both our time if you don't also take the time to read it first.

The technique you've suggested doesn't work, as I outlined in the original post. Do you have any other ideas?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Oct 2010 15:23 Edited at: 21st Oct 2010 15:35
Quote: "baxslash, thanks for taking the time to respond to a post, but it ends up a waste of both our time if you don't also take the time to read it first."

You are right, sorry. I skimmed it...

You could use "mousePosX()/mousePosY()" if you have BlueGUI to get the mouse position on the screen (not just the direct X window). You'd have to check it against the window position too though...

EDIT: Scratch that, it doesn't quite do what I thought it did... I'll look into this though. PS. Please don't give me a hard time when I'm trying to help...

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Oct 2010 15:42 Edited at: 21st Oct 2010 15:42
You said before that the mouse is not detected anymore when it leaves the window. What happens when you put the following command at the start?

always active on

This should make the program always active, no matter where the window is.

If that doesn't work, then baxslash has a good idea of finding the real mouse positions on the entire screen

Do note I can't test anything right now, I'm at work

TheComet

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 15:43
You'll have to forgive me, but I don't agree that skimming a post and then submitting an incorrect response equates to helping. I'm not trying to give you a hard time, but it's not fair to get defensive when someone points out that you've done so

I do appreciate any help you're able to provide, now that you fully understand the problem. I'm using Van's solution for the time being, but I don't like it; it continues to restrain the mouse movements if you switch out the application, so you have to close it completely if you want to switch windows.

Perhaps a way to detect if the application has lost focus, and release the mouse until it regains focus? Do we know how to trap focus events?
Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 15:45
Hi Comet,

I'm not familiar with that command and it's not recognised by my IDE or compiler. I couldn't find anything similar in the command list. Have you got the exact command correct? I'll keep looking.
Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 21st Oct 2010 15:47
Correction: My IDE doesn't recognise the command (ie, it doesn't syntax highlight it) but the compiler doesn't throw any errors or warnings. The command doesn't appear to do anything, however - I am able to switch focus freely as normal.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Oct 2010 15:49
"has focus" is a matrix utility command...

I was only asking you to not give me a hard time when I'm "trying" to help. I didn't say I was helping just that I was trying. You needn't worry about it happening again.

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 15:59
xm=DarkSide_Get_Desktop_MouseX()
ym=DarkSide_Get_Desktop_MouseY()

This finction from DarkSideDeskTop.dll
see the attach.

.....already beside..... for all

Attachments

Login to view attachments
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 16:18
OR for WIN XP only


load dll "user32.dll",1
local hwnd as integer :`desktop window handle
local dc as integer :`device context for entire desktop window
local x,y,in,pix as integer
local dll_user,dll_gdi as integer :`dll numbers
local mem as integer : `memblock number
global PTR as dword
ptr=65553
global PTRa as dword
global color as dword
global col as dword
`load dlls
dll_user = 1
dll_gdi = 2
`get the desktop handle and device context for entire window
hwnd = call dll(dll_user,"GetDesktopWindow")
dc = call dll(dll_user,"GetWindowDC",hwnd)



dd = call dll(dll_user,"GetCursorPos",ptr)
aa=*ptr
ptra=ptr+4
ab=*ptra
center text 50,65,str$(aa)
center text 10,65,str$(ab)
ddaa = call dll(dll_user,"SetCursorPos",aa+1,ab)
position mouse aa,ab+1

.....already beside..... for all
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 21st Oct 2010 17:54
Quote: "Even better, a way to prevent the mouse from leaving the window at all would be ideal."


It's best not to do that. You want the user to have free range to choose any window they want so they can multi-task and the minimize, maximize, and close buttons in your program. Tie the users hands to the window and they'll just get mad when they can't leave the window and probably won't want to run your program again.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 21st Oct 2010 18:15
Whatever you do, make sure you do not constrain the mouse while the window is inactive. Having the mouse stuck in the middle of the screen when you have the offending application minimized - or out of focus - is highly annoying, not to mention unprofessional.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 21st Oct 2010 18:18
One issue with that though, is if your using the mouse to look around and fire - the mouse goes out of the window and you end up clicking on stuff you don't want to.

I'd just do this, and have a release key for the mouse, or have the mouse release during menus, non-gameplay bits - that's how it works in Minecraft at least, when you use the inventory the mouse is released.



Health, Ammo, and bacon and eggs!
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Oct 2010 18:47
In my game Odd Blox I had the mouse reposition to the centre of the screen and released if the user paused or hit escape. I think it worked quite well (basically as VanB suggests). I also used "has focus" in case the user went via the windows button to bring up explorer.

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 25th Oct 2010 07:50
Yes, I tried using a technique similar to Van's suggestion, but I have two problems:

1. I cannot release the mouse when the application loses focus, because I know of no way to detect that loss of focus to trigger the release.

2. MouseMoveX and MouseMoveY are inaccurate. My mouse speed/accelleration does not translate correctly, with a net result in a loss of sensitivity on the mouse. I need to multiply the figures I get by a constant to retain mouse sensitivity between the OS and my application.

I am getting around these problems by not making any attempt to constrain the mouse at all, leaving the user free to pick whatever window he wants. This comes at the expense of being unable to detect when the mouse is at the edge of the window, which is necessary in my latest project (I am panning the user's view when he mouses to the screen edges).

So, if I can find a solution to either one of the two problems I listed above, this issue can be closed. Can anyone help?
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Oct 2010 08:35
You can use IanMs HAS FOCUS() command to check if your program has focus. It returns a 0 if the window doesn't have focus and a 1 if it does.

The following is Van Bs code above with an added check for focus that will only reposition the mouse if the window has focus.



Now if you don't have IanMs Matrix 1 Utilities Plugin... now is a good time to get it.

http://forum.thegamecreators.com/?m=forum_view&t=85209&b=18

Indicium
15
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 25th Oct 2010 10:13
Quote: "2. MouseMoveX and MouseMoveY are inaccurate. My mouse speed/accelleration does not translate correctly, with a net result in a loss of sensitivity on the mouse. I need to multiply the figures I get by a constant to retain mouse sensitivity between the OS and my application."


I'm sure they aren't, you just need to use timer based movement ... or just flame me if i'm wrong... it happens alot xD

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Oct 2010 11:29
Quote: "You can use IanMs HAS FOCUS() command to check if your program has focus. It returns a 0 if the window doesn't have focus and a 1 if it does."

That sounds vaguely familiar... I think someone might have already pointed that command out here...

Quote: "MouseMoveX and MouseMoveY are inaccurate"

I thought I'd test this and it seems you are right. Try the code below and you'll see that there is a difference:


BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 25th Oct 2010 12:17
Just one little thing to add to this thread...if you use mousemovex() and mousemovey(), you might want to put their values in a variable. They are a "one-shot" value, if you read them a second time between SYNCs, they will return zero.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Oct 2010 12:23
Quote: "Just one little thing to add to this thread...if you use mousemovex() and mousemovey(), you might want to put their values in a variable. They are a "one-shot" value, if you read them a second time between SYNCs, they will return zero."

Good point, also if you are only calling it occasionally it will return the difference in values since it was last called which might cause your movement to 'jump'... I often call these commands to a 'null' or unused variable on loops when I'm not using them for movement. That way when you start using them again there is no 'leap' between values...

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 25th Oct 2010 14:34
Yes, I've set them to a variable to avoid this problem.

I will investigate Ian's hasfocus() function.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 25th Oct 2010 14:50
Quote: "I thought I'd test this and it seems you are right. Try the code below and you'll see that there is a difference:"


Yeah, mousemove will return a value even if the mouse doesn't move - so when you move to the far left MOUSEX() will be 0, but MOUSEMOVEX() could be anything. It's a good job though, because half the FPS games written in DBPro would stop working without it - would be like the player had a crick in their neck, as they turn around and the mouse hits the screen edge.

Health, Ammo, and bacon and eggs!
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Oct 2010 17:47
Quote: "That sounds vaguely familiar... I think someone might have already pointed that command out here... "


Sorry I didn't notice you mentioned that earlier.

@ Agent:

Yeah, do what Baxslash first suggested (the HAS FOCUS() command) and you'll be fine.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 14th Feb 2011 05:52
Hey all,

In programming up some mouse boundary code, I thought I'd do a search to see how others were working around this problem.

Here's a function I wrote. The workings of it are as follows;

* Remembers the mouse location upon pressing right click

* Hides, then centres the mouse location to the Window.

* The user then moves the mouse around whilst holding down the right button. Remember the cursor is hidden.

* If the mouse strays +/- 200 units from the centre, the if statements kick in which slowly return it to a 400x400 boundary around the centre of the Window.

* One interesting side effect, is that the mouse cursor never leaves the active game window. I believe this is the desired functionality?

*Upon releasing the mouse right button, the cursor is returned to its original location, and then shown. This part of my code is controlled in another function, not the one below.



Help build an online DarkBASIC Professional help archive.

http://dbprohelp.wikia.com/wiki/DarkBASIC_Professional_Help_Wiki
CodemanV
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: South Wales Valleys, UK
Posted: 14th Feb 2011 07:05 Edited at: 14th Feb 2011 07:19
Hello

Recently, on another post, I used a Code-Snippet shovel to dig up a snippet that covered the desktop mouse in relation to the DBPro app window.

It's useful for keeping the mouse on one monitor in a multi-monitor setup.

http://forum.thegamecreators.com/?m=forum_view&t=78262&b=6

--- edit ---

here's that snippet implementing a function instead


All the best
Ermes
20
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 12th Mar 2011 19:31
Quote: "xm=DarkSide_Get_Desktop_MouseX()
ym=DarkSide_Get_Desktop_MouseY()

This finction from DarkSideDeskTop.dll
see the attach."


this is the only working. used with windowed desktop setting.


Ciao facce da sedere!

Login to post a reply

Server time is: 2024-04-25 02:34:30
Your offset time is: 2024-04-25 02:34:30