spacebar long press as modifier

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

spacebar long press as modifier

28 Nov 2023, 09:18

Hello All

I need Spacebar single press to send regular Space as usual
Spacebar long press to act as if I am holding Alt
When I release Spacebar, Alt is released

Thanks
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: spacebar long press as modifier

28 Nov 2023, 09:36

Code: Select all

*Space::
	Send {Blind}{Alt Down}
	KeyWait, Space ; waits for Space to be released
return

*Space Up::
	Send {Blind}{Alt Up}
	If (A_PriorHotKey = "*Space" AND A_TimeSincePriorHotkey < 300)
		Send {Blind}{Space}
return
viewtopic.php?f=76&t=60608&p=482892#p482892
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

28 Nov 2023, 09:39

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33

*Space::
KeyWait Space, T.3
If ErrorLevel {
 SetKeyDelay -1
 SendEvent {Blind}{Alt DownR}
 KeyWait Space
 SendEvent {Blind}{Alt up}
} Else Send {Space}
Return
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

04 Dec 2023, 06:36

Thank you!
only problem is, when I hold spacebar and quickly tap a letter, it sends the letter, not Alt+letter
therefore I would have to struggle and hold myself from quickly pressing the letter
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

04 Dec 2023, 06:45

The first script doesn't have this problem,
but it presses Alt while regular typing, when I press space, not hold it
therefore instead of typing a letter I fire a hotkey
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

04 Dec 2023, 08:29

Code: Select all

#Requires AutoHotkey v1.1.33
Global pressed := False
ih := InputHook("I"), ih.KeyOpt("{All}", "+N")
ih.OnKeyDown := Func("altSend")

Space::
ih.Start()
KeyWait Space
Return

Space Up::
ih.Stop(), pressed := False
If (ih.Input = "")
 Send {Space}
Return

altSend(ih, vk, sc) {
 Send % (pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}"
 If !pressed {
  Send {Alt up}
  pressed := True
 }
}
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

04 Dec 2023, 10:13

Thank you, tried this but it doesn't seem to send Alt at all...
I am even with On-Screen Keyboard and I don't see alt firing at all...
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

04 Dec 2023, 10:51

It works here. Check the :arrow: KeyHistory. Test in Notepad. I was able to activate the various menu items there.
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

05 Dec 2023, 00:52

I tested in notepad, couldn't activate alt using spacebar... also in no other software. I don't know why it works for you but not for me
GEV could you test it please?
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

05 Dec 2023, 00:58

I tested some more, it works only one time, after I run the script
and it doesn't work the second time and on
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

05 Dec 2023, 05:52

Close other scripts first. Use only this script with no changes and no other code.

What does your KeyHistory show?
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

06 Dec 2023, 05:17

I did close all the others...

002: pressed := False
003: ih := InputHook("I"), ih.KeyOpt("{All}", "+N")
004: ih.OnKeyDown := Func("altSend")
006: Return (8.61)
007: ih.Start()
008: KeyWait,Space (0.27)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.03)
019: if !pressed
020: Send,{Alt up} (0.02)
021: pressed := True
022: }
023: } (0.50)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (1.51)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.20)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.19)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.25)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.01)
019: if !pressed
023: } (0.16)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.01)
019: if !pressed
023: } (0.20)
012: ih.Stop(), pressed := False
013: if (ih.Input = "")
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

06 Dec 2023, 07:19

I would check your key history, which is not shown here. Do not use on-screen keyboard in your test. What key sequence are you testing in Notepad?
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

07 Dec 2023, 07:14

got it, here it is
I am pressing ctrl+Q over and over
the first time it fures alt+Q and then it fires just Q
spacebar keeps being pressed, I am not releasing it


002: pressed := False
003: ih := InputHook("I"), ih.KeyOpt("{All}", "+N")
004: ih.OnKeyDown := Func("altSend")
006: Return (11.84)
007: ih.Start()
008: KeyWait,Space (0.13)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.03)
019: if !pressed
020: Send,{Alt up} (0.01)
021: pressed := True
022: }
023: } (1.36)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.01)
019: if !pressed
023: } (0.36)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.01)
019: if !pressed
023: } (0.25)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.19)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.20)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.17)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.01)
019: if !pressed
023: } (0.22)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.19)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.20)
018: Send,(pressed ? "" : "{Alt down}") "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}" (0.02)
019: if !pressed
023: } (0.27)
012: ih.Stop(), pressed := False
013: if (ih.Input = "")
015: Return
009: Return (6.92)
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

07 Dec 2023, 07:16

