Skip to content

Instantly share code, notes, and snippets.

@hailangx
Last active November 13, 2025 19:25
Show Gist options
  • Select an option

  • Save hailangx/75c28cb4a926be61e32c65a9d32cb21d to your computer and use it in GitHub Desktop.

Select an option

Save hailangx/75c28cb4a926be61e32c65a9d32cb21d to your computer and use it in GitHub Desktop.
Remapping-WindowsKey-to-ControlKey
# Windows Key to Control Key Remapping Script
# This script remaps the Windows key to act as a Control key using Windows Registry
# Run as Administrator for the changes to take effect
param(
[switch]$Restore,
[switch]$Help
)
function Show-Help {
Write-Host @"
Windows Key to Control Key Remapping Script
USAGE:
.\remap_winkey_to_ctrl.ps1 - Apply the remapping
.\remap_winkey_to_ctrl.ps1 -Restore - Restore original key mapping
.\remap_winkey_to_ctrl.ps1 -Help - Show this help
DESCRIPTION:
This script modifies the Windows Registry to remap the left Windows key to function
as a Control key. The right Windows key remains unchanged. The changes require a
restart or log off/log on to take effect.
REQUIREMENTS:
- Must be run as Administrator
- Requires restart for changes to take effect
REGISTRY LOCATION:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout
"@ -ForegroundColor Cyan
}
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Set-KeyRemapping {
Write-Host "Applying Left Windows Key to Control Key remapping..." -ForegroundColor Yellow
# Registry path for keyboard layout
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout"
# Scancode Map value to remap Left Windows key (0x5B) to Left Control (0x1D)
# Format: Version (4 bytes) + Flags (4 bytes) + Number of entries (4 bytes) + Mappings + Null terminator (4 bytes)
# Each mapping: Target scancode (2 bytes) + Source scancode (2 bytes)
$scancodeMap = [byte[]](
0x00, 0x00, 0x00, 0x00, # Version (0)
0x00, 0x00, 0x00, 0x00, # Flags (0)
0x02, 0x00, 0x00, 0x00, # Number of entries (2: 1 mapping + null terminator)
0x1D, 0x00, 0x5B, 0x00, # Map Left Windows (0x5B) to Left Control (0x1D)
0x00, 0x00, 0x00, 0x00 # Null terminator
)
try {
# Set the registry value
Set-ItemProperty -Path $regPath -Name "Scancode Map" -Value $scancodeMap -Type Binary
Write-Host "✓ Registry updated successfully!" -ForegroundColor Green
Write-Host "✓ Left Windows key has been remapped to Control key" -ForegroundColor Green
Write-Host "⚠️ Please restart your computer or log off/log on for changes to take effect" -ForegroundColor Yellow
}
catch {
Write-Host "✗ Error updating registry: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
function Restore-KeyMapping {
Write-Host "Restoring original key mapping..." -ForegroundColor Yellow
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout"
try {
# Remove the Scancode Map registry value to restore default behavior
Remove-ItemProperty -Path $regPath -Name "Scancode Map" -ErrorAction SilentlyContinue
Write-Host "✓ Original key mapping restored!" -ForegroundColor Green
Write-Host "⚠️ Please restart your computer or log off/log on for changes to take effect" -ForegroundColor Yellow
}
catch {
Write-Host "✗ Error restoring registry: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
function Main {
if ($Help) {
Show-Help
return
}
Write-Host "Windows Key to Control Key Remapping Script" -ForegroundColor Cyan
Write-Host "=" * 50 -ForegroundColor Cyan
# Check if running as Administrator
if (-not (Test-Administrator)) {
Write-Host "✗ This script must be run as Administrator!" -ForegroundColor Red
Write-Host " Right-click on PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
exit 1
}
Write-Host "✓ Running with Administrator privileges" -ForegroundColor Green
if ($Restore) {
Restore-KeyMapping
}
else {
# Show current status
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout"
$currentValue = Get-ItemProperty -Path $regPath -Name "Scancode Map" -ErrorAction SilentlyContinue
if ($currentValue) {
Write-Host "ℹ️ Key remapping is currently active" -ForegroundColor Blue
}
else {
Write-Host "ℹ️ No key remapping currently active" -ForegroundColor Blue
}
Set-KeyRemapping
}
}
# Run the main function
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment