Skip to content

Instantly share code, notes, and snippets.

@gene-pavlovsky
Last active March 27, 2023 19:43
Show Gist options
  • Save gene-pavlovsky/97da0fa85bbf323fb8b0716991f1d558 to your computer and use it in GitHub Desktop.
Save gene-pavlovsky/97da0fa85bbf323fb8b0716991f1d558 to your computer and use it in GitHub Desktop.
EnvGet, ProgramFiles32, ProgramFiles(x86)
EnvGet, ProgramFiles64, ProgramFiles
if (!ProgramFiles32)
ProgramFiles32 := ProgramFiles64
; ************************************************************************************************************************************************************
; Run/show/hide KeePassXC using Win+K
;
#k::
if (WinExist("ahk_exe KeePassXC.exe") && WinActive("ahk_exe KeePassXC.exe"))
WinMinimize
else
Run % ProgramFiles64 . "\KeePassXC\KeePassXC.exe"
return
@CerebralFreeze
Copy link

Hi, Gene,

Thank you for making a concise and helpful script. I really appreciate it since I'm a newb at AutoHotkey and even writing a simple script like you wrote would have taken me an entire day because I have to literally look up every single function and figure out how it works. I'm sure there are others as well as who are in the same boat as me. So, thanks.

Unfortunately, while trying to implement your script, I encountered an error. It might be due to the fact that I didn't make this a separate, independent script. Instead, I cut and pasted your script, adding it to the bottom of my big script dedicated for all my global hotkeys. No immediate error occurred but when I pressed Win+k, an error dialog window popped up. It stated:

Error: Failed attempt to launch program or document:

Action: <\KeePassXC\KeePassXC.exe>
Params: <>

Specifically: The system cannot find the file specified.

It also stated that the following line was the cause of the error. It refers to the 2nd-to-last line in your script.

Run,ProgramFiles64 . “\KeePassXC\KeePassXC.exe”

I guess you added an automatic method to determine whether the user was using a 32 or 64-bit version of KeePassXC. Very nice feature but unfortunately, something was wrong.

I amended your script to the one below. I just wanted to put it here so other newbs like me can just cut and paste it instead of figuring out how to fix the script by themselves. The script I rewrote was:

`::
If (WinExist("ahk_exe KeePassXC.exe") && WinActive("ahk_exe KeePassXC.exe"))
	WinMinimize
Else
{
	Run C:\Program Files\KeePassXC\KeePassXC.exe
	WinActivate, ahk_exe KeePassXC.exe
}
Return

**Just to let everyone know, I changed the hotkey from Win+k to the ` key. This is the key that is at the top left of the keyboard, right below the Escape button. I thought it would be much easier to use this to toggle the password manager which I use a few times a day.

***I also wrote this script for KeePassXC for 64 bit. If you have the 32 bit version, change the 4th to last line to my script to:

Run C:\Program Files(86)\KeePassXC\KeePassXC.exe

****Finally, stay healthy!! I think everyone is eventually going to have to contract the Coronavirus. I am guessing that whether you live or die will depend upon how healthy you are.

@gene-pavlovsky
Copy link
Author

Hi CerebralFreeze,

The EnvGet lines read Windows environment variables ProgramFiles(x86) and ProgramFiles, which have different values depending on whether your computer and/or Windows are 64-bit or 32-bit. These days it's getting less and less relevant as I suppose everyone runs 64-bit, but as a programmer I like to make things more compatible if it's not too much of a hassle.
https://stackoverflow.com/questions/9594066/how-to-get-program-files-x86-env-variable

Software being compiled for 32-bit or 64-bit is another thing, and my script doesn't do any auto-detection for this. I just use the right variable name depending on which version of which software I have installed (some only come in 64-bit or 32-bit, and some are available in both versions).

The error that you were having looks like the variables were not getting defined, which is weird.

There is nothing wrong with hardcoding the complete path, and I'm glad you figured it out and have it working.

I've recently had some issues with minimize working but activate/restore not. I don't know if it was caused by some KeePassXC update or something else, but it just wouldn't restore when "minimize to tray" option was activated. I didn't want to bother investigating it and just disabled minimizing to tray. It is a bit annoying, though. I wish KeePassXC adds this toggle window command-line option one day, which would make start/hide/show an AutoHotKey one-liner.

So far healthy, and doing my best to stay in shape. Exercise / play / red wine or beer is my recipe.

Cheers

@CerebralFreeze
Copy link

Gene,

To be honest, I'm a newb programmer so I don't know what the beginning section does or how the variables get defined later. So, I just wrote it the simple beginner way. You can change it back if you want. I just didn't know how to correct it.

Just to let you know, I haven't had any issues with active/restore after "minimize to tray" option was activated. So, I'm speculating the problem is related to the expert method you used.

Anyway, thanks again for the script. I appreciate it.

Enjoy the weekend!

@valner88
Copy link

What does this ahk script do??

@gene-pavlovsky
Copy link
Author

gene-pavlovsky commented Sep 14, 2021

@valner88 What is described in the comment on line 7 - runs, shows or hides KeePassXC (depending on it's current state), when you press Windows key and K simultaneously.

Similar to how (on Windows 7+) you can run/show/hide an app pinned to your taskbar by using Windows key and a corresponding digit 1-9.

I use various other similar shortcuts to make it quick to switch between commonly used apps. Some apps that hide to tray on minimize are a bit more tricky to unhide (WinExist returns false for the hidden window, you have to use some options like detect hidden windows etc), KeePassXC is actually one of them, I had a script that worked for a while but stopped working with another KeePassXC update, in the end I gave up and just disable the hide to tray option in it's settings.

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