Different actions on Short-press and Long-press of hotkey Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Lem2001
Posts: 63
Joined: 27 Jun 2017, 17:59

Different actions on Short-press and Long-press of hotkey

13 Mar 2020, 11:32

I would like to assign a 'focus Desktop' command to the Win D hotkey to set focus to the Desktop without hiding / minimizing any open windows (e.g. something like WinActivate, ahk_class Progman).

I also want to retain the existing built-in Windows 'Show Desktop' function (i.e. hide all windows, then focus Desktop) and have this activated by a long-press of Win D.

In theory it's not too difficult to achieve these two outcomes, but the problems I'm having are to do with various unwanted side-effects due to me not knowing how best to structure the code.

Issues I'm struggling to resolve are:
  • Being able to long-press and have the 'Show Desktop' feature activate with or without hotkey release.
  • Preventing multiple triggers when long-pressing the hotkey (should only fire once until key is released, regardless of keypress duration).
  • Using the real Show Desktop feature that can restore open windows to previous state (not a manual minimize command)
  • Getting each subsequent long-press to alternate between Show Desktop and 'Unshow' Desktop (instead of only activating 'Show' Desktop)
  • Not interfering with any other use of the Win key (either on its own or in other Win key combinations)
I know that this is probably straightforward, but my first attempts are such a broken mess that I won't bother posting them here. One problem was that, despite me setting KeyWait as part of the executed command (to prevent multiple triggers when long-pressing Win D), it ended up logging me out of my account instead of just Showing the Desktop (multiple triggers were causing weird outcomes).
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Different actions on Short-press and Long-press of hotkey  Topic is solved

13 Mar 2020, 12:47

Hallo,
try:

Code: Select all

$#d::
KeyWait, d, T.5 ;500 ms
IF ErrorLevel
	SendInput, {Blind}d
Else
	WinActivate, ahk_class Progman
KeyWait, d
KeyWait, LWin
Return
Lem2001
Posts: 63
Joined: 27 Jun 2017, 17:59

Re: Different actions on Short-press and Long-press of hotkey

13 Mar 2020, 16:09

Wow this is amazing, Rohwedder.

You have taken into account all of my criteria, and it works exactly as I requested. Thank you so much!
The $ sign and {Blind} were two of the things that I had overlooked in my code attempts (I am not familiar with them).

Seeing as this solution works so well, I would like to ask for just one more thing to be added please:

If Win D is short-pressed when the Desktop currently has focus, switch focus back to the window that was last active.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Different actions on Short-press and Long-press of hotkey

14 Mar 2020, 02:43

Then:

Code: Select all

$#d::
KeyWait, d, T.5 ;500 ms
IF ErrorLevel
	SendInput, {Blind}d
Else IF WinActive("ahk_class Progman")
	WinActivate, ahk_id %LastID%
Else
{
	LastID := WinExist("A")
	WinActivate, ahk_class Progman
}
KeyWait, d
KeyWait, LWin
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, Google [Bot], pnKed, SilasDeVis and 60 guests