Use numbers as Numeric Pad for ASCII conversion Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DaisyFrancoForte
Posts: 2
Joined: 29 Nov 2022, 08:16

Use numbers as Numeric Pad for ASCII conversion

Post by DaisyFrancoForte » 29 Nov 2022, 08:24

Hi guys,
I've recently brought a laptop which hasn't numeric pad nor the possibility to activate numblock, etc.
I've seen Autohotkey as a possible solution, but I couldn't find script for this job.

So I've written a script code working in this way: while pressing LALt, you can insert any number you want, and then it's converted in ascii.
This code now works, but there's some issues:
1. It receives any input, even though I'd like to strict it only on numbers.
2. Even if the LALt is not pressed, it doesn't send the converted number right away. You have to press another key and it will be sent.

I'd like to fix it and change it so when you don't hold LAlt anymore it will automatically send the code. The problem is I'm new to this language and I'm not really sure how to implement it.
Every bit of help is appreciated ^^

Code: Select all

~LAlt::
	code:=""
    while GetKeyState("LAlt")
    {
	Input, key, L1 M
	code:=code . key
        Sleep, 50
    }
	Send, % Chr(code)

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Use numbers as Numeric Pad for ASCII conversion  Topic is solved

Post by teadrinker » 29 Nov 2022, 11:22

Try this:

Code: Select all

Hook := InputHook("V")
Hook.KeyOpt("0123456789", "NS")
Hook.OnKeyUp := Func("OnKeyUp").Bind(Keys := [""])
Return

~LAlt::
   Hook.Start()
   KeyWait, LAlt
   Hook.Stop()
   if (Keys[1] != "")
      Send % "{Alt Down}" . Keys[1] . "{Alt Up}"
   Keys[1] := ""
   Return

OnKeyUp(Keys, Hook, vk, sc) {
   Keys[1] .= "{Numpad" . GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) . "}"
}
The script will decode the set of numeric keys exactly like Alt + Numpad Keys does it.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Use numbers as Numeric Pad for ASCII conversion

Post by mikeyww » 29 Nov 2022, 11:24

I was posting at the same time as teadrinker....

Code: Select all

LAlt::
SoundBeep, 1500
While GetKeyState("LAlt", "P")
 Input, key, M
Send % Chr(RegExReplace(key, "\D"))
LAlt Up::Input

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Use numbers as Numeric Pad for ASCII conversion

Post by teadrinker » 29 Nov 2022, 11:30

@mikeyww
With your option, Alt loses its functionality. Try Alt + F4, for example.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Use numbers as Numeric Pad for ASCII conversion

Post by mikeyww » 29 Nov 2022, 11:40

Your point is well taken, though I addressed only the non-working issues!
This code now works, but there's some issues:
1. It receives any input, even though I'd like to strict it only on numbers.
2. Even if the LALt is not pressed, it doesn't send the converted number right away. You have to press another key and it will be sent.
I admit that I probably created a new problem by removing that tilde! :)

Bottom line: use teadrinker's scripts!

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Use numbers as Numeric Pad for ASCII conversion

Post by teadrinker » 29 Nov 2022, 11:45

Yep, but in the initial code, OP uses ~Alt. As I understand, he wanted to keep Alt's capabilities.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Use numbers as Numeric Pad for ASCII conversion

Post by mikeyww » 29 Nov 2022, 11:49

You're right. I hadn't tried that earlier.

DaisyFrancoForte
Posts: 2
Joined: 29 Nov 2022, 08:16

Re: Use numbers as Numeric Pad for ASCII conversion

Post by DaisyFrancoForte » 29 Nov 2022, 14:09

Thanks everyone!
Teadrinker's code works perfectly, and it still maintains combinations such as Alt + F4. It was exactly as I imagined it.
I'm sorry if my answer delayed but I was at uni.

Thanks again!

rubah
Posts: 1
Joined: 16 Jan 2024, 00:57

Re: Use numbers as Numeric Pad for ASCII conversion

Post by rubah » 16 Jan 2024, 01:03

teadrinker wrote:
29 Nov 2022, 11:22
Try this:

Code: Select all

Hook := InputHook("V")
Hook.KeyOpt("0123456789", "NS")
Hook.OnKeyUp := Func("OnKeyUp").Bind(Keys := [""])
Return

~LAlt::
   Hook.Start()
   KeyWait, LAlt
   Hook.Stop()
   if (Keys[1] != "")
      Send % "{Alt Down}" . Keys[1] . "{Alt Up}"
   Keys[1] := ""
   Return

OnKeyUp(Keys, Hook, vk, sc) {
   Keys[1] .= "{Numpad" . GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) . "}"
}
The script will decode the set of numeric keys exactly like Alt + Numpad Keys does it.
I wanted to share that I appreciate this script. I have a Lenovo Yoga with no numpad, and it's pretty silly to not have a way to easily access these keys.

I did need to make some modifications to get this to run with AHK 2.0.11:

Code: Select all

#Requires AutoHotkey v2.0
Hook := InputHook("V")
Hook.KeyOpt("0123456789", "NS")
Hook.OnKeyUp := OnKeyUp.Bind(Keys := [""])
Return

~LAlt::{
   Hook.Start()
   KeyWait "LAlt"
   Hook.Stop()
   if (Keys[1] != "")
      Send "{Alt Down}" . Keys[1] . "{Alt Up}"
   Keys[1] := ""
   Return
}
OnKeyUp(Keys, Hook, vk, sc) {
   Keys[1] .= "{Numpad" . GetKeyName(Format("vk{:x}sc{:x}", vk, sc)) . "}"
}
The difference is minor, but without any prior experience with autohotkey, it took some time to figure out. To summarize, I added the Force 2.0 directive so that it would show me debug instead of prompting to install v1; quote the name of LAlt in KeyWait, change the reference to OnKeyUp when adding the bound parameter, remove the % after Send to force expression evaluation.

Thank you again, teadrinker!

Post Reply

Return to “Ask for Help (v1)”