Re enter the last strings on keypress

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ktbjx
Posts: 71
Joined: 11 Jul 2015, 23:53

Re enter the last strings on keypress

19 Sep 2017, 21:09

is it possible to input the last numbers when i hit NumpadEnter?
i found this code but it only record the last number...

Code: Select all

Loop
     Input, LastKey, L1 V


$NumpadEnter::
        Send, %LastKey%`n
Return
what i want is to enter the whole series of numbers..
example: i pressed 15 and hit NumpadEnter,
i want it to remember the number 15. so when i hit numpadenter again, it will put 15
like this:
15
15
2.1
2.1
1
1

basically i want it to remember the last keystrokes for me to use it again
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Re enter the last strings on keypress

19 Sep 2017, 21:23

So your Input is limited by Length 1.

However, Input is probably a tough way to go about this.

What I would do personally is create hotkeys for the numbers (Numpad0-9) and the decimal (NumpadDot) which use the ~ modifier. (See KeyList) Each press, they concatenate to the end of the string (see the .= operator); such as if we use the variable string we'd use string.="1" or string.=".". (You could optionally stack Numpad0-9 hotkeys and just use string.=SubStr(A_ThisHotkey,0) to capture the last character.)

Then what I would do is have $NumpadEnter:: first check for the variable string having a value (explained a bit further why). If it does have a value, then what we should do is copy it to a new variable and then clear out string with string:="". Then always have NumpadEnter send the value of the second variable. This way, string is reset so that if you start typing a new number, it's not tacked onto the previous number. And in this way, NumpadEnter can check if there is a new number to use or not -- if there is not, string will be blank, so it won't overwrite a new value to this second variable.
ktbjx
Posts: 71
Joined: 11 Jul 2015, 23:53

Re: Re enter the last strings on keypress

19 Sep 2017, 22:34

Exaskryz wrote:So your Input is limited by Length 1.

However, Input is probably a tough way to go about this.

What I would do personally is create hotkeys for the numbers (Numpad0-9) and the decimal (NumpadDot) which use the ~ modifier. (See KeyList) Each press, they concatenate to the end of the string (see the .= operator); such as if we use the variable string we'd use string.="1" or string.=".". (You could optionally stack Numpad0-9 hotkeys and just use string.=SubStr(A_ThisHotkey,0) to capture the last character.)

Then what I would do is have $NumpadEnter:: first check for the variable string having a value (explained a bit further why). If it does have a value, then what we should do is copy it to a new variable and then clear out string with string:="". Then always have NumpadEnter send the value of the second variable. This way, string is reset so that if you start typing a new number, it's not tacked onto the previous number. And in this way, NumpadEnter can check if there is a new number to use or not -- if there is not, string will be blank, so it won't overwrite a new value to this second variable.
Thank you!!!!
i didnt get it at first, but i get i hope i did it right?!

Code: Select all

#Persistent
#SingleInstance, Force
#NoEnv
TrayTip, ReType Enabled!, Right click for`n more options, 20, 17
SetBatchLines -1
SetKeyDelay, 50
LastKey=
Boolean = True

~Numpad1::
if Boolean = False
{
LastKey = 
}
LastKey.="1"
Boolean = True
Return

~Numpad2::
if Boolean = False
{
LastKey = 
}
LastKey.="2"
Boolean = True
Return

~Numpad3::
if Boolean = False
{
LastKey = 
}
LastKey.="3"
Boolean = True
Return

~Numpad4::
if Boolean = False
{
LastKey = 
}
LastKey.="4"
Boolean = True
Return

~Numpad5::
if Boolean = False
{
LastKey = 
}
LastKey.="5"
Boolean = True
Return

~Numpad6::
if Boolean = False
{
LastKey = 
}
LastKey.="6"
Boolean = True
Return

~Numpad7::
if Boolean = False
{
LastKey = 
}
LastKey.="7"
Boolean = True
Return

~Numpad8::
if Boolean = False
{
LastKey = 
}
LastKey.="8"
Boolean = True
Return

~Numpad9::
if Boolean = False
{
LastKey = 
}
LastKey.="9"
Boolean = True
Return

~Numpad0::
if Boolean = False
{
LastKey = 
}
LastKey.="0"
Boolean = True
Return

~NumpadDot::
if Boolean = False
{
LastKey = 
}
LastKey.="."
Boolean = True
Return

$NumpadEnter::
if boolean = True
{
Boolean = False
Send, `n
}
else
Send, %LastKey%`n

Return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Re enter the last strings on keypress

20 Sep 2017, 00:38

If it seems to be working, that's great.

It wasn't exactly what I had in mind, but if it is functional for what you have, that's what matters.

If you were curious, what I had in mind was something like this for your numbers and Dot.

Code: Select all

~Numpad1::
LastKey.="1"
return
And then for your NumpadEnter, I was thinking you'd do this:

Code: Select all

$NumpadEnter::
If LastKey
{
LastKeycopy:=LastKey
LastKey:=""
}
Send %LastKeyCopy%`n
Return
You'll notice I shift the clearing of the LastKey to the NumpadEnter, not in each of the Number and Dot hotkeys.

But this behaves differently from the code you just wrote. In your case, you get to repeat the number one time. In my code, you can repeat the number as many times as you want with every press of NumpadEnter. Whichever behavior you want is totally up to you and neither is wrong.

But to match your behavior, I would use this:

Code: Select all

$NumpadEnter::
If LastKey
{
Send %LastKey%`n
LastKey:=""
}
else
Send `n
return
That will let NumpadEnter repeat the previous number one time, and then it'll send the Enter character `n otherwise. You could actually rewrite this, because it is OK to have a blank value in a variable in the Send command:

Code: Select all

$NumpadEnter::
Send %LastKey%`n
LastKey:=""
return
If NumpadEnter is pressed two times in a row, then on the second press LastKey will be empty (from the first press) and all that comes out from that is just the `n.

Again, if your code works, no need to make any changes. You can take what you want from my examples and explanations.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: sachinme and 356 guests