stuck with keyboard layers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

stuck with keyboard layers

Post by HighArn » 30 Apr 2022, 12:58

Hi i'm pretty new in scripting. Just want to do keyboard more
convenient. So what i need is 3 layers in keyboard, layer1, layer2 that activates when i press once LAlt, and it must go back to layer1 when i press Lalt again, and layer3 that activates when i press and HOLD LAlt, and it must go back when i release it(It must go back to the layer from which it was activated. It's means that if I press and hold Lalt in layer1, after releasing it must back to layer1 if from layer2, must back in layer2). So i have this code:

Code: Select all


LAlt::
  T := !T 

 
return
  
#If  T
SC017::7
SC018::8 
SC019::9
SC025::4 
SC026::5 
SC027::6
SC032::1 
SC033::2
SC034::3
SC035::0

#If GetKeyState("LAlt", "P")

SC01E::+
SC01F::-
SC020::*
SC021::/

SC010::(
SC011::)
SC012::{
SC013::}
SC014::[
SC015::]



return



Problem with this is that when I in layer1 no matter I just press or press and hold is always goes to layer2. When I in layer2 it works better but not as i want. If in layer2 i press LAlt once it goes to layer1 if i press and hold it goes to layer 3, but after releasing it goes back to layer1. Would very appreciate for any help.


[Mod edit: Changed quote tags to code tags.]

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

Re: stuck with keyboard layers

Post by mikeyww » 30 Apr 2022, 13:35

Code: Select all

layer = 1 ; Auto-execute section

LAlt::
KeyWait, LAlt, T.4
If ErrorLevel { ; Held
 last := layer, layer := 3
 SoundBeep, 1900
 ToolTip, Layer = %layer%
 KeyWait, LAlt
 layer := last
} Else layer := 3 - layer
ToolTip, Layer = %layer%
SoundBeep, 700 + 300 * layer
Return

#If (layer = 1)
F3::Send 1
#If (layer = 2)
F3::Send 2
#If (layer = 3)
F3::Send 3
#If

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 30 Apr 2022, 14:24

mikeyww wrote:
30 Apr 2022, 13:35

Code: Select all

layer = 1 ; Auto-execute section

LAlt::
KeyWait, LAlt, T.4
If ErrorLevel { ; Held
 last := layer, layer := 3
 SoundBeep, 1900
 ToolTip, Layer = %layer%
 KeyWait, LAlt
 layer := last
} Else layer := 3 - layer
ToolTip, Layer = %layer%
SoundBeep, 700 + 300 * layer
Return

#If (layer = 1)
F3::Send 1
#If (layer = 2)
F3::Send 2
#If (layer = 3)
F3::Send 3
#If
Thanks for your response. Works pretty well when i create new script for it, but for some reason it works bad when i add to end of my script. I have this script that remaps keys for colemak-hd keyboard layout:

Code: Select all

Pause::

Suspend

Pause,,1

return



; Colemak Mod-DH wide mapping for ANSI boards




;SC002::1
;SC003::2
;SC004::3
;SC005::4
;SC006::5
SC007::=
SC008::6
SC009::7
SC00a::8
SC00b::9
SC00c::0
SC00d::-

;SC010::q
;SC011::w
SC012::f 
SC013::p
SC014::b
SC016::j
SC015::[ 
SC017::l 
SC018::u 
SC019::y
SC01A::;
SC01B::'

;SC01E::a
SC01F::r 
SC020::s
SC021::t
SC022::g
SC023::]
SC024::k
SC025::n 
SC026::e
SC027::i
SC028::o

SC02c::x
SC02d::c
SC02e::d
;SC02f::v
SC030::z
SC031::/
SC032::m
SC033::h
SC034::,
SC035::.

return
So when i add your code to the end of this - works only layer 3, first two just showing - "layer =".

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

Re: stuck with keyboard layers

Post by mikeyww » 30 Apr 2022, 14:49

Auto-exec section goes at the top.

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 30 Apr 2022, 15:23

Thanks! Sorry for many questions, but maybe you or someone else can help help with another. In yours code when LAlt pressed and held it goes through level 2 and then after short time lands on layer 3. Even if change timer digits to lowest it's still a little delay if i want go to level 3. Can it be done, so when LAlt pressed and held it istantly goes to 3, but if LAlt shortly pressed and then released it goes to layer 2?

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

Re: stuck with keyboard layers

Post by mikeyww » 30 Apr 2022, 16:19

Yes, you can change the timeout to be whatever you want. My hunch is that you have not decided what you need yet.

Explained: KeyWait

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 30 Apr 2022, 17:36

My bad that I explained it not very clever.

What we have now:

1) LAlt press and held - delay for layer 3(even if timeout short, delay still exist)
2) LAlt pressed and quicky released - instant level 2

