4

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.

2
  • 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 that you may be able to take some inspiration from. Commented Oct 9, 2021 at 13:25
  • 1
    @Ash, you can use COM from PowerShell to create a shortcut file with a hotkey; see below. Commented Oct 10, 2021 at 2:01

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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...
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.
Ah it's the same notepad application. Thanks for answering!
check my edit 1 and 2 at: stackoverflow.com/questions/69574360/… you're right. It's just sth with my computer...
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.