6

I have a numerical keypad which I use for entering numbers.

Normally, when Windows starts (Windows 11 Pro 10.0.22621) the numlock is enabled.

However whenever I start a Hyper-V VM (gen 2) it turns of the numlock and this is really annoying.

Is there a way to prevent that starting of VM turns of my numlock?

Giacomo1968
  • 58,727

3 Answers3

6

I faced the same issue, and tried different solutions to fix it, but no one out of googled approaches really helpedregistry key:

Finally, I found only one working solution to fix the NumLock issue. It's to create a PowerShell script at VM, let's call it for example 'NumlockEnable.ps1' with the following content:

$WshShell = New-Object -ComObject WScript.Shell
if ([console]::NumberLock -eq $false) {
   $WshShell.SendKeys("{NUMLOCK}")
}

And make the script run at logon, for example by using Group Policy Object (GPO), according to the instructions.

Also, in my case (Windows 10 Guest OS), the following registry key has to be set to the next value

[HKEY_USERS\.DEFAULT\Control Panel\Keyboard]
"InitialKeyboardIndicators"="80000002"

P.S. Of course, this is a crutch, but everything else does not work

2

After many hours of reverse engineering of assembly 'Microsoft.HyperV.PowerShell.Cmdlets' and 'Microsoft.HyperV.PowerShell.Objects', I finally found the best way to make a Hyper-V VM "enable NumLock by default".

TL;DR; Here's the Powershell code:

function Set-VMNumLock {
  param(
    [Microsoft.HyperV.PowerShell.ComputeResource]$vm,
    [bool]$NumLockEnabled
  )
  # Get underlying settings object with reflection
  $ComputerResourceType = [Microsoft.HyperV.PowerShell.ComputeResource]
  $SettingField = $ComputerResourceType.GetDeclaredField("_settings")
  $settingUpdater = $SettingField.GetValue($vm)

$vmComputerSystemView = $settingUpdater.GetData([Microsoft.HyperV.Powershell.UpdatePolicy]::EnsureUpdated) $vmComputerSystemView.BiosNumLock = $NumLockEnabled $vmComputerSystemView.Put() }

Note: This setting is not modifiable when VM is powered on.

Here’s how to use it

$vm = Get-VM -VMName “SampleVM”
Set-VMNumLock -vm $vm -NumLockEnabled $true

FullStory

First I was inspired by some "WMI"/"CIM" Powershell code on some other website.(I'm not sure if I can write hyper-link of other websites here) Although that code does not work, it however did show me the underlying stuff about "Where is the numlock setting stored".

Eventually I decompiled 'Microsoft.HyperV.PowerShell.Cmdlets.dll' and 'Microsoft.HyperV.PowerShell.Objects.dll'. In class Microsoft.HyperV.PowerShell.Commands.SetVMFirmware, you will find everything is defined in Microsoft.HyperV.PowerShell.VMFirmware. Dig deeper, it's underlying data structure was Microsoft.Virtualization.Client.Management.IVMComputerSystemSetting (It's actually shared by both Gen1 and Gen2 VMs), there's a boolean property called BiosNumLock.

Actually Microsoft's programmer is just one step away of supporting NumLock in Set-VMFirmware cmdlet.

In class VMBios, there's a property called NumLockEnabled which is basically a warp of that BiosNumLock

namespace Microsoft.HyperV.PowerShell;

public sealed class VMBios : VMComponentObject, IUpdatable { private readonly DataUpdater<IVMComputerSystemSetting> m_VMSetting;

public bool NumLockEnabled
{
    get
    {
        return m_VMSetting.GetData(UpdatePolicy.EnsureUpdated).BiosNumLock;
    }
    internal set
    {
        m_VMSetting.GetData(UpdatePolicy.None).BiosNumLock = value;
    }
}
// ... other code 

If Microsoft want to support NumLock in Gen2 VMs, they just paste this property to VMFirmware class. I don't know why they're too lazy to do it.

1

For me on win 11 24H2 host this worked: I basically enabled numlock per default on the VM: (Execute on the HyperV VM) Follow these steps to enable the “Num lock” option on boot.

  1. Press “Windows key + R” from the keyboard.
  2. Type “regedit” without quotes and hit enter.
  3. Navigate to “HKEY_USERS.Default\Control Panel\Keyboard”.
  4. Change the value for “InitialKeyboardIndicators” from 0 to 2.
  5. Restart the computer and then check how it works.

Source

Now when I reboot the VM the numlock stays activated for the host and the VM. Before when I rebooted the VM numlock was deactivated for the host and the VM.