How to pass clicks (MouseDown) events to HTMLViewer?

OK let’s say that the HTMLViewer does not exist in my project. Let’s say that I wanted to capture the mouse pointer to make a FPS game in Xojo. Surely a solution to this exists in Xojo, because if you can’t capture/pin the mouse pointer, then it’s impossible to make a FPS game in Xojo. FPS need you to constantly roll your mouse. The cursor needs to be stuck to the center of the screen at all times, otherwise it will bump on the sides of the screen very quickly and the player will not be able to move in a given direction anymore.

I don’t know that anyone has ever attempted to make an FPS in Xojo, but there are probably declares that can accomplish what you want. I’d check Windows Functionality Suite and macOSLib or @Christian_Schmitz might have them in his plug-ins.

1 Like

Ah, yes, so talking to Windows directly. I did that last month to overcome another Xojo limitation with global floating windows that can only be made frontmost one time and then no longer go to front when calling .show()

OK I will take a look at declares then. Thanks.

Happy to help, as much as I did.

1 Like

It looks like, for Windows, ClipCursor is what you’re looking for.

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-clipcursor?redirectedfrom=MSDN

Interesting, although the requirements table only show Windows 2000? ClipCursor function (winuser.h) - Win32 apps | Microsoft Docs

Or perhaps I’m interpreting this the wrong way :stuck_out_tongue:

It says “Minimum Supported”. :wink:

Can you change the post you selected as the solution?

This is the most janky solution I’ve put together, but now IT FINALLY WORKS! :slight_smile: User32.dll writes the X coordinate in the first 3 bytes and the Y coordinate in the last 3 bytes, so by passing an Int64 by ref and then extracting the first and last 3 bytes, we can get the XY coordinates, precisely up to 4096x4096. If anybody has a larger screen and needs this to work, you’d read the first and last 4 bytes instead of 3. I don’t know anybody who uses a screen that’s larger than 4096 so for the moment, this is perfect! (well not perfect, but almost)

The janky IF covers the scenario where the last hex digit is a zero. This is guesstimated as a disposable byte if the resulting integer from the full 3 bytes is larger than the real screen’s height. Obviously, it will work +94% of the time, but there is a small chance that the disposable zero digit was actually good. For a project where the mouse moves a lot, I would guess you could achieve 100% accuracy by simply ignoring the frames where the last digit is a zero altogether. I don’t think it would be noticeable where the mouse changes position 50-60 times a second.

ezgif-3-aa4ced711c

So there you have it, how to capture (get + set) the mouse cursor in a Xojo app (for Windows) for free. Enjoy. Tested under both Windows 7 and Windows 10 (up to date release).

Check this capture out wow: :slight_smile:

ezgif-4-bcbd306628

:rofl:

:face_with_raised_eyebrow: That is all sorts of wrong.

GetCursorPos returns a structure made up of 2 int32, you can just pass a 8 bit MemoryBlock as a Ptr param to the function and then just use the Int32Value method of the MemoryBlock to get the values directly.

Being an int32 the maximum value shoul be like 2,147,483,647 NOT 4,096

I know it expects a Point(x,y) but as I wrote earlier, Xojo doesn’t want to compile, complaining that you can’t pass an object in an external function :frowning:

Also, as I wrote above, it’s not int32, I read 12 bits (3 bytes) because 4096px is enough for most people needs. Like, when is it that you can, IN A SINGLE FRAME (1/60th of a second) displace your mouse cursor more than 4096 pixels in any given direction? And do you have a screen that’s larger than 4096px to begin with? You can read 4 bytes if you have a screen that’s 65536px wide and a SUPER TURBOCHARGED mouse :stuck_out_tongue:

Can you provide a working code example of the MemoryBlock thing you referred to above, please?

True statement. That way lies madness. Use the right tool for the job.

1 Like

Hey, at least I got it working thanks to User32 Lib declares as shown in the animated GIF above. It’s not like I’m planning on making a FPS game in Xojo either, so it’s all good. But when I read “Use the right tool for the job”, what comes in my mind is: What exactly is Xojo the right tool for then?

Xojo is great for cross platform applications. It works quite well for database intensive applications such as accounting and inventory management. However, the cross platform nature of Xojo means that it often hits the lowest common denominator between platforms, which means it can be difficult to produce applications that require high performance or need to talk to the hardware more directly.

I have been working with Xojo for almost 20 years and I find I can do almost anything with it, provided I spend enough time and manpower to do so. When it comes to working with OS specific declares, I sometimes think I might as well be coding in assembler.

Printing has been a pain, but after about a year of R&D, we created a good framework that produces very nice reports and forms. But it requires translating everything down to the pixel level. It can be done, but Xojo doesn’t help much with it. (We also export to PDF and Excel, so that was part of the devel process.)

On Windows, there is a 13ms pause every time the event loop runs, which makes it nearly impossible to make any kind of performant game or application that requires real-time control.

I abandoned Xojo for web and mobile applications. There are just better tools out there. That said, I am intrigued by what Xojo is doing these days. They might just suck me back in for those platforms. We’ll see.

The HTMLViewer, in particular, has always been limited in functionality. There is a firewall between Xojo and the viewer. It’s very difficult to communicate back and forth. HTMLViewer is a viewer (one way), not a full featured control.

And the web controls - httpsocket et. al - have been limited as well. They have gotten better, but they’re not what I would call production worthy.

Hope that helps clarify my statement.

2 Likes
Declare Function GetCursorPos Lib "User32" (pt As Ptr) As Boolean
Dim mypt As New MemoryBlock(8)
Call GetCursorPos(mypt)
Dim x1 As Integer = mypt.Int32Value(0)
Dim y1 As Integer = mypt.Int32Value(4)
1 Like

Just to mention it, no need for declares when Xojo System.MouseX, System.mouseY and System.MouseDown already provide what is needed to get MouseDown systemwide.

…TO GET the mouse information yes, but unless you’re telling me that you can write a value to System.MouseX and System.MouseY, then it’s only half of the solution. I don’t know if you understand what has been done in the marked as solution post above, but the declares allow me to arbitrarily force the mouse cursor to move anywhere, on demand. This allows for mechanisms like pointer lock control that HTML5 already has had for a decade. In simulation apps and also games, this is mandatory to have infinite mouse movement, otherwise the mouse cursor bumps the side of the screen and can no longer provide a X/Y movement delta. Check out the solution post, I put animated GIF and all the code: How to pass clicks (MouseDown) events to HTMLViewer? - #31 by Ben_D

No, they’re read-only. Also, although system.MouseDown tells you whether the mouse is down or not, there’s no corresponding event. One would need to poll it.

This is 100% working. Very nice! I love it, it’s clean too :heart:

@Ben_D if you like clean, then use a Structure:

Structure Point32
  x as Int32
  y as Int32
End Structure

Declare Function GetCursorPos Lib "User32" (byref pt as Point32) As Boolean
Dim mypt As Point32
Call GetCursorPos(mypt)
dim x1 As Integer = mypt.x
Dim y1 As Integer = mypt.y