Longpress Home key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Longpress Home key

01 Sep 2021, 20:10

I'm having a very hard time remapping longpress of the Home key to act as the End key.

I started by trying to copy my working code for longpress of the Escape key...

Code: Select all

$Escape::
	KeyWait, %A_ThisHotkey%, T.5
	If %ErrorLevel%
		WinMinimize, A
	else
		Send {Escape}
Return

$Home::
	KeyWait, %A_ThisHotkey%, T.5
	If %ErrorLevel%
		Send {End}
	else
		Send {Home}
Return
This appears to just send the End key every time I press the Home key for some reason.

So I messed around a little and found that there is a difference between the firing of an Escape hotkey vs a Home hotkey...

Code: Select all

$Escape::msgbox %A_ThisHotkey%	; this shows "Escape"
$Home::msgbox %A_ThisHotkey%	; this shows "$Home"
Why would they show different results? I think this is part of the reason I'm having such a hard time.

So next I just replaced the A_ThisHotkey reference for the Home hotkey to use the direct keyname...

Code: Select all

$Home::
	KeyWait, Home, T.5
	If %ErrorLevel%
		Send {End}
	else
		Send {Home}
Return
Now the Home key seems to work correctly when shortpressed, and when you hold it down it does send the End key correctly, but when you release the Home key, it sends the Home key again, moving the cursor back to the beginning of the line.

I messed around some more and found that the "Send {Home}" in the else statement was getting fired when releasing the Home key. (Turns out my Escape longpress hotkey was doing the same thing, but I never noticed because it was just an extra Escape key press which didn't do anything on the next window that took focus after the previous one was minimized.)

So why does the key get sent again when releasing the physical key? I'm using the $ modifier so the keypress shouldn't trigger the key itself, but I don't think that's the issue. If I hold down the Home key, the End key does get sent, proving the KeyWait line and if statement are doing their job using ErrorLevel. So it seems like the Home key is getting sent once when I press the physical key down, and again when I release it, triggering the hotkey again...and since the second time is fast, the KeyWait check doesn't timeout and therefore we skip the if statement and go right to the else and send the Home key a second time.

Thoughts on this? I'm kind of stumped and it's been an hour and a half for me.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Longpress Home key

02 Sep 2021, 05:43

The key repeats when you hold it, so you can wait for the release.

Code: Select all

$Home::
KeyWait, Home, T.5
Send % ErrorLevel ? "{End}" : "{Home}"
KeyWait, Home
Return
Check KeyHistory. Without the extra wait:

image210902-0647-001_cr.png
image210902-0647-001_cr.png (29.12 KiB) Viewed 799 times
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Re: Longpress Home key

02 Sep 2021, 12:43

mikeyww wrote:
02 Sep 2021, 05:43
The key repeats when you hold it, so you can wait for the release.

Code: Select all

$Home::
KeyWait, Home, T.5
Send % ErrorLevel ? "{End}" : "{Home}"
KeyWait, Home
Return
That extra KeyWait solved that issue, thanks! However now I have a new issue...I can't get Shift and Ctrl to work correctly with the hotkey.
Here is what I currently have:

Code: Select all

; longpress Home for End
*Home::
	KeyWait, Home, T.5
	if GetKeyState("Shift")
		Send {Shift down}
	if GetKeyState("Ctrl")
		Send {Ctrl down}
	If %ErrorLevel%
		Send {End}
	else
		Send {Home}
	KeyWait Home
Return
I am using the * modifier to allow the hotkey to fire when modifier keys are pressed, but since that basically ignores Shift and Ctrl, I am using GetKeyState to detect if they're held down to manually send them with the Home/End buttons. I'm not sure if the problem is with the $ modifier (inherited automatically via the * modifier) stopping the hotkey from sending Shift and Ctrl, but I can't figure it out. I can confirm that the GetKeyState checks do succeed correctly when holding Shift or Ctrl (I used a msgbox in place of the Shift down send and it pops up correctly when holding shift), but for some reason Shift and Ctrl never actually get sent out with the End or Home key sends. Maybe its something to do with how the * modifier works; it just flat out prevents any sending of Shift or Ctrl in this case?
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Re: Longpress Home key

02 Sep 2021, 13:08

I got it to work by putting the Shift down and Ctrl down sends on the same line as End/Home sends:

Code: Select all

; longpress Home for End
*Home::
	KeyWait, Home, T.5
	If %ErrorLevel% {
		if (GetKeyState("Shift") and GetKeyState("Ctrl"))
			Send {Shift down}{Ctrl down}{End}
		else if GetKeyState("Shift")
			Send {Shift down}{End}
		else if GetKeyState("Ctrl")
			Send {Ctrl down}{End}
		else
			Send {End}
	} else {
		if (GetKeyState("Shift") and GetKeyState("Ctrl"))
			Send {Shift down}{Ctrl down}{Home}
		else if GetKeyState("Shift")
			Send {Shift down}{Home}
		else if GetKeyState("Ctrl")
			Send {Ctrl down}{Home}
		else
			Send {Home}
	}
	KeyWait Home
Return
Looks a little jank, but it works perfectly, so I'm not complaining. Hope this helps someone else in the future!
jazzvb
Posts: 32
Joined: 14 Jan 2019, 20:10

Re: Longpress Home key

02 Nov 2023, 18:34

I tried

Code: Select all

$Home::
KeyWait, Home, T.5
Send % ErrorLevel ? "{End}" : "{Home}"
KeyWait, Home
Return
and it works like a charm. I replaced "{End}" by for example "Hello, world" and also that worked out. Meaning, you suddenly have a whole lot more shortcuts available. But what I couldn't figure out was to use Run (in combination with a website) instead of Send. The website opened but PgDn (the key I used for this) did not work anymore. Is there a way to bypass that?

Edit: I now see this is version 1.1. Could it be because I use version 2?
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Longpress Home key

02 Nov 2023, 18:38

Show your code.

V2 uses different syntax.
jazzvb
Posts: 32
Joined: 14 Jan 2019, 20:10

Re: Longpress Home key

03 Nov 2023, 12:48

It's more trial and error than programming, as I don't understand coding much. Here's what I tried, mikeyww:

Code: Select all

$PgDn::
KeyWait, PgDn, T.5
Run % ErrorLevel ? "{www.google.com}" : "{PgDn}"
KeyWait, PgDn
Return

Code: Select all

$PgDn::
KeyWait, PgDn, T.5
Run % ErrorLevel ? "www.google.com" : "{PgDn}"
KeyWait, PgDn
Return

Code: Select all

$PgDn::
KeyWait, PgDn, T.5
Run % ErrorLevel ? {www.google.com} : "{PgDn}"
KeyWait, PgDn
Return

Code: Select all

$PgDn::
KeyWait, PgDn, T.5
Run % ErrorLevel ? www.google.com : "{PgDn}"
KeyWait, PgDn
Return
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Longpress Home key

03 Nov 2023, 12:53

Since you want to Run or Send, you can separate the commands so that they are on different lines.

Code: Select all

#Requires AutoHotkey v1.1.33

$PgDn::
KeyWait PgDn, T.5
If ErrorLevel ; Held
 Run http://www.google.com/
Else Send {PgDn}
KeyWait PgDn
Return
jazzvb
Posts: 32
Joined: 14 Jan 2019, 20:10

Re: Longpress Home key

03 Nov 2023, 16:37

Thank you for your help, mikeyww. Earlier I tried to add 'Else' but totally at the wrong place. No that it would have worked anyway, because the other adjustments I couldn't have figured out.
This short script is very powerful and useful.

Kind regards, jazz

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Ralf_Reddings200244 and 278 guests