Enabling sloppy mouse focus in Windows

I'm a fan of the sloppy mouse focus model, where focus follows the mouse cursor except when the cursor is over the desktop or the task bar. Only recently did I find that this can be enabled in Windows by twiddling a bit in the registry.

The particular registry key is: HKEY_CURRENT_USER\Control Panel\Desktop\UserPreferencesMask.

Setting the least significant bit of the first byte enables this focus model. To make this easier, I've written the following PowerShell function:

  1. # Enable or disable 'Sloppy' mouse focus. Effective on the next login.  
  2. function Set-SloppyMouseFocus([switch]$Enable)  
  3. {  
  4.     # Read user preferences mask.  
  5.     $regKeyPath = "HKCU:\Control Panel\Desktop\" 
  6.     $prefs = $(Get-ItemProperty $regKeyPath).UserPreferencesMask 
  8.     # Index into UserPreferencesMask where the relevant flag resides. 
  9.     $flag = 0x0 
  11.     # Bit in UserPreferencesMask[$flag] which sets sloppy mouse focus. 
  12.     $sloppy = 0x01 
  14.     if ($Enable) 
  15.     { 
  16.         Write-Host "Enabling 'Sloppy' mouse focus." 
  17.         $prefs[$flag] = $prefs[$flag] -bor $sloppy 
  18.     } 
  19.     else 
  20.     { 
  21.         Write-Host "Disabling 'Sloppy' mouse focus."  
  22.         $sloppy = -bnot $sloppy  
  23.         $prefs[$flag] = $prefs[$flag] -band $sloppy  
  24.     }  
  26.     # Update UserPreferencesMask.  
  27.     Set-ItemProperty -Path $regKeyPath -Name UserPreferencesMask -Value $prefs  
  28. }  

To enable Sloppy focus, save that snippet as a file and dot-source it, then execute Set-SloppyMouseFocus -Enable.

  1. . .\Set-SloppyMouseFocus.ps1  
  2. Set-SloppyMouseFocus -Enable  

This is particularly useful if you're using multiple monitors.

Credit for this goes to Scurvy Jake's Pirate Blog.


Comments (0)

Skip to main content