Window: C:\Users\genad\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\spaceAlt.ahk - AutoHot
Keybd hook: yes
Mouse hook: no
Enabled Timers: 0 of 0 ()
Interrupted threads: 0
Paused threads: 0 of 0 (0 layers)
Modifiers (GetKeyState() now) =
Modifiers (Hook's Logical) =
Modifiers (Hook's Physical) =
Prefix key is down: no

NOTE: To disable the key history shown below, add the line "#KeyHistory 0" anywhere in the script. The same method can be used to change the size of the history buffer. For example: #KeyHistory 100 (Default is 40, Max is 500)

The oldest are listed first. VK=Virtual Key, SC=Scan Code, Elapsed=Seconds since the previous event. Types: h=Hook Hotkey, s=Suppressed (blocked), i=Ignored because it was generated by an AHK script, a=Artificial, #=Disabled via #IfWinActive/Exist, U=Unicode character (SendInput).

VK SC Type Up/Dn Elapsed Key Window
-------------------------------------------------------------------------------------------------------------
20 039 h d 2.75 Space снег.rtf [Compatibility Mode] - Word
51 010 s d 0.41 q
A4 038 i d 0.00 LAlt
51 010 i d 0.02 q
51 010 i u 0.00 q
A4 038 i u 0.02 LAlt
51 010 s u 0.16 q
51 010 s d 0.89 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.13 q
51 010 s d 0.11 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.08 q
51 010 s d 0.11 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.08 q
51 010 s d 0.09 q
51 010 i d 0.00 q
51 010 i u 0.02 q
51 010 s u 0.09 q
51 010 s d 0.11 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.11 q
51 010 s d 0.09 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.09 q
51 010 s d 0.09 q
51 010 i d 0.00 q
51 010 i u 0.00 q
51 010 s u 0.13 q
20 039 h u 0.13 Space
Press [F5] to refresh.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

08 Dec 2023, 10:09

Perhaps:

Code: Select all

#Requires AutoHotkey v1.1.33
Global pressed := False
ih := InputHook("I"), ih.KeyOpt("{All}", "+N")
ih.OnKeyDown := Func("altSend")

Space::
ih.Start()
KeyWait Space
Return

Space Up::
ih.Stop()
If (ih.Input = "")
     Send {Space}
Else Send {Alt up}
Return

altSend(ih, vk, sc) {
 If !GetKeyState("Alt")
  Send {Alt down}
 Send % "{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}"
}
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

11 Dec 2023, 05:54

this works well the first time (right after I run the script)
but after I release the spacebar and press it again, it doesn't work anymore
here is the sequence I am pressing: spacebar down, q, q, spacebar up, spacebar down, q, q, spacebar up
and this is what the script actually does:
20 039 h u 1.05 Space
A4 038 i u 0.00 LAlt
20 039 h d 0.45 Space
51 010 s d 0.45 q
51 010 s u 0.16 q
51 010 s d 0.22 q
51 010 s u 0.13 q
20 039 h u 0.09 Space
A4 038 i u 0.00 LAlt
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

11 Dec 2023, 10:06

Hmm, that's strange. Your key history is showing that your LAlt is not being sent down. I do not know why, because the script should send it down upon any key press, if Space is held. When I test in Notepad with Space+F+X, I see the following.

image231211-1005-001sm.png
Key history
image231211-1005-001sm.png (198.13 KiB) Viewed 706 times

Thus, I am not able to reproduce your problem. Others may have additional ideas for you here. Ensure that all other scripts are closed during your testing.

If you are holding modifier keys, or modifier keys are down, then the hotkey will not be triggered. This can typically be corrected by adding a wildcard (*) to the hotkeys.
genadil
Posts: 12
Joined: 28 Nov 2023, 09:11

Re: spacebar long press as modifier

12 Dec 2023, 04:15

there is a misunderstanding between us
the first time everything works fine, here is what I get the first time (right after I activate the script)

20 039 h d 0.03 Space
46 021 s d 0.02 f
A4 038 i d 0.00 LAlt
46 021 i d 0.01 f
46 021 i u 0.00 f
46 021 s u 0.11 f
58 02D d 0.64 x
58 02D i d 0.00 x
58 02D i u 0.00 x
58 02D u 0.11 x Notepad
20 039 h u 0.73 Space
A4 038 i u 0.00 LAlt

but this is what I get on the SECOND time (don't reload the script)
try it, I am sure you will see the same results on the second time

20 039 h d 17.16 Space *New Text Document.txt - Notepad
20 039 h d 0.50 Space
46 021 s d 0.02 f
46 021 s u 0.11 f
58 02D s d 0.55 x
58 02D s u 0.11 x
20 039 h u 0.16 Space
A4 038 i u 0.00 LAlt
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: spacebar long press as modifier

12 Dec 2023, 19:59

An improvement is below.

Code: Select all

#Requires AutoHotkey v1.1.33
ih := InputHook("IM"), ih.KeyOpt("{All}", "+N")
ih.OnKeyDown := Func("altSend")

Space::
ih.Start()
KeyWait Space
Return

*Space Up::
ih.Stop()
Send % ih.Input = "" ? "{Space}" : "{Alt up}"
Return

altSend(ih, vk, sc) {
 Send % GetKeyState("Alt") ? "" : "{Alt down}"
 Send % "{Blind}{" GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) "}"
}
An alternative is below, but I prefer the script above that uses InputHook.

Code: Select all

#Requires AutoHotkey v1.1.33

$Space::
$!Space::
Send {Alt down}
KeyWait Space
Send % "{Alt up}" (A_PriorKey = "Space" ? "{Esc} " : "")
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Bing [Bot], Descolada and 223 guests