The Linux Page

Mouse gets stuck when working between VirtualBox VM and Host

Once in a while, I click on something and the mouse literally dies.

From what I've seen the last time it happened, Opera had a lock issue. It could not create a lock and went on and on about it. Killing Opera did not help, even though the issue happened when I clicked on a button in Opera.

That last click happened in Opera and that's when the mouse froze. This is why I thought killing Opera would help... The good thing, though, is that the keyboard is running.

On that day, I decided to reboot the whole computer anyway because it had been running for over 42 days and so I needed to refresh all sorts of things (VirtualBox, Kernel, etc.) So I started closing each window using the keyboard to go into a console and kill things or move to a window and hit Ctrl-Q.

At some point, I decided to shut down the VirtualBox VM where I was just before I clicked said Opera button... and once it was down, the mouse woke up and worked just fine!

There is the Right-Control key which can be used to catch/release the mouse in a VM. The fact is that I tried that first, many times, until I was sure that it was not that specific issue (which I have had a while back).

Note that there are many other answers about a mouse freeze. If you do not have VirtualBox VMs running, then it probably is a driver issue. In that case, you are likely to have to restart X-Windows. There are many solutions so you'd have to test each one available and see what works for you.

Freeze Reason → Solutions

  • VirtualBox VM + Click → Shutdown the VirtualBox VM and see whether that resolves the issue
  • X11 Only (no VM) →
    • Ctrl-Alt-F1 gets you out to your console, then do Ctrl-Alt-F71
    • If you have a "psmouse" module, remove and reload:
          modprobe -r psmouse
          modprobe psmouse
    • Restart GDM — that worked well on older systems, for some reasons, on my newer system (Ubuntu 18.04) it has never worked correctly:
          systemctl restart gdm
      (And maybe it did not work because we now have a gdm3 as well, so maybe doing systemctl restart gdm3 would work better?)
      I have also seen the following command in case you have KDE:
        DISPLAY=:0 kwin_x11 --replace &
      which will work better than a GDM restart if you have KDE.
    • In the old days, we were running compiz, but I don't think you would still be running off of that environment anymore:
          pkill compiz
    • Regular shutdown Ctrl-Alt-Del
    • Allowing killing the X session for next time; add the following to your ~/.profile file:
        setxkbmap -option terminate:ctrl_alt_bksp
      This allows you to use Ctrl-Alt-Backspace to kill X.
    • Kill the GUI session: Alt + SysRq + K2 (which apparently means Push and Hold the Alt key down, then Press & Release the SysRq key, then Press & Hold the K key, you can then release the Alt key).
    • Keyboard SysRq commands to save your state to disk and then reboot:
          Alt + SysRq + E — killall but give each process a chance to save their state.
          Alt + SysRq + U — write all buffered data to disk (flush memory caches)
          Alt + SysRq + B — reboot
      Please, wait long enough between each keystroke so that way most if not all the processes have time to finish up before you reboot.
    • Turn off the swap — watch out with that one... if your memory is really stressed, the whole system will freeze... not just your mouse:
          sudo swapoff -a
      The -a means all swap spaces will be removed. In most cases, if you have swap on a very slow drive, you certainly will get slowness issues. One way is to get a larger drive (i.e. 10Tb) and use a separate partition used just and only for the swap (i.e. don't use a file). Also avoid using an SSD drive because those are not so good for swap space... you'll kill your drive much sooner even though it will go faster.

Mouse does not get out the VM correctly?

The mouse does not get out the VM on the right edge and the bottom edge. It actually disappear or so it feels.

In this case, the mouse works just fine. However, your window was probably incorrectly resized and part of the GUI extends in a hidden area toward the right and toward the bottom of the window.

One solution is to resize your VM's window to start with. That is rather useless to get the window to the correct size. Editing the Display Resolution is going to be your best bet. So open the preferences, go to the Display settings, then do a change of resolution. When the change happens, the VM receives messages and corrects the size of its window accordingly.

  • 1. Note that the latest version of Ubuntu does not always work with Ctlr-Alt-F7, it may be F8 or just Ctrl-F8 or Alt-F7... just play with those until you see a flash on your screen)
  • 2. Note that you may need to allow the SysRq capability. This is done by setting a flag with the following two commands:
        sudo su -
        echo 1 > /proc/sys/kernel/sysrq