Problems with own SendInput function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Problems with own SendInput function

03 Dec 2017, 12:47

Hi there,

some time ago I've written a SendInput() function (but it is not solely written by me) that I now have problems with. I did never use it in this constellation and thus never recognized the problems. The function has the purpose to introduce delay to the SendInput command.

However I am pretty sure there are no bugs left in my function and the problem might be AHK's fault. The problem occurs, when I keep the hotkey pressed and do not release it while the text is written.

Here is the code of the function (and others that are used in the first one):

Code: Select all

SendInput(keydelay:=10, string:="") {
	if (keydelay < 0) {
		SendInput, %string%
	} else {
		modifiers := []	
		Loop, Parse, string
		{			
			if (!brace) {
				modifier := RegExMatch(A_LoopField, "[\^!+#]", curr_modifier)
				modifiers.Push(curr_modifier)
			} if (modifier) {
				continue
			} else if (brace) {
				if (IsNum(A_LoopField) && space) {
					number .= A_LoopField
					continue
				} else if (A_LoopField = "}" && number > 0) {
					curr_key := SubStr(curr_key, 1, StrLen(curr_key) - 1) "}"
					Loop, %number% {
						SendInput, % modifiers[1] modifiers[2] modifiers[3] modifiers[4] curr_key
						Sleep(keydelay)
					} modifiers := [], brace := space := number := false
				} else if (A_LoopField = "}") {
					curr_key .= "}"
					SendInput, % modifiers[1] modifiers[2] modifiers[3] modifiers[4] curr_key
					Sleep(keydelay), modifiers := [], brace := space := false
				} else if (A_LoopField = A_Space && curr_key <> "{") {
					curr_key .= A_LoopField
					space := true
				} else {
					curr_key .= A_LoopField
					continue
				}
			} else if (A_LoopField = "{") {
				brace := A_Index
				curr_key := "{"
				continue
			} else {
				SendInput, % modifiers[1] modifiers[2] modifiers[3] modifiers[4] A_LoopField
				Sleep(keydelay), modifiers := []
			}
		}
	}
}

Sleep(delay) {
	Sleep, delay
}

IsNum(var) {
	if var is number
		return true
	return false
}
When I use the following code I have several unwanted m letters between the text and furthermore Alt gets pressed.

Code: Select all

!m:: SendInput(0, "This is a text to show the problem.")
When I use the SendInput command this partially also happens:

Code: Select all

!m:: SendInput, This is a text to show the problem.
Now Alt does not get pressed, but there are also several unwanted m letters.


So basically the keys of the hotkey get pressed, although they should get blocked. Am I doing something wrong or is this a problem of AHK, as ist also happens with the command? Of course the Alt key gets pressed for every single letter/key by design in my function (a delay would not be possible otherwise), so it might be, that AHK is not able to block all those keypresses?

I am using Win10 64-bit, AHK Unicode 64-bit.


Edit: I have another example with the SendInput command that also generates Alt keystrokes.

Code: Select all

^!m:: SendInput, This is a text to show the problem.
When I activate this hotkey multiple times or keep the keys pressed, it sends the Alt key.
Last edited by Leli196 on 08 Dec 2017, 11:51, edited 1 time in total.
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Probles with own SendInput function

07 Dec 2017, 15:29

Does nobody have an idea or might test the code? If it also occurs at other computers I would like to post a bug report.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Probles with own SendInput function

07 Dec 2017, 15:35

- What happens if you use a different hotkey?
You could try SendInput, {Alt Up}, and possibly {Ctrl Up}, to release the keys that triggered the hotkey.
- I thought I'd mention this, even though it might not be relevant:
Send command sends ctrl unintentionally - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40799
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Probles with own SendInput function

08 Dec 2017, 02:32

When you hold a character, Windows re-sends the character until it's released, at a rate which may be set in the control panel. Your best bet would be to do what jeeswg suggested and send all the hotkey characters up:

Code: Select all

!m:: SendInput(0, "{alt up}{m up}This is a text to show the problem.")
Or if your function doesn't accomodate for that:

Code: Select all

!m::
sendInput {alt up}{m up}
SendInput(0, "This is a text to show the problem.")
return
I would also suggest allowing your function to send blind (maybe raw as well?), either by default or by param.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Probles with own SendInput function

08 Dec 2017, 11:58

jeeswg wrote:What happens if you use a different hotkey?
It is always the same when I use a hotkey with a modifier, e. g. ^j, ^!o, !a, #4, +r and also !+, +#, !,.
jeeswg wrote:You could try SendInput, {Alt Up}, and possibly {Ctrl Up}, to release the keys that triggered the hotkey.
Masonjar13 wrote:When you hold a character, Windows re-sends the character until it's released, at a rate which may be set in the control panel. Your best bet would be to do what jeeswg suggested and send all the hotkey characters up:
This does not change anything. Even with {Alt up} there are Alt keypresses. And I am not talking about my function, the problem remains even with

Code: Select all

!m:: SendInput, {Alt up}{m up}This is a text to show the problem.
jeeswg wrote:I thought I'd mention this, even though it might not be relevant:
Send command sends ctrl unintentionally - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40799
Well, this is also my topic as I was bugged by the circles around the cursor (I use the Windows option to show it when pressing ctrl), but this is something different from this.
Masonjar13 wrote:I would also suggest allowing your function to send blind (maybe raw as well?), either by default or by param.
I have a separate funtion for this: SendRaw([send_mode=*event/play/input], [keydelay=10], [keypress=10], string)

