Simple memory of lastpress Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Simple memory of lastpress

Post by blad4 » 16 Aug 2022, 08:37

Using numbers via Numpad, i.e.

Code: Select all

"Numpad1"
for 1,

Code: Select all

"Numpad5"
for 5,

Code: Select all

"Numpad1 & Numpad3"
for 13

What is the simplest way to

Code: Select all

Numpaddot::
Send "last sent Numpad command"

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simple memory of lastpress

Post by Rohwedder » 16 Aug 2022, 08:55

Hallo,
Numpad1:: for 1 and
Numpad1 & Numpad3:: for 13 ?
Show the Numpad command lines of your script.

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 16 Aug 2022, 08:58

Code: Select all

Numpad1::
ch=1
gosub sendroutine
return

Numpad2::
ch=9
gosub sendroutine
return

...


Numpad1 & Numpad2::
ch=122
gosub sendroutine
return

Numpad1 & Numpad3::
ch=137
gosub sendroutine
return

etc...



Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simple memory of lastpress

Post by Rohwedder » 16 Aug 2022, 09:09

Try:

Code: Select all

last_sent_Numpad_command  = Numpaddot
Numpad1::
ch=1
gosub sendroutine
return

Numpad2::
ch=9
gosub sendroutine
return

; ...


Numpad1 & Numpad2::
ch=122
gosub sendroutine
return

Numpad1 & Numpad3::
ch=137
gosub sendroutine
return

Numpaddot::
IF (last_sent_Numpad_command <> A_ThisHotkey)
	Goto, %last_sent_Numpad_command%
Return

sendroutine:
IF InStr(A_ThisHotkey, "Numpad")
	last_sent_Numpad_command := A_ThisHotkey
SoundBeep, 4000, 20
ToolTip,% ch
;...
Return
Last edited by Rohwedder on 16 Aug 2022, 09:18, edited 1 time in total.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Simple memory of lastpress

Post by gregster » 16 Aug 2022, 09:15

Didn't run it yet, but aren't parentheses missing here ? (Or percent symbols on the right side of the operator?)

Code: Select all

IF last_sent_Numpad_command <> A_ThisHotkey

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simple memory of lastpress

Post by Rohwedder » 16 Aug 2022, 09:20

Yes, parentheses were missing.

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 17 Aug 2022, 19:32

Thank you Rohwedder, but I'm so baffled. In practice it keeps adding +1 to ch??

I'm obviously using

Code: Select all

 IF InStr 
completely wrong!!

Here is my routine:

Code: Select all

sendroutine:
if ch between 0 and 8
{
IF InStr(A_ThisHotkey, "Numpad")
	last_sent_Numpad_command := A_ThisHotkey
send ^g
send %ch%
send {enter}
return
}
if ch>8
{
IF InStr(A_ThisHotkey, "Numpad")
	last_sent_Numpad_command := A_ThisHotkey
send ^g
send %ch%
send {enter}
send {f9 3}
return
}
return

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simple memory of lastpress

Post by Rohwedder » 18 Aug 2022, 01:05

it keeps adding +1 to ch?
Here with this code this does not happen:

Code: Select all

last_sent_Numpad_command  = Numpaddot
Numpad1::
ch=1
gosub sendroutine
return
Numpad2::
ch=9
gosub sendroutine
return
; ...
Numpad1 & Numpad2::
ch=122
gosub sendroutine
return
Numpad1 & Numpad3::
ch=137
gosub sendroutine
return
Numpaddot::
IF (last_sent_Numpad_command <> A_ThisHotkey)
	Goto, %last_sent_Numpad_command%
Return
sendroutine:
if ch between 0 and 8
{
	IF InStr(A_ThisHotkey, "Numpad")
		last_sent_Numpad_command := A_ThisHotkey
	send ^g
	send %ch%
	send {enter}
	return
}
if ch>8
{
	IF InStr(A_ThisHotkey, "Numpad")
		last_sent_Numpad_command := A_ThisHotkey
	send ^g
	send %ch%
	send {enter}
	send {f9 3}
	return
}
return
The Numpad+Number Hotkeys each send the ch value + Enter, Numpaddot:: repeats this.

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 18 Aug 2022, 09:00

Still the same issue. But what is the difference in the last code you sent? It seems letter for letter the same as what I had before?

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Simple memory of lastpress

Post by Rohwedder » 18 Aug 2022, 10:49

There should be no difference.
Read what I wrote about it. This "it keeps adding +1 to ch" does not happen here with that script!

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Simple memory of lastpress

Post by Xtra » 18 Aug 2022, 11:04

Something different:

Code: Select all

#NoEnv

Numpad1::sendroutine(last_sent_Numpad_command := "Numpad1")
Numpad2::sendroutine(last_sent_Numpad_command := "Numpad2")
Numpad1 & Numpad2::sendroutine(last_sent_Numpad_command := "Numpad1 & Numpad2")
Numpad1 & Numpad3::sendroutine(last_sent_Numpad_command := "Numpad1 & Numpad3")

NumpadDot::
    if (last_sent_Numpad_command)
	    Goto, %last_sent_Numpad_command%
return

