NumLock problems have plagued VirtualBox for more than a decade.
Periodically they were fixed, only to reappear again in the next
release.
It seems that it's quite complicated to keep account of this state
when focus and NumLock state may change by a simple movement of
the cursor.
The article
VirtualBox numlock weirdness
by Martin van Beurden from 2015 claims to have found this solution:
After upgrading to VirtualBox 5 on Windows every time I started,
focused or unfocused a virtual machine my numlock status would toggle.
If you're having the same problem here is how to fix it:
VBoxManage setextradata "<machine-name>" GUI/HidLedsSync "0"
This will disable keyboard indicators (HID LEDs) synchronization. This
setting it supposed to be disabled by default but for some reason it
was not on my installation. VBoxManage is located where you installed
virtualbox, usually C:\Program Files\Oracle\VirtualBox.
If you have a lot of virtual machines you can process them all by
running the following commands:
Open a command prompt
Add virtualbox to you path:
set path=%path%;C:\Program Files\Oracle\VirtualBox
Go to the directory where you're virtual machines are located, commands below are for a default install:
%HOMEDRIVE%
cd "%HOMEPATH%\VirtualBox VMs"
Update the setting for all machines in the directory
for /f "tokens=*" %G in ('dir /b /a:d ".\*"') do (VBoxManage setextradata "%G" GUI/HidLedsSync "0")
Hooray, no more numlock weirdness.
I'm not using VirtualBox, so cannot test if this 2015 fix still works.