Key that changes the output of other keys Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
purzedo
Posts: 31
Joined: 14 Nov 2020, 00:01

Key that changes the output of other keys

21 Nov 2020, 22:29

Hi. Right now, I am trying to basically create a modifier key, but without the holding down of the key.

For example:

Pressing Q on the keyboard does X.

However, when I press 1 (tap, not hold) and then press Q, it does Y instead -- but only after I press 1.

And this should be able to work for any key after pressing 1. So, when G does X, pressing 1 then G does y.

I tried doing this:

Code: Select all

1::
Q::
Send, X
return
But that just makes both keys send X. I'm sure the code is very simple... but sometimes the concept is what is complicated.

Edit:

To clarify, I actually want every key to act normal at first; so Q sends Q, X sends X, etc etc. Only after I press 1 it changes the behavior of the other keys.
Last edited by purzedo on 21 Nov 2020, 23:02, edited 1 time in total.
purzedo
Posts: 31
Joined: 14 Nov 2020, 00:01

Re: Key that changes the output of other keys

21 Nov 2020, 22:44

Actually, I think I got it. Here's the code, taken from another thread here (I will do more research next time, I promise):

Code: Select all

Keyboard := 0
1::Keyboard := 1
2::Keyboard := 2
3::Keyboard := 3
4::Keyboard := 4

#If (Keyboard = 1)
Space::
Send, q
Keyboard = 0
return
#If

#If (Keyboard = 2)
Q::
Send, T
Keyboard = 0
return
#If

; etc
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: Key that changes the output of other keys

21 Nov 2020, 22:48

Try this one:

Code: Select all

1:: modif:= 1
Q::
    If modif
        Send Y
    Else Send X
    modif:= ""
Return
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Key that changes the output of other keys

21 Nov 2020, 22:54

Why not simple hotstrings?

Code: Select all

:*?:1q::y
:*?:q::x

purzedo
Posts: 31
Joined: 14 Nov 2020, 00:01

Re: Key that changes the output of other keys

21 Nov 2020, 23:00

rommmcek wrote:
21 Nov 2020, 22:48
Try this one:

Code: Select all

1:: modif:= 1
Q::
    If modif
        Send Y
    Else Send X
    modif:= ""
Return
Hmm, I just commented above (sorry if you didn't see that, I figured it out quite quick, I should have done more research).

To clarify, I actually want every key to act normal until I press the 1 key. So, q sends q, unless I press 1; then it sends y. Didn't mean to mislead you there!

When I do that with your code I think it repeats it self (it said I sent 71 hotkeys in like 750 ms).

Anyway... if you DO want to help, perhaps you could help me figure out how to take this code:

Code: Select all

Keyboard := 0
1::Keyboard := 1
2::Keyboard := 2
3::Keyboard := 3
4::Keyboard := 4

#If (Keyboard = 2)
Q::
Send, t
Keyboard = 0
return
#If

#If (Keyboard = 2)
F::
Send, x
Keyboard = 0
return
#If
And mush it into one? Notice the two almost identical

Code: Select all

#If (Keyboard = 2)
blocks; I tried merging them and it didn't work unfortunately.
To clarify exactly what I want this time, every time I press a key it needs to revert back to Keyboard 0. Hence the Keyboard = 0 in every block. I don't think it's possible to merge them, but if you have any ideas feel free to post 'em! You know the whole deal with repeating stuff in the code world :)
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Key that changes the output of other keys

21 Nov 2020, 23:19

purzedo wrote:
21 Nov 2020, 23:00
To clarify, I actually want every key to act normal until I press the 1 key. So, q sends q, unless I press 1; then it sends y. Didn't mean to mislead you there!
Hotstrings meet that requirement and are much more compact and straightforward:

Code: Select all

:*?:1q::y
:*?:2q::t
purzedo
Posts: 31
Joined: 14 Nov 2020, 00:01

Re: Key that changes the output of other keys

21 Nov 2020, 23:35

boiler wrote:
21 Nov 2020, 23:19
purzedo wrote:
21 Nov 2020, 23:00
To clarify, I actually want every key to act normal until I press the 1 key. So, q sends q, unless I press 1; then it sends y. Didn't mean to mislead you there!
Hotstrings meet that requirement and are much more compact and straightforward:

Code: Select all

:*?:1q::y
:*?:2q::t
Thank you very much -- I will definitely add this to my arsenal.

At first I didn't actually understand what the heck that code even meant (and I tried it and still didn't), just looked like gibberish but now I understand it. If I press 1 and then q, it sends y; the asterisk just makes it so you don't have to press space, enter, or period, and the question mark makes it so it works even if I typed letters previous to it. (Correct? :D)

Right now I want to actually deactivate all the number keys. I work in Photoshop, and when I press the 1 key, it changes the opacity of the brush or layer or flow etc. So I need all those keys to actually be "deactivated" -- but I am actually going to use hotstrings to make another hotkey right now, so thanks for enlightening me!
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Key that changes the output of other keys  Topic is solved

21 Nov 2020, 23:35

Your way can be written more efficiently. If you don’t follow it with another #If, then the previous one is still in effect:

Code: Select all

Keyboard := 0
1::Keyboard := 1
2::Keyboard := 2
3::Keyboard := 3
4::Keyboard := 4

#If (Keyboard = 2)
Q::
Send, t
Keyboard = 0
return

F::
Send, x
Keyboard = 0
return
#If

Even more compact:

Code: Select all

Keyboard := 0
1::
2::
3::
4::Keyboard := A_ThisHotkey

#If (Keyboard = 2)
q::
f::
Send, % {q: "t", f: "x"}[A_ThisHotkey]
Keyboard = 0
return
#If
purzedo
Posts: 31
Joined: 14 Nov 2020, 00:01

Re: Key that changes the output of other keys

21 Nov 2020, 23:52

boiler wrote:
21 Nov 2020, 23:35
Your way can be written more efficiently. If you don’t follow it with another #If, then the previous one is still in effect:

Code: Select all

Keyboard := 0
1::Keyboard := 1
2::Keyboard := 2
3::Keyboard := 3
4::Keyboard := 4

#If (Keyboard = 2)
Q::
Send, t
Keyboard = 0
return

F::
Send, x
Keyboard = 0
return
#If

Even more compact:

Code: Select all

Keyboard := 0
1::
2::
3::
4::Keyboard := A_ThisHotkey

#If (Keyboard = 2)
q::
f::
Send, % {q: "t", f: "x"}[A_ThisHotkey]
Keyboard = 0
return
#If
Thank you! - that works great. I don't mean to devalue your work here but I think there's something to be said about compactness vs readability - and that first version you have there is much more readable to me. Especially since I may be making a big "template guide" to give out to artists who are intimidated or confused by AHK, I think it's best if the code stays as readable as possible. Also, your first version aligns better with my coding convention anyway - but thank you again!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, mebelantikjaya, ositoMalvado and 291 guests