Better/idiomatic script writing?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mjbatty
Posts: 19
Joined: 05 Nov 2020, 08:50

Better/idiomatic script writing?

Post by mjbatty » 30 Nov 2022, 09:07

As a follow up to my post yesterday viewtopic.php?f=76&t=110997, I can now toggle between 3 keyboards and everything works correctly.

Although I have been using AutoHotKey a couple of years, I have only used the basics and not really studied the language.

I am curious to know if there is a better (or more idiomatic) way of doing the following (which is working). One concern is that I have to set the state of each keyboard in every else block; it feels clumsy.

Code: Select all

;Default keyboard
keychron := true
...
; Input (keyboards/mice): Alt+i <key>
!i::
  ; Default is keychron, set above
  Input key, I L1 T0.75
  if (key = "k") {
		keychron := true
		dell5550 := false
	}
  else if (key = "d") {
		dell5550 := true
		keychron := false
	}
  else if (key = "n") {
		dell5550 := false
		keychron := false
	}
  else
    MsgBox, Unknown input shortcut: Alt+i %key%
  Return
...
#if dell5550
	; Move to start/end of line
	F11::send {Home}
	F12::send {End}
	...
#if

#if keychron
	; Remap page and home/end keys
	PgUp::send {Home}
	PgDn::send {End}
	Home::send {PgUp}
	End::send {PgDn}
	...
#if

; Don't need any remaps for normal (n) keyboard
Anything I can do to improve it, make it better somehow?

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Better/idiomatic script writing?

Post by boiler » 30 Nov 2022, 09:22

One way:

Code: Select all

;Default keyboard
keychron := true
...
; Input (keyboards/mice): Alt+i <key>
!i::
  ; Default is keychron, set above
  Input key, I L1 T0.75
  keychron := key = "k" ? true : false
  switch key
  {
  	Case "k": dell5550 := false
   	Case "d": dell5550 := true
  	Case "n": dell5550 := false
	Default: MsgBox, Unknown input shortcut: Alt+i %key%
  }
  Return
...
#if dell5550
	; Move to start/end of line
	F11::send {Home}
	F12::send {End}
	...
#if

#if keychron
	; Remap page and home/end keys
	PgUp::send {Home}
	PgDn::send {End}
	Home::send {PgUp}
	End::send {PgDn}
	...
#if

; Don't need any remaps for normal (n) keyboard

mjbatty
Posts: 19
Joined: 05 Nov 2020, 08:50

Re: Better/idiomatic script writing?

Post by mjbatty » 30 Nov 2022, 09:57

Thanks @boiler , that would be a little better/neater for the example with 3 keyboards, i.e keychron, dell, and a normal with no mapping.

But if I add say, HP1, TP1 and AC1 as 3 additional keyboards that need some keys remapped; I would still need to set each of those to false in each case statement, or am I missing something?

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Better/idiomatic script writing?

Post by boiler » 30 Nov 2022, 13:02

Not sure exactly how everything would be set for each based on what you said, so I'll just show this as an example:

Code: Select all

!i::
  ; Default is keychron, set above
  Input key, I L1 T0.75
  switch key
  {
  	Case "k":
		dell5550 := false
		keychron := false
   	Case "d": dell5550 := true
  	Case "n": dell5550 := false
	Case "HP1":
		dell5550 := false
		keychron := false
	Case "TP1":
		dell5550 := false
		keychron := false
	Case "AC1":
		dell5550 := false
		keychron := false
	Default: MsgBox, Unknown input shortcut: Alt+i %key%
  }
  Return

RussF
Posts: 1229
Joined: 05 Aug 2021, 06:36

Re: Better/idiomatic script writing?

Post by RussF » 30 Nov 2022, 13:12

If you are going to limit your input length to L1, you won't be able to use a key comparison with 3 characters, so you should either change the length option or stick to single character comparisons.

Russ

mjbatty
Posts: 19
Joined: 05 Nov 2020, 08:50

Re: Better/idiomatic script writing?

Post by mjbatty » 01 Dec 2022, 07:13

Thanks @boiler and @RussF, but I think I may have misled you.

I wanted to avoid setting every keyboard except the currently selected one to false. If I added more keyboards called HP1, TP1 and AC1 (for a total of 6), I would need to set 6 variables in if/else if block.

However, while writing a reply I had a light bulb moment and came up with the following; can this be improved in any way?

Code: Select all

; Keyboards
ac := "AC1"
de := "Dell5550"
hp := "HP1"
kc := "Keychron"
no := "Normal (no maps)"
tp := "TP1"

; Current (default) keyboard
curKeyboard = %kc%

; Show current keyboard
^!F12:: MsgBox, Current keyboard is %curKeyboard%
...
; Input (keyboards/mice): Alt+i <key>
!i::
	; Default is keychron, set above
  Input key, I L1 T0.75
  if (key = "a") {
		curKeyboard = %ac%
	}
  else if (key = "d") {
		curKeyboard = %de%
	}
  else if (key = "h") {
		curKeyboard = %hp%
	}
  else if (key = "k") {
		curKeyboard = %kc%
	}
  else if (key = "n") {
		curKeyboard = %no%
	}
  else if (key = "t") {
		curKeyboard = %tp%
	}
	else
    MsgBox, Unknown input shortcut: Alt+i %key%
  Return
...
#if curKeyboard = ac
	; maps for ac
#if

#if curKeyboard = de
	; maps for de
#if

#if curKeyboard = hp
	; maps for hp
#if

#if curKeyboard = kc
	; maps for kc
#if

#if curKeyboard = no
	; maps for no
#if

#if curKeyboard = tp
	; maps for tp
#if

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Better/idiomatic script writing?

Post by boiler » 01 Dec 2022, 07:27

On a part of your script you’re not asking about, you don’t need a blank #If after each group. Just start with another one. The blank #If does nothing here. Don’t think of it as a close to the previous one. That’s not what it does.

mjbatty
Posts: 19
Joined: 05 Nov 2020, 08:50

Re: Better/idiomatic script writing?

Post by mjbatty » 01 Dec 2022, 08:49

OK, good to know that @boiler 👍

RussF
Posts: 1229
Joined: 05 Aug 2021, 06:36

Re: Better/idiomatic script writing?

Post by RussF » 01 Dec 2022, 08:52

mjbatty wrote: I wanted to avoid setting every keyboard except the currently selected one to false. If I added more keyboards called HP1, TP1 and AC1 (for a total of 6), I would need to set 6 variables in if/else if block.
Why? You would only have to do it once and then you're done. There are many ways to accomplish the same thing. If your original script worked, expand on it and use it. If you prefer @boiler's, use it. The point is that you probably spent more time trying to make your code "shorter" and crafting your last script than if you had just expanded your original one that worked for you. :D

Don't get me wrong, there's certainly nothing wrong with learning how to make your code more efficient - that's a good thing. I have certainly fallen into the trap of agonizing on how turn 10 lines of code into 5. While the result may work, it may also be harder to read and modify in the future. When it does become valuable, however, is when you have a script that you use repeatedly that processes a lot of data and you are trying to cut the processing time down from minutes to seconds. Otherwise, go with what works.

Russ

mjbatty
Posts: 19
Joined: 05 Nov 2020, 08:50

Re: Better/idiomatic script writing?

Post by mjbatty » 01 Dec 2022, 09:49

Thanks @RussF and I agree, it's a balance. My last reply show's how I'm doing it now, not over-optimised but certainly better; thanks.

Post Reply

Return to “Ask for Help (v1)”