Hold/press events from one key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Diomar
Posts: 3
Joined: 06 Jul 2022, 12:27

Hold/press events from one key

06 Jul 2022, 12:51

Hello.

When holding NumPad1, I need to hold down the spacebar. When pressing NumPad1, I need to press the x key. All this should work even if other keys are held/pressed.

I tried to implement it, however there are bugs.

Code: Select all

*NumPad1::
KeyWait, NumPad1, T0.3
if (ErrorLevel) {
	; If NumPad1 is held down
	Send {Space down}
	KeyWait, Space
	Send {Space up}
} else {
	; If NumPad1 is pressed
	Send {x}
}
return
Bugs:
- In notepad, holding NumPad1 prints a space very slowly.
- In notepad, holding NumPad1 can print the letter x at the end of a series of spacebars.
- In notepad, holding NumPad1 and pressing any other key will stop printing the spacebar.
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Hold/press events from one key

06 Jul 2022, 20:28

Welcome to this AutoHotkey forum!

Code: Select all

Numpad1::
KeyWait, Numpad1, T.3
If !ErrorLevel
 Send x
Else While GetKeyState("Numpad1", "P")
 Send {Space}
Return
Diomar
Posts: 3
Joined: 06 Jul 2022, 12:27

Re: Hold/press events from one key

07 Jul 2022, 01:38

When holding NumPad1, your code sends continuous spacebar presses, but I need to hold down the spacebar. I tried to make edits and they do not work: in notepad, a space is printed once. Do you have any ideas how to fix this?

Code: Select all

Numpad1::
KeyWait, Numpad1, T.3
If !ErrorLevel
	Send {x}
Else {
	Send {Space down}
	While GetKeyState("Numpad1", "P") {
		
	}		
	Send {Space up}
}
Return
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Hold/press events from one key

07 Jul 2022, 05:52

Code: Select all

Numpad1::
KeyWait, Numpad1, T.3
If ErrorLevel { ; Held
 Send {Space down}
 KeyWait, Numpad1
 Send {Space up}
} Else Send x
Return
Diomar
Posts: 3
Joined: 06 Jul 2022, 12:27

Re: Hold/press events from one key

07 Jul 2022, 10:07

mikeyww wrote:
07 Jul 2022, 05:52

Code: Select all

Numpad1::
KeyWait, Numpad1, T.3
If ErrorLevel { ; Held
 Send {Space down}
 KeyWait, Numpad1
 Send {Space up}
} Else Send x
Return
When holding NumPad1 in notepad, your code only prints a space once and then nothing happens.
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Hold/press events from one key

07 Jul 2022, 10:16

Yep, that's what it does.
When holding NumPad1, I need to hold down the spacebar. When pressing NumPad1, I need to press the x key.
image220707-1126-001_cr.png
Key history
image220707-1126-001_cr.png (18.01 KiB) Viewed 605 times
User avatar
morrischris
Posts: 2
Joined: 15 Aug 2022, 23:57

Re: Hold/press events from one key

16 Aug 2022, 02:12

Diomar wrote:
07 Jul 2022, 01:38
When holding NumPad1, your code sends continuous spacebar presses [Mod edit: URL removed], but I need to hold down the spacebar. I tried to make edits and they do not work: in notepad, a space is printed once. Do you have any ideas how to fix this?
I think the 'Send {Space down}' should go inside the While statement in order to make the 'spacebar hold' work. Try this.

Code: Select all

Numpad1::
KeyWait, Numpad1, T.3
If !ErrorLevel
	Send {x}
Else {
	While GetKeyState("Numpad1", "P") {
		Send {Space down}
	}		
	Send {Space up}
}
Return
Last edited by gregster on 16 Aug 2022, 04:15, edited 1 time in total.
Reason: Spam link removed.
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Hold/press events from one key

16 Aug 2022, 03:20

morrischris wrote: I think the 'Send {Space down}' should go inside the While statement in order to make the 'spacebar hold' work.
No, that is not correct. That would keep sending Space down keypresses. It does not belong inside the While loop in order to hold the spacebar.
gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Hold/press events from one key

16 Aug 2022, 04:18

@morrischris, this is a warning: If you continue to spam that link, you will be banned immediately.
Actually, altering a quote to hide that link is already a bannable offense. Consider this your lucky day.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 96 guests