Edit: And shouldn't AutoHotkey block input to Windows when the input is a hotkey? When I press the hotkey !m neither of the keypresses should be send but blocked, even when I keep them pressed.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Problems with own SendInput function

08 Dec 2017, 19:41

Modifiers are not blocked, no. It should be blocked if you use the actual key, though: LAlt & m:: You can also try using BlockInput while the hotkey is running.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Problems with own SendInput function

09 Dec 2017, 12:23

But modifiers aren't repeated like letters etc. Even if I keep the modifier pressed it should just be kept pressed instead of being released and repeatedly pressed.
Additionally, what about the letters? They also get repeatedly send.

Btw of course the problem does not occur with LAlt & m::, as I have to release both keys and press them again, as the auto-repeat is missing.

Edit: Something like this is not possible, there are several unwanted letters in the second text.

Code: Select all

!m::
KeyWait, % SubStr(A_ThisHotkey, 2), T0.45
if (A_TimeSinceThisHotkey < 450)
	SendInput, This is a text to show the problem 1
else
	SendInput, This is a text to show the problem 2
KeyWait, % SubStr(A_ThisHotkey, 2)
Return
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Problems with own SendInput function

14 Dec 2017, 19:17

Doesn't anybody else have something to say regarding this problem? It cannot be alright, that this hotkey

Code: Select all

!m:: SendInput, This is a text to show the problem.
creates this unusable text (when hold down)

Code: Select all

This is a text to show the problem.Thmis mis a tmext tmo show tmhe promblem.Thmis is ma text mto show mthe probmlem.Thism is a tmext to mshow thme problem.
The text is literally flooded with m letters that do not belong there. Additionally the Alt key gets pressed several times, but this is not visible in the text, obviously.
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Problems with own SendInput function

19 Dec 2017, 07:37

Try this:
  • Make sure the hotkey uses the hook; e.g. $!m::. Registered hotkeys (which the OS is entirely responsible for detecting) are finicky, especially if you mess with the modifier key state. SendInput does that implicitly, because otherwise it would not be able to produce the text you requested.
  • If you have other scripts running which have a keyboard hook installed, close them. Otherwise, SendInput is not SendInput, but really SendEvent in disguise (as documented). Close all other scripts to be sure.
SendInput sends Alt down after it finishes because if it doesn't, Alt will remain in the logically "not pressed" state. It would then not work as the user expected, if the user is still physically holding down the key.
Nellybird

Re: Problems with own SendInput function

19 Dec 2017, 07:40

lexikos wrote:Try this:
  • Make sure the hotkey uses the hook; e.g. $!m::. Registered hotkeys (which the OS is entirely responsible for detecting) are finicky, especially if you mess with the modifier key state. SendInput does that implicitly, because otherwise it would not be able to produce the text you requested.
  • If you have other scripts running which have a keyboard hook installed, close them. Otherwise, SendInput is not SendInput, but really SendEvent in disguise (as documented). Close all other scripts to be sure.
SendInput sends Alt down after it finishes because if it doesn't, Alt will remain in the logically "not pressed" state. It would then not work as the user expected, if the user is still physically holding down the key.
hey, sorry to bother but I am using part of a script you wrote and I need help. can you please take a look when you can?

https://autohotkey.com/boards/viewtopic.php?f=5&t=41563

The suggestion made by others I cannot make work :headwall:
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Problems with own SendInput function

25 Dec 2017, 12:01

@lexikos

Thanks for the reply. However, closing all other scripts and/or using the hook did not help in any way. There are still many wrong m and even µ symbols.
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Problems with own SendInput function

02 May 2018, 23:34

BTW, it is a bit late, but thanks for the update!
Leli196
Posts: 215
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Problems with own SendInput function

27 Nov 2018, 13:47

Hi there,

the problem indeed got fixed. Now I have another problem that bothers me and I think this might be the best place to ask for help, as it is related with my own SendInput function. The code of the function is provided in the first post.

This code works perfectly fine:

Code: Select all

#MenuMaskKey vkFF
#UseHook

!x:: SendInput(10, "t@e")
This however creates several unwanted LControl keypresses: (I have the Windows option to show the cursor position when Ctrl is pressed active and it therefore is very annoying.)

Code: Select all

#MenuMaskKey vkFF
#UseHook

x:: SendInput(10, "t@e")
Key History:

Code: Select all

54  014	i	d	0.00	t              	
54  014	i	u	0.00	t              	
A2  01D	i	d	0.01	LControl       	
A5  138	i	d	0.00	RAlt           	
51  010	i	d	0.00	q              	
51  010	i	u	0.00	q              	
A2  01D	i	u	0.00	LControl       	
A5  138	i	u	0.00	RAlt           	
A2  01D	i	d	0.00	LControl       	
A2  01D	i	u	0.02	LControl       	
45  012	i	d	0.02	e              	
70  03B	s	u	0.00	F1             	
45  012	i	u	0.00	e              	
So with modificator keys like Alt, Shift or Win the Ctrl key is not pressed, but without them it is.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, garry, Google [Bot], Rohwedder and 171 guests