My goal is:

1) LAlt press and held instant layer 3 (It's kinda worked like that with "#If GetKeyState("LAlt", "P")" )
2) LAlt pressed and quicky released - instant level 2

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

Re: stuck with keyboard layers

Post by mikeyww » 30 Apr 2022, 19:41

Your goal remains vague to me, but the following might fit.

Code: Select all

LAlt::
last := layer, layer := 3
SoundBeep, 700 + 300 * layer
KeyWait, LAlt
layer := A_Priorkey != "LAlt" ? last : last = 2 ? 1 : 2
SoundBeep, 700 + 300 * layer
Return

F3::Send 1
#If (layer = 2)
F3::Send 2
#If (layer = 3)
F3::Send 3
#If

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 02 May 2022, 14:09

Thanks that works a lot better for me! Maybe to not create new topic, i can ask another question? Why this code isn't working?

Code: Select all

#If (layer = 3)

Space:: CapsLock
Input, OutputVar, L1 
#if (ErrorLevel = "Max") 
Send, {CapsLock}
  
The idea is, turn capslock with space, but after pressing any button turn it off. This for getting 1 capitalized letter. For example: Space then "a" will produce "A" then other letters will be not capitalized.

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

Re: stuck with keyboard layers

Post by mikeyww » 02 May 2022, 15:06

You have multiple problems there: as a remap, your hotkey ends after its own line executes. It has an implicit Return, so subsequent lines will not execute. You have also confused If with #If, explained in the documentation.

Code: Select all

#If (layer = 3)
Space::
SetCapsLockState, On
Input, key, VL1, % "{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}"
                 . "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
                 . "{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}"
                 . "{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}"
SetCapsLockState, Off
Return
#If
An alternative is to have Space set a new layer, and then use the Hotkey command to create context-sensitive hotkeys for every key. Using a simple InputBox is another approach.

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 02 May 2022, 15:59

Thanks again, you saved a day like always. I only add layer := 1 after SetCapsLockState, On
because for some reason without this after pressing space and some key, script hit hotkeys from layer 3 that mapped to that key. But now is work like i wished.

HighArn
Posts: 17
Joined: 20 Oct 2021, 08:25

Re: stuck with keyboard layers

Post by HighArn » 05 May 2022, 13:42

Asking for help again. Now i'm stucked with two problems.
First I want to LAlt + Space always bring me to layer 1. What i tried;

Code: Select all

LAlt & Space::
layer := 1
return
but this work when I holding this hotkey, after I release LAlt it switches layer again.

Second I tried to hotkey that switch to layer 6 if I double tap LAlt. I add this code to "#if layer := 2" that triggers when LAlt pressed and released .

Code: Select all

LAlt::

#if A_Priorkey = "LAlt Up"
layer := 6
return
But this always switch to level 6, no matter if last key was LAlt or not.

And I Have a weird problem, sometimes when I fast switching through levels, my CTRL, that also remapped, not releasing and still pressed when actually release it. This all my code, maybe my bad coding skills are problem.
Spoiler

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

Re: stuck with keyboard layers

Post by mikeyww » 05 May 2022, 14:27

Shorten your script so that you can test & debug only the essential pieces that are causing difficulties. You are confusing #If and If. See documentation about them. := is used for assignment rather than comparison.

Post Reply

Return to “Ask for Help (v1)”