3

I am creating an application and I want that when the user opens my app, it should create a hotkey for itself like ctrl + alt + f so that the app automatically runs everytime the user presses those key combinations.

I know how to achieve this manually in windows by right-clicking the app and adding a key combination but I don't know how to achieve this programmatically as I don't want the user to do this by their own.

user692942
  • 16,398
  • 7
  • 76
  • 175
Abhay Salvi
  • 890
  • 3
  • 15
  • 39
  • 1
    I believe that registering and unregistering hot keys is only available via the Windows API programmatically, so you would need to use Pinvoke with either VBS, or C# and then make the type available to PowerShell if you were planning on writing a PowerShell script. There is a VB example [here](https://stackoverflow.com/a/34755545/9164015) that you may be able to take some inspiration from. – Ash Oct 09 '21 at 13:25
  • 1
    @Ash, you can use COM from PowerShell to create a shortcut file with a hotkey; see below. – mklement0 Oct 10 '21 at 02:01

1 Answers1

4

You can programmatically create a shortcut file (*.lnk) that points to your application executable, which also allows defining a hotkey for invocation.

The following example creates MyApp.lnk on your Desktop, which launches Notepad, optionally via hotkey Ctrl+Alt+F; the hotkey takes effect instantly:

  • Caveat: Only shortcut files saved to select directories ensure that their hotkey definitions take effect persistently, i.e. continue to work after reboots. The (current user's) Desktop works - I'm unclear on what other directories work as well.
# Get the Desktop dir. path.
$desktopDir = [Environment]::GetFolderPath('Desktop') 

# Create the shortcut object, initially in-memory only.
$shc = 
  (New-Object -ComObject 'Wscript.Shell').CreateShortcut("$desktopDir\MyApp.lnk") 

# Set the target executable (name will do if in $env:PATH).
# If arguments must be passed, use the separate .Arguments property (single string)
$shc.TargetPath = 'notepad.exe' 

# Assign the hotkey.
$shc.Hotkey = 'Ctrl+Alt+F'

# Save to disk (using the initially specified path).
$shc.Save() 

See the documentation of the WshShortcut COM object, of which the above creates an instance.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I've got one issue. The script takes 4-5 sec to open the desired application with the hotkey. Can you tell me how to speed this up? It runs fast in a sec at the time when I boot up my PC, but then it becomes sluggish to function. Like 5 seconds is too long to pen the app... – Abhay Salvi Oct 14 '21 at 10:36
  • 1
    I'm sorry to hear it, @Abhaysalvi - unfortunately, I have no explanation. Are you sure that it is the hotkey mechanism that makes it slow, or does the application itself slow down on startup? Either way, this sounds like a separate problem (I didn't see this in my tests, and whether you assign a hotkey interactively or programmatically shouldn't make a difference), so I encourage you to ask a new question. – mklement0 Oct 14 '21 at 13:40
  • 1
    Ah it's the same notepad application. Thanks for answering! – Abhay Salvi Oct 14 '21 at 14:30
  • 1
    check my edit 1 and 2 at: https://stackoverflow.com/questions/69574360/powershell-script-is-slow-in-opening-an-application you're right. It's just sth with my computer... – Abhay Salvi Oct 15 '21 at 02:32
  • Hey umm, I've been stuck in a strange situation. Your script works fine when assigned the `.lnk` file to the desktop. But when I try to use a different location, it does creates the shortcut but the hotkeys for some reason don't work. It only works when the `.lnk` file is on the desktop. Any idea about that? – Abhay Salvi Oct 15 '21 at 14:58
  • 1
    @Abhaysalvi, yes, that is a limitation that is also stated in the answer. I don't know where the behavior is documented and what other special folders, if any - in addition to the desktop - support shortcut files with hotkeys that work _persistently_ (i.e., across reboots). Pragmatically speaking, it makes sense that not just _any_ folder can be supported, as the system would then have to scan the entire hard drive to find all hotkeys. – mklement0 Oct 15 '21 at 15:06