How to Remap a Program to lock Windows (Win+L)

I'm trying to remap WinLock to something new. Basiclly i want to remove Win+L to lock Windows and add Win+L to Open a specific program to be opened. Any Help ? Thanks.

PS: currently i using #L::Run "C:\Program Files\program.exe" return to open a program but it also lock workstation.i found a way in Registry to disable the function of Win+L to lock Windows but i dont want to edit registry so i'm Curious if that can be done with autohotkey ?


Solution 1:

AHK can't intercept these Windows shortcuts. If you don't want to edit registry values, I don't think there is a way to do this. The registry value is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System: DisableLockWorkstation which if 1 will disallow locking the system entirely, with or without shortcut, and with 0 locking is allowed and shortcut Win+L will lock the system no matter what tries to intercept it. Commented (for those looking for a working Win+L solution but don't know AHK) code:

With registry editing:

  ; WARNING: Programs that use User32\LockWorkStation (i.e. programmatically locking the operating system) may not work correctly! 
  ; This includes Windows itself (i.e. using start menu or task manager to lock will also not work).
  ; Script changes Win-L to show a msgbox and Ctrl-Alt-L to lock windows

  ; The following 3 code lines are auto-executed upon script run, the return line marks an end to the auto-executed code section.
  ; Register user defined subroutine 'OnExitSub' to be executed when this script is terminating
  OnExit, OnExitSub

  ; Disable LockWorkStation, so Windows doesn't intercept Win+L and this script can act on that key combination 
  SetDisableLockWorkstationRegKeyValue( 1 )
return

#l::
  MsgBox, Win-L was pressed! ; Arbitrary code here
return

^!l::
  ; Ctrl-Alt-L 
  ; Temporary enable locking
  SetDisableLockWorkstationRegKeyValue( 0 )
  ; Lock
  DllCall( "User32\LockWorkStation" )
  ; Disable locking again 
  SetDisableLockWorkstationRegKeyValue( 1 )
return

OnExitSub:
  ; Enable LockWorkStation, because this script is ending (so other applications aren't further disturbed)
  SetDisableLockWorkstationRegKeyValue( 0 )
  ExitApp
return

SetDisableLockWorkstationRegKeyValue( value )
  {
  RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Policies\System, DisableLockWorkstation, %value%
  }