Skip to content

Instantly share code, notes, and snippets.

@darylwright
Created April 19, 2020 11:50
Show Gist options
  • Save darylwright/f3272846650b28906550a0454d7f4305 to your computer and use it in GitHub Desktop.
Save darylwright/f3272846650b28906550a0454d7f4305 to your computer and use it in GitHub Desktop.
This PowerShell script frees up the Hyper key, which Windows uses as the Office key, so that it is freed up for other use cases.
# DisableOfficeHotKeys
#
# This script disables the hot keys that Windows 10 uses as a result of hijacking
# the Hyper key (Alt + Ctrl + Shift + Win/Super/Command), otherwise known as the
# Office key on some Microsoft devices. This is accomplished by stopping the
# explorer process and registering the hot keys before explorer has a chance to
# do so. The hot keys are then unregistered so that they are free for other use
# cases. This process is reversible via the -Reenable flag.
#
# References:
# - How-To Geek: https://www.howtogeek.com/445318/how-to-remap-the-office-key-on-your-keyboard/
# - OfficeKeyFix: https://github.com/anthonyheddings/OfficeKeyFix/
# - RegisterHotKey: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
# - UnregisterHotKey: https://docs.microsoft.com/en-ca/windows/win32/api/winuser/nf-winuser-unregisterhotkey
# - Pinvoke.net: http://pinvoke.net/default.aspx/user32/RegisterHotKey.html
param(
[switch]$Reenable = $false
)
# Restore original Office hot key functionality with the -Reenable flag
if ($Reenable) {
$regPath = "HKCU:\Software\Classes\ms-officeapp\Shell"
if (Test-Path $regPath) { Remove-Item -Path $regPath -Recurse -Force }
Stop-Process -Name "explorer" -Force
Start-Process -FilePath "explorer"
return
}
# Add the assembly reference that contains hot key registration methods
$signature = @"
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern int UnregisterHotKey(IntPtr hwnd, int id);
"@
$hotKeyRegister = Add-Type -MemberDefinition $signature -Name "HotKeyRegister" -Namespace "Win32Functions" -PassThru
if ($null -eq $hotKeyRegister) {
Write-Host "Unable to import hot key registration module, exiting."
return
}
# These keys map to W, T, Y, O, P, D, L, X, N, and Space, respectively.
$officeKeys = @(0x57, 0x54, 0x59, 0x4F, 0x50, 0x44, 0x4C, 0x58, 0x4E, 0x20)
# We need to stop the explorer process to register the office hot keys
Stop-Process -Name "explorer" -Force
$hyperKey = 0x1 + 0x2 + 0x4 + 0x8 # Hyper is Alt + Ctrl + Shift + Win
$noRepeat = 0x4000 # This modifier suppresses keyboard auto-repeat for hot keys
# Register hot keys while explorer isn't running
for ($i = 0; $i -lt $officeKeys.Count; $i++) {
$hotKeyRegister::RegisterHotKey([System.IntPtr]::Zero, $i, $hyperKey -bor $noRepeat, $officeKeys[$i]) | Out-Null
}
# explorer won't be able to register Office hot keys on startup since they're already taken
Start-Process -FilePath "explorer"
# Wait long enough for explorer to finish attempting hot key registration
Start-Sleep -s 4
# Unregister the hot keys so that they're free to use in other applications
for ($i = 0; $i -lt $officeKeys.Count; $i++) {
$hotKeyRegister::UnregisterHotKey([System.IntPtr]::Zero, $i) | Out-Null
}
# The hyper key itself needs to be unbound from the Office application via a registry entry
$regPath = "HKCU:\Software\Classes\ms-officeapp\Shell"
if (-not (Test-Path $regPath)) { New-Item -Path $regPath | Out-Null }
$regPath = $regPath + "\Open"
if (-not (Test-Path $regPath)) { New-Item -Path $regPath | Out-Null }
$regPath = $regPath + "\Command"
if (-not (Test-Path $regPath)) { New-Item -Path $regPath | Out-Null }
New-ItemProperty -Path $regPath -Name "(Default)" -PropertyType String -Value "rundll32" | Out-Null
@GreatestFool
Copy link

This does not seem to work anymore.

@darylwright
Copy link
Author

I haven't used this utility since upgrading to Win11. I'll find some time to look into it. Thanks for reporting!

@darylwright
Copy link
Author

This does not seem to work anymore.

I just tested the script in Windows 11 without administrator privileges and it seems to still work as intended. @GreatestFool, could you provide more details on what isn't working for you? What OS are you using, and what happens when you run the script? Thanks!

@GreatestFool
Copy link

Sorry for the late reply. I'm just using Windows 10, without administrator privileges. The script runs, didn't produce any errors, but nothing happens either. I'm suspecting the keys aren't being registered properly, but I'm not particularly familiar with this kind of workaround.

@darylwright
Copy link
Author

Try adjusting the sleep time in the script (line 67) to a higher number and see if that works. It's the only thing I can quickly think of at the moment with what I know about your problem.

Start-Sleep -s 10 

If explorer.exe takes too long to restart, the script will already have unregistered the hotkeys by the time that finally happens, making them available for Explorer to remap them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment