Numpad on TKL keyboard

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bhaules
Posts: 3
Joined: 15 Dec 2021, 03:55

Numpad on TKL keyboard

20 Mar 2024, 03:37

I've been using a v1 code to turn on "numpad Mode" on my TKL keyboard.
I've been trying to convert it to v2 (using a converter and then trying my best) but I'm still not able to get it fully working.
This is what I have so far:

Code: Select all

;=============================================================================================
; Left Alt + Caps Lock turns on Num Pad Mode
;=============================================================================================
; This script enables you to overlay a numpad on your tkl keyboard as shown below.
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|
; | [7] [8] [9] [0]    |  ----> | [7] [8] [9] [/]    | ; | [2] [3] [4] [5]    |  ----> | [9] [8] [7] [/]    |
; |  [u] [i] [o] [p]   |  ----> |  [4] [5] [6] [*]   | ; |  [w] [e] [r] [t]   |  ----> |  [6] [5] [4] [*]   |
; |   [j] [k] [l] [;]  |  ----> |   [1] [2] [3] [-]  | ; |   [s] [d] [f] [g]  |  ----> |   [3] [2] [1] [-]  |
; |    [m] [,] [.] [/] |  ----> |    [0] [0] [.] [+] | ; |    [x] [c] [c] [b] |  ----> |    [0] [0] [.] [+] |
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|

SetNumLockState True         ;Turns on numlock when the script is first executed. With it off, you won't be able to input numbers.

nmsDisp(text, ms, icon_no ){
	myGui := Gui()
	myGui.Opt("+AlwaysOnTop +ToolWindow -SysMenu -Caption")
	myGui.BackColor := "ffffff" ;changes background color
	myGui.SetFont("c000000 s20 bold", "Verdana") ;changes font color, size and font
	myGui.Add("Text", "x0 y0", text) ;the text to display
	myGui.Title := "Xn: 0"
	myGui.Show("NoActivate")

	Sleep(ms)
	myGui.Destroy()

	TraySetIcon("Shell32",icon_no,"1")
}

<!Capslock::     ;If left alt and capslock is pressed.  Will work even if other modifier keys are down.

{
static numPadMode :=false ; although the code runs with this line, I feel it's not right for it to be here.
	if (numPadMode)
		{
			numPadMode := false
            OutputDebug (numPadMode)
			nmsDisp("   Numpad OFF", 200, 25 )
		}
	else 
		{
			SetNumLockState("on")        ;Turns on numlock when numPadMode is activated.  With it off, you won't be able to input numbers.
			numPadMode := true
            OutputDebug (numPadMode)
			nmsDisp("   Numpad ON", 200, 317 )
		}
} 

#HotIf (numPadMode) ; This is where the error is pointing at.
; numPadMode := True
    {
        j::        Send("{Numpad1}")        ;When numPadMode is activated, the number row always sends numpad input.
        k::        Send("{Numpad2}")        ;
        l::        Send("{Numpad3}")        ;
        u::        Send("{Numpad4}")        ;
        i::        Send("{Numpad5}")        ;
        o::        Send("{Numpad6}")        ;
        7::        Send("{Numpad7}")        ;
        8::        Send("{Numpad8}")        ;
        9::        Send("{Numpad9}")
        ,::        Send("{Numpad0}")        ;
        m::        Send("{Numpad0}")        ;
        p::        Send("{NumpadDiv}")      ;
        0::        Send("{NumpadMult}")     ;
        `;::       Send("{NumpadSub}")      ;
        /::        Send("{NumpadAdd}")
        .::        Send("{NumpadDot}")      ;

        f::        Send("{Numpad1}")        ;
        d::        Send("{Numpad2}")        ;
        s::        Send("{Numpad3}")        ;
        r::        Send("{Numpad4}")        ;
        e::        Send("{Numpad5}")        ;
        w::        Send("{Numpad6}")        ;
        4::        Send("{Numpad7}")        ;
        3::        Send("{Numpad8}")        ;
        2::        Send("{Numpad9}")        ;
        v::        Send("{Numpad0}")        ;
        c::        Send("{Numpad0}")        ;
        q::        Send("{Backspace}")      ;
        5::        Send("{NumpadMult}")     ;
        t::        Send("{NumpadDiv}")      ;
        g::        Send("{NumpadAdd}")      ;
        b::        Send("{NumpadSub}")      ;
        a::        Send("{NumpadEnter}")      ;
        x::        Send("{NumpadDot}")      ;
        CapsLock:: Send("{Backspace}")     ;
    }
#Hotif

When I run this code i get:

Code: Select all

Warning: This variable appears to never be assigned a value.

Specifically: local numPadMode

	075: }
	077: {
▶	077: Return (numPadMode)
	077: }
	079: {


I feel like this should be a simple fix, I just don't know how.

Thank you for looking into my issue.
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Numpad on TKL keyboard

20 Mar 2024, 10:51

I think your problem is that hotkey threads are local scope in v2 (like a function called from a hotkey in v1) and it appears numPadMode is global in the v1 script. Also, aside from putting "global numPadMode" statements in all your hotkey code blocks, you'll want to declare and assign it at the top of the script (numPadMode := 0)
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Numpad on TKL keyboard

20 Mar 2024, 10:55

Oh, and you'll want to replace that static line 32 with the "global numPadMode" line
reddyshyam
Posts: 46
Joined: 24 Jul 2023, 04:34

Re: Numpad on TKL keyboard

20 Mar 2024, 11:08

Hi both,

I have used both Static and Global to make it work. Not sure if Global can replace Static but here goes my updated code.

Code: Select all

;=============================================================================================
; Left Alt + Caps Lock turns on Num Pad Mode
;=============================================================================================
; This script enables you to overlay a numpad on your tkl keyboard as shown below.
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|
; | [7] [8] [9] [0]    |  ----> | [7] [8] [9] [/]    | ; | [2] [3] [4] [5]    |  ----> | [9] [8] [7] [/]    |
; |  [u] [i] [o] [p]   |  ----> |  [4] [5] [6] [*]   | ; |  [w] [e] [r] [t]   |  ----> |  [6] [5] [4] [*]   |
; |   [j] [k] [l] [;]  |  ----> |   [1] [2] [3] [-]  | ; |   [s] [d] [f] [g]  |  ----> |   [3] [2] [1] [-]  |
; |    [m] [,] [.] [/] |  ----> |    [0] [0] [.] [+] | ; |    [x] [c] [c] [b] |  ----> |    [0] [0] [.] [+] |
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|

SetNumLockState True         ;Turns on numlock when the script is first executed. With it off, you won't be able to input numbers.

nmsDisp(text, ms, icon_no ){
	myGui := Gui()
	myGui.Opt("+AlwaysOnTop +ToolWindow -SysMenu -Caption")
	myGui.BackColor := "ffffff" ;changes background color
	myGui.SetFont("c000000 s20 bold", "Verdana") ;changes font color, size and font
	myGui.Add("Text", "x0 y0", text) ;the text to display
	myGui.Title := "Xn: 0"
	myGui.Show("NoActivate")

	Sleep(ms)
	myGui.Destroy()

	TraySetIcon("Shell32",icon_no,"1")
}

numPadMode1:=false

<!Capslock::     ;If left alt and capslock is pressed.  Will work even if other modifier keys are down.

{
	Static numPadMode :=false ; although the code runs with this line, I feel it's not right for it to be here.
	Global numPadMode1 := numPadMode
	if (numPadMode)
		{
			numPadMode := false
            OutputDebug (numPadMode)
			nmsDisp("   Numpad OFF", 200, 25 )
		}
	else 
		{
			SetNumLockState("on")        ;Turns on numlock when numPadMode is activated.  With it off, you won't be able to input numbers.
			numPadMode := true
            OutputDebug (numPadMode)
			nmsDisp("   Numpad ON", 200, 317 )
		}
} 

#HotIf (numPadMode1) ; This is where the error is pointing at.
; numPadMode := True
    {
        j::        Send("{Numpad1}")        ;When numPadMode is activated, the number row always sends numpad input.
        k::        Send("{Numpad2}")        ;
        l::        Send("{Numpad3}")        ;
        u::        Send("{Numpad4}")        ;
        i::        Send("{Numpad5}")        ;
        o::        Send("{Numpad6}")        ;
        7::        Send("{Numpad7}")        ;
        8::        Send("{Numpad8}")        ;
        9::        Send("{Numpad9}")
        ,::        Send("{Numpad0}")        ;
        m::        Send("{Numpad0}")        ;
        p::        Send("{NumpadDiv}")      ;
        0::        Send("{NumpadMult}")     ;
        `;::       Send("{NumpadSub}")      ;
        /::        Send("{NumpadAdd}")
        .::        Send("{NumpadDot}")      ;

        f::        Send("{Numpad1}")        ;
        d::        Send("{Numpad2}")        ;
        s::        Send("{Numpad3}")        ;
        r::        Send("{Numpad4}")        ;
        e::        Send("{Numpad5}")        ;
        w::        Send("{Numpad6}")        ;
        4::        Send("{Numpad7}")        ;
        3::        Send("{Numpad8}")        ;
        2::        Send("{Numpad9}")        ;
        v::        Send("{Numpad0}")        ;
        c::        Send("{Numpad0}")        ;
        q::        Send("{Backspace}")      ;
        5::        Send("{NumpadMult}")     ;
        t::        Send("{NumpadDiv}")      ;
        g::        Send("{NumpadAdd}")      ;
        b::        Send("{NumpadSub}")      ;
        a::        Send("{NumpadEnter}")      ;
        x::        Send("{NumpadDot}")      ;
        CapsLock:: Send("{Backspace}")     ;
    }
#Hotif

bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Numpad on TKL keyboard

20 Mar 2024, 11:24

I should have just done what reddyshyam did and include the updated code, but I was in a hurry. Unfortunately, reddyshyam's version can bomb too, because the global isn't set until the <!CapsLock hotkey is pressed. Better to just do away with the extra variable IMO

I did take the time to test!

Code: Select all

;=============================================================================================
; Left Alt + Caps Lock turns on Num Pad Mode
;=============================================================================================
; This script enables you to overlay a numpad on your tkl keyboard as shown below.
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|
; | [7] [8] [9] [0]    |  ----> | [7] [8] [9] [/]    | ; | [2] [3] [4] [5]    |  ----> | [9] [8] [7] [/]    |
; |  [u] [i] [o] [p]   |  ----> |  [4] [5] [6] [*]   | ; |  [w] [e] [r] [t]   |  ----> |  [6] [5] [4] [*]   |
; |   [j] [k] [l] [;]  |  ----> |   [1] [2] [3] [-]  | ; |   [s] [d] [f] [g]  |  ----> |   [3] [2] [1] [-]  |
; |    [m] [,] [.] [/] |  ----> |    [0] [0] [.] [+] | ; |    [x] [c] [c] [b] |  ----> |    [0] [0] [.] [+] |
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|

global numPadMode := 0

SetNumLockState True         ;Turns on numlock when the script is first executed. With it off, you won't be able to input numbers.

nmsDisp(text, ms, icon_no ){
	myGui := Gui()
	myGui.Opt("+AlwaysOnTop +ToolWindow -SysMenu -Caption")
	myGui.BackColor := "ffffff" ;changes background color
	myGui.SetFont("c000000 s20 bold", "Verdana") ;changes font color, size and font
	myGui.Add("Text", "x0 y0", text) ;the text to display
	myGui.Title := "Xn: 0"
	myGui.Show("NoActivate")

	Sleep(ms)
	myGui.Destroy()

	TraySetIcon("Shell32",icon_no,"1")
}

<!Capslock::     ;If left alt and capslock is pressed.  Will work even if other modifier keys are down.

{
global numPadMode ; although the code runs with this line, I feel it's not right for it to be here.
	if (numPadMode)
		{
			numPadMode := false
            OutputDebug (numPadMode)
			nmsDisp("   Numpad OFF", 200, 25 )
		}
	else 
		{
			SetNumLockState("on")        ;Turns on numlock when numPadMode is activated.  With it off, you won't be able to input numbers.
			numPadMode := true
            OutputDebug (numPadMode)
			nmsDisp("   Numpad ON", 200, 317 )
		}
} 

#HotIf (numPadMode) ; This is where the error is pointing at.
; numPadMode := True
    {
        j::        Send("{Numpad1}")        ;When numPadMode is activated, the number row always sends numpad input.
        k::        Send("{Numpad2}")        ;
        l::        Send("{Numpad3}")        ;
        u::        Send("{Numpad4}")        ;
        i::        Send("{Numpad5}")        ;
        o::        Send("{Numpad6}")        ;
        7::        Send("{Numpad7}")        ;
        8::        Send("{Numpad8}")        ;
        9::        Send("{Numpad9}")
        ,::        Send("{Numpad0}")        ;
        m::        Send("{Numpad0}")        ;
        p::        Send("{NumpadDiv}")      ;
        0::        Send("{NumpadMult}")     ;
        `;::       Send("{NumpadSub}")      ;
        /::        Send("{NumpadAdd}")
        .::        Send("{NumpadDot}")      ;

        f::        Send("{Numpad1}")        ;
        d::        Send("{Numpad2}")        ;
        s::        Send("{Numpad3}")        ;
        r::        Send("{Numpad4}")        ;
        e::        Send("{Numpad5}")        ;
        w::        Send("{Numpad6}")        ;
        4::        Send("{Numpad7}")        ;
        3::        Send("{Numpad8}")        ;
        2::        Send("{Numpad9}")        ;
        v::        Send("{Numpad0}")        ;
        c::        Send("{Numpad0}")        ;
        q::        Send("{Backspace}")      ;
        5::        Send("{NumpadMult}")     ;
        t::        Send("{NumpadDiv}")      ;
        g::        Send("{NumpadAdd}")      ;
        b::        Send("{NumpadSub}")      ;
        a::        Send("{NumpadEnter}")      ;
        x::        Send("{NumpadDot}")      ;
        CapsLock:: Send("{Backspace}")     ;
    }
#Hotif
User avatar
WarlordAkamu67
Posts: 230
Joined: 21 Mar 2023, 06:52

Re: Numpad on TKL keyboard

20 Mar 2024, 11:27

reddyshyam wrote:
20 Mar 2024, 11:08
I have used both Static and Global to make it work. Not sure if Global can replace Static but here goes my updated code.
Happy to hear you have been able to get it to work! Here is a demo with global/static/local variables. This is not related to the actual topic so I will hide it in a spoiler.
Spoiler
reddyshyam
Posts: 46
Joined: 24 Jul 2023, 04:34

Re: Numpad on TKL keyboard

20 Mar 2024, 11:43

Thanks @WarlordAkamu67 for the insight, it is really helpful. :clap:

As I can see, there is no way to access static variable outside the call other than assiging to a global variable. I am a newbie so please excuse me. :)
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Numpad on TKL keyboard

20 Mar 2024, 12:21

reddyshyam wrote:
20 Mar 2024, 11:43
Thanks @WarlordAkamu67 for the insight, it is really helpful. :clap:

As I can see, there is no way to access static variable outside the call other than assiging to a global variable. I am a newbie so please excuse me. :)
Global is by definition static... but I'm sure that's not what you meant
You could access a local static variable outside...

Code: Select all

tf
MsgBox %gVar%
tf(){
  static tVar := 66
  global gVar := &tVar
}
reddyshyam
Posts: 46
Joined: 24 Jul 2023, 04:34

Re: Numpad on TKL keyboard

20 Mar 2024, 12:27

Hi all,

Actually replaced static with global and also commented out gui for alerts and enabled traytip which might be better but you can undo the changes if you prefer your own GUI.

Code: Select all

;=============================================================================================
; Left Alt + Caps Lock turns on Num Pad Mode
;=============================================================================================
; This script enables you to overlay a numpad on your tkl keyboard as shown below.
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|
; | [7] [8] [9] [0]    |  ----> | [7] [8] [9] [/]    | ; | [2] [3] [4] [5]    |  ----> | [9] [8] [7] [/]    |
; |  [u] [i] [o] [p]   |  ----> |  [4] [5] [6] [*]   | ; |  [w] [e] [r] [t]   |  ----> |  [6] [5] [4] [*]   |
; |   [j] [k] [l] [;]  |  ----> |   [1] [2] [3] [-]  | ; |   [s] [d] [f] [g]  |  ----> |   [3] [2] [1] [-]  |
; |    [m] [,] [.] [/] |  ----> |    [0] [0] [.] [+] | ; |    [x] [c] [c] [b] |  ----> |    [0] [0] [.] [+] |
; |--------------------|        |--------------------| ; |--------------------|        |--------------------|

SetNumLockState True         ;Turns on numlock when the script is first executed. With it off, you won't be able to input numbers.

nmsDisp(text, ms, icon_no ){
	;myGui := Gui()
	;myGui.Opt("+AlwaysOnTop +ToolWindow -SysMenu -Caption")
	;myGui.BackColor := "ffffff" ;changes background color
	;myGui.SetFont("c000000 s20 bold", "Verdana") ;changes font color, size and font
	;myGui.Add("Text", "x0 y0", text) ;the text to display
	;myGui.Title := "Xn: 0"
	;myGui.Show("NoActivate")

	Traytip text
	Sleep(ms)
	Traytip 
	;myGui.Destroy()

	TraySetIcon("Shell32",icon_no,"1")
}

numPadMode:=false

<!Capslock::     ;If left alt and capslock is pressed.  Will work even if other modifier keys are down.

{

	if (numPadMode) {
			Global numPadMode := !numPadMode
            OutputDebug (numPadMode)
			nmsDisp("Numpad OFF", 2000, 25 )
		}
	else 
		{
			SetNumLockState("on")        ;Turns on numlock when numPadMode is activated.  With it off, you won't be able to input numbers.
			Global numPadMode := !numPadMode
            OutputDebug (numPadMode)
			nmsDisp("Numpad ON", 2000, 317 )
		}
		
} 

#HotIf (numPadMode) ; This is where the error is pointing at.
; numPadMode := True
    {
        j::        Send("{Numpad1}")        ;When numPadMode is activated, the number row always sends numpad input.
        k::        Send("{Numpad2}")        ;
        l::        Send("{Numpad3}")        ;
        u::        Send("{Numpad4}")        ;
        i::        Send("{Numpad5}")        ;
        o::        Send("{Numpad6}")        ;
        7::        Send("{Numpad7}")        ;
        8::        Send("{Numpad8}")        ;
        9::        Send("{Numpad9}")
        ,::        Send("{Numpad0}")        ;
        m::        Send("{Numpad0}")        ;
        p::        Send("{NumpadDiv}")      ;
        0::        Send("{NumpadMult}")     ;
        `;::       Send("{NumpadSub}")      ;
        /::        Send("{NumpadAdd}")
        .::        Send("{NumpadDot}")      ;

        f::        Send("{Numpad1}")        ;
        d::        Send("{Numpad2}")        ;
        s::        Send("{Numpad3}")        ;
        r::        Send("{Numpad4}")        ;
        e::        Send("{Numpad5}")        ;
        w::        Send("{Numpad6}")        ;
        4::        Send("{Numpad7}")        ;
        3::        Send("{Numpad8}")        ;
        2::        Send("{Numpad9}")        ;
        v::        Send("{Numpad0}")        ;
        c::        Send("{Numpad0}")        ;
        q::        Send("{Backspace}")      ;
        5::        Send("{NumpadMult}")     ;
        t::        Send("{NumpadDiv}")      ;
        g::        Send("{NumpadAdd}")      ;
        b::        Send("{NumpadSub}")      ;
        a::        Send("{NumpadEnter}")      ;
        x::        Send("{NumpadDot}")      ;
        CapsLock:: Send("{Backspace}")     ;
    }
#Hotif

reddyshyam
Posts: 46
Joined: 24 Jul 2023, 04:34

Re: Numpad on TKL keyboard

20 Mar 2024, 12:31

bj8tr wrote:
20 Mar 2024, 11:24
I should have just done what reddyshyam did and include the updated code, but I was in a hurry. Unfortunately, reddyshyam's version can bomb too, because the global isn't set until the <!CapsLock hotkey is pressed. Better to just do away with the extra variable IMO

I did take the time to test!

Please check my latest update, it should handle that case too. Totally global variable way!
bj8tr
Posts: 14
Joined: 23 Feb 2014, 14:02

Re: Numpad on TKL keyboard

20 Mar 2024, 12:52

Nice! Eliminates the extra global declaration line at the top of the <!CapsLock hotkey code block, and helped me realize that I inadvertently put the totally redundant global keyword in the global section when initializing the variable. I'm a little surprised the interpreter (or even debugger) didn't make at least a wisecrack about that. I suppose it isn't the worst idea to use global even then, to highlight it's globalness
User avatar
boiler
Posts: 17281
Joined: 21 Dec 2014, 02:44

Re: Numpad on TKL keyboard

20 Mar 2024, 15:04

bj8tr wrote:
20 Mar 2024, 12:52
I'm a little surprised the interpreter (or even debugger) didn't make at least a wisecrack about that. I suppose it isn't the worst idea to use global even then, to highlight it's globalness
The reasons for allowing global declarations outside of functions are outlined by the developer here.
bhaules
Posts: 3
Joined: 15 Dec 2021, 03:55

Re: Numpad on TKL keyboard

20 Mar 2024, 19:30

Thank you everyone for the effort of looking into this.
@WarlordAkamu67 - I think I now understand better how global, static, local variable work
@reddyshyam & @bj8tr - thank you for looking into this and providing me with a functional code :)

Cheers

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Marrsstar4 and 22 guests