sendroutine(A_Hotkey) {
    static ch := {"Numpad1":1,"Numpad2":9,"Numpad1 & Numpad2":122,"Numpad1 & Numpad3":137}
	send ^g
	send % n := ch[A_Hotkey]
	send {enter}
	if (n > 8)
        send {f9 3}
}

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 18 Aug 2022, 18:39

Rohwedder wrote:
18 Aug 2022, 01:05
it keeps adding +1 to ch?
Here with this code this does not happen:

Code: Select all

last_sent_Numpad_command  = Numpaddot
Numpad1::
ch=1
gosub sendroutine
return
Numpad2::
ch=9
gosub sendroutine
return
; ...
Numpad1 & Numpad2::
ch=122
gosub sendroutine
return
Numpad1 & Numpad3::
ch=137
gosub sendroutine
return
Numpaddot::
IF (last_sent_Numpad_command <> A_ThisHotkey)
	Goto, %last_sent_Numpad_command%
Return
sendroutine:
if ch between 0 and 8
{
	IF InStr(A_ThisHotkey, "Numpad")
		last_sent_Numpad_command := A_ThisHotkey
	send ^g
	send %ch%
	send {enter}
	return
}
if ch>8
{
	IF InStr(A_ThisHotkey, "Numpad")
		last_sent_Numpad_command := A_ThisHotkey
	send ^g
	send %ch%
	send {enter}
	send {f9 3}
	return
}
return
The Numpad+Number Hotkeys each send the ch value + Enter, Numpaddot:: repeats this.
With this exact code, and when ch<9, numpaddot increases ch by 1. When ch>9, it increases ch by 4

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 18 Aug 2022, 18:40

Xtra wrote:
18 Aug 2022, 11:04
Something different:

Code: Select all

#NoEnv

Numpad1::sendroutine(last_sent_Numpad_command := "Numpad1")
Numpad2::sendroutine(last_sent_Numpad_command := "Numpad2")
Numpad1 & Numpad2::sendroutine(last_sent_Numpad_command := "Numpad1 & Numpad2")
Numpad1 & Numpad3::sendroutine(last_sent_Numpad_command := "Numpad1 & Numpad3")

NumpadDot::
    if (last_sent_Numpad_command)
	    Goto, %last_sent_Numpad_command%
return

sendroutine(A_Hotkey) {
    static ch := {"Numpad1":1,"Numpad2":9,"Numpad1 & Numpad2":122,"Numpad1 & Numpad3":137}
	send ^g
	send % n := ch[A_Hotkey]
	send {enter}
	if (n > 8)
        send {f9 3}
}
Thanks, but because this does not affect ch again, gives me problems.

Example, pressed Numpad2... then NumpadDot, ch should now become 9 to replicate the original Numpad2 press again.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Simple memory of lastpress

Post by Xtra » 19 Aug 2022, 01:53

Did you even try it? Press any hotkey and NumpadDot will repeat the last used hotkey.
Are you are using ch somewhere else in your script that you are not showing here?

If you wanted something else please explain what that is so anyone answering here can understand.

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 19 Aug 2022, 06:15

Yes of course I applied it to my script and tried it, and yes using ch for many different things in the script, that is why the functionality needs to affect ch.

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 19 Aug 2022, 06:17

Maybe a better way to explain:

Code: Select all

NumpadX::
ch=Y
gosub sendroutine
return

Somewhere we hold the memory of X

NumpadDot repress that NumPadX

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Simple memory of lastpress  Topic is solved

Post by Xtra » 19 Aug 2022, 12:22

The ch value can be returned from the function and saved to a var named ch. Then it can be used in parts of your script not shown here.

Simplified the script some:

Code: Select all

#NoEnv

Numpad1::
Numpad2::
Numpad1 & Numpad2::
Numpad1 & Numpad3::ch := sendroutine(last_sent_Numpad_command := A_ThisLabel)

NumpadDot::
    if (last_sent_Numpad_command)
	    Goto, %last_sent_Numpad_command%
return

sendroutine(A_Hotkey) {
    static chArr := {"Numpad1":1,"Numpad2":9,"Numpad1 & Numpad2":122,"Numpad1 & Numpad3":137}
	send ^g
	send % n := chArr[A_Hotkey]
	send {enter}
	if (n > 8)
        send {f9 3}
	return n
}

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 19 Aug 2022, 18:27

Works a charm. Thank you Xtra and thank you Rohwedder (and sorry to you also for my confusion).

blad4
Posts: 307
Joined: 07 Oct 2015, 11:06

Re: Simple memory of lastpress

Post by blad4 » 31 Aug 2022, 17:56

May I ask how to do more than one thing from the numpad press, for example:

Code: Select all

sendcharts(A_Hotkey) {
    static chArr := {"Numpad1":1,"Numpad2":3,"Numpad3":15,"Numpad4":27,"Numpad5":39,"Numpad6":51,"Numpad7":63}
	send ^g
	send % n := chArr[A_Hotkey]
	send {enter}
	;;;;;;;;;;;; add one to n.. (n+1), then send % n again
	send ^g
	send % n := chArr[A_Hotkey]
	send {enter}

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Simple memory of lastpress

Post by Xtra » 31 Aug 2022, 22:03

send % n+1 ; add one to n.. (n+1), then send % n again

Post Reply

Return to “Ask for Help (v1)”