3

Trying to control virtualbox programatically some unexpected behaviour occourse. I try to:

  1. Place the mouse-cursor at 200x200.
  2. Move the mouse-cursor by 10x10 to the lower-right.

Steps to reproduce:

  1. Start a virtualbox-machine.
  2. Start xev in the virtualbox-machine.
  3. Start vboxwebsrv
  4. Enter user-credentials for vboxwebsrv-Server in the code below

This is my code:

VirtualBoxManager manager = VirtualBoxManager.createInstance(null);

private IVirtualBox connect() throws IOException, FileNotFoundException {
    manager.connect("http://localhost:18083", "user", "pass");
    return manager.getVBox();
}

public void testMousePos() throws Exception {
    IVirtualBox box = connect();
    for (IMachine machine : box.getMachines()) {
        if (machine.getState() == MachineState.Running) {
            ISession session = manager.getSessionObject();
            machine.lockMachine(session, LockType.Shared);
            try {
                IConsole console = session.getConsole();
                IMouse mouse = console.getMouse();
                mouse.putMouseEventAbsolute(200, 200, 0, 0, 0x00);
                mouse.putMouseEvent(10, 10, 0, 0, 0x00);
            } finally {
                session.unlockMachine();
            }
        }
    }
}

This will produce this screenshot:

Now, the first putMouseEventAbsolute(200, 200, places the mouse to 199x199, that is almost ok.

Then the putMouseEvent(10, 10, result in the new position of 203x203, that is NOT ok.

Then

If I restart the test it is getting more worst.

The position is now changed from 203x203 to 208x208. The putMouseEventAbsolute seems to be not executed at all anymore (even if I restart the vboxwebsvr-service).

Any idea?

1 Answer 1

0

Now, the first putMouseEventAbsolute(200, 200, places the mouse to 199x199, that is almost ok.

That looks like VirtualBox issue 9321, duplicate of 7566 with the comment:

Actually there is only a single pixel difference, as the API officially uses an origin of (1, 1) for reasons no one can remember.
Which no one wants to change now.

That means, if you want the mouse to end up at (200,200) you would need to provide (202,202) as the arguments to putMouseEventAbsolute, given the current behavior of the VirtualBox API​.

Looking at "IMouse::putMouseEventAbsolute - no mouse event received by VM ", absolute mouse events will only get through if IMouse::absoluteSupported returns TRUE (similarly for relative mouse events and IMouse::relativeSupported).
If the VM is configured to use an actual mouse (which is inherently relative), then this will only be the case if the Guest Additions are installed in the VM, because these add absolute support. If you configure the VM to use the USB tablet (which is the default configuration for reasonably new Linux VMs), then the absolute events should always go through.

I see also "PutMouseEventAbsolute not moving mouse": It has been suggested that when you send absolute position events, the guest OS may assume that you are telling it the location of the host cursor and stop drawing it itself.
Using relative position events (mouse.putMouseEvent(dx, dy, dz, dw, buttonState);) would therefore be advisable, and yet, as you have seen, the result in strange.
If the guest system's display is not at a 1:1 pixel ratio with the host system, or if there is any kind of display scaling going on, this could affect the interpretation of the mouse events: try and check both resolutions.
In VirtualBox itself, do also check the "Display" settings for the specific virtual machine. Look at the "Screen" tab and make sure that the "Scale Factor" is set to 100%. Also, ensure that the "Graphics Controller" is set appropriately for your guest system.

5
  • @Grim OK. What version of VirtualBox are you using? Would a different mouse mode yield the same result?
    – VonC
    Jun 1 at 16:24
  • I am using VirtualBox 7.0.6 r155176 (Qt5.15.8).
    – Grim
    Jun 5 at 9:21
  • FYI The VM is configured to use USB Tablet. I did not change that.
    – Grim
    Jun 5 at 9:23
  • @Grim OK. At this point, VB forums might be a better place to seek an answer. I hope someone will have one here (Stack Overflow) or there (VB forum) for you.
    – VonC
    Jun 5 at 9:41
  • yea, I posted to vb-forum and referenced this stackoverflow question. Different mouse-mode did not helped. Thanks for the effort but the problem remains unchanged.
    – Grim
    Jun 5 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.