Cycle Through Characters on Keypress Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zeus
Posts: 2
Joined: 21 Jan 2017, 12:27

Cycle Through Characters on Keypress

Post by zeus » 21 Jan 2017, 12:46

Hello, brand new here so I apologize for any lack of etiquette.

I would like to create a script that essentially mimics the functionality from: http://ipa.typeit.org/
Particularly, the functionality where a user can hold down the alt key, press a character (like 'a') and then if the alt key is still pressed, and the same character ('a') is pressed again, that character is replace by the next character, in that particular character's set.

So for example, the 'a' character would have this set ["ɑ", "æ", "ɐ", "ɑ̃" ], and each keypress of 'a' while alt is held down iterates through the set replacing the character (in just a basic text editor).

I imagine a dynamic function could be created that takes the particular character's array and loops through it.

If someone could point me in the right direction if there is a similar example that would be awesome.

User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Cycle Through Characters on Keypress

Post by Almost_there » 21 Jan 2017, 14:58

This will do - insert char when releasing Alt button.

Code: Select all

#persistent
Global charList := { 1 : "a",  2 : "b",  3 : "c",  4 : "d" }

#if altButtonStillDown

!a::
	character := charListCycle(0)
	ToolTip, Key = %character%
Return

LAlt up::Send,%character%


#If

; Alt+A (first time, or after Alt button is released)ba
!a::
	altButtonStillDown := 1
	SetTimer, checkAltButtonPressed, 50
	character := charListCycle(1)
	ToolTip, Key = %character%
Return

checkAltButtonPressed:
	If (GetKeyState("LAlt") || GetKeyState("RAlt"))
		Return
	altButtonStillDown := 0
	ToolTip
	SetTimer, checkAltButtonPressed, Off
Return

charListCycle(reset) {
	Static cycler := 1
	If (reset || (cycler = charList._MaxIndex()))
		cycler := 1
	Else
		cycler++
	Return charList[cycler]
}
[edit]
I believe it looks more clean when array is not global.

Code: Select all

#persistent

#if altButtonStillDown

!a::
	character := charListCycle(0)
	ToolTip, Key = %character%
Return

LAlt up::Send,%character%


#If

; Alt+A (first time, or after Alt button is released)ba
!a::
	altButtonStillDown := 1
	SetTimer, checkAltButtonPressed, 50
	character := charListCycle(1)
	ToolTip, Key = %character%
Return

checkAltButtonPressed:
	If (GetKeyState("LAlt") || GetKeyState("RAlt"))
		Return
	altButtonStillDown := 0
	ToolTip
	SetTimer, checkAltButtonPressed, Off
Return

charListCycle(reset) {
	Static cycler := 1
	Static charList := { 1 : "a",  2 : "b",  3 : "c",  4 : "d" }
	If (reset || (cycler = charList._MaxIndex()))
		cycler := 1
	Else
		cycler++
	Return charList[cycler]
}

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Cycle Through Characters on Keypress  Topic is solved

Post by Helgef » 21 Jan 2017, 18:22

An alternative interpretation of the question.

Code: Select all

#SingleInstance, Force
; Define sets
sets:={	 a:{sym:["ɑ", "æ", "ɐ","ɑ̃"],ind:0}
		,b:{sym:["∫","∬"],ind:0}
		,c:{sym:["∑","∈"],ind:0}
		,d:{sym:["hello","world!"],ind:0} }
		; etc...
; Hotkeys are defined as !key, eg, !a
for key in sets
	Hotkey, % "!" key, typeit
return

; When alt is released, the cycle is reset.
~Alt up::
	key:=SubStr(A_PriorHotkey,2)			; Remove the modifier (!)
	if sets.HasKey(key)
		sets[key].ind:=0
return

typeit()
{
	global sets
	static pSym
	key:=SubStr(A_ThisHotkey,2)				; Remove the modifier (!)
	sym:=sets[key].sym
	ind:= (bs:= A_ThisHotkey=A_PriorHotkey) ? mod(sets[key].ind++, sym.length())+1 : sets[key].ind:=1
	SendInput, % (bs ? "{BS " StrLen(pSym) "}" : "") "{RAW}" sym[ind]
	pSym:=sym[ind] ; To get correct number of BS.
	return
}
Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.
Good luck

zeus
Posts: 2
Joined: 21 Jan 2017, 12:27

Re: Cycle Through Characters on Keypress

Post by zeus » 21 Jan 2017, 19:53

Helgef wrote:An alternative interpretation of the question.

Code: Select all

#SingleInstance, Force
; Define sets
sets:={	 a:{sym:["ɑ", "æ", "ɐ","ɑ̃"],ind:0}
		,b:{sym:["∫","∬"],ind:0}
		,c:{sym:["∑","∈"],ind:0}
		,d:{sym:["hello","world!"],ind:0} }
		; etc...
; Hotkeys are defined as !key, eg, !a
for key in sets
	Hotkey, % "!" key, typeit
return

; When alt is released, the cycle is reset.
~Alt up::
	key:=SubStr(A_PriorHotkey,2)			; Remove the modifier (!)
	if sets.HasKey(key)
		sets[key].ind:=0
return

typeit()
{
	global sets
	static pSym
	key:=SubStr(A_ThisHotkey,2)				; Remove the modifier (!)
	sym:=sets[key].sym
	ind:= (bs:= A_ThisHotkey=A_PriorHotkey) ? mod(sets[key].ind++, sym.length())+1 : sets[key].ind:=1
	SendInput, % (bs ? "{BS " StrLen(pSym) "}" : "") "{RAW}" sym[ind]
	pSym:=sym[ind] ; To get correct number of BS.
	return
}
Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.
Good luck

This works great. Thank you. Where would you recommend I look to understand the syntax and meaning of different 'methods(?)' used in this code?

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Cycle Through Characters on Keypress

Post by Helgef » 22 Jan 2017, 03:22

That's the Ternary operator, it basically consists of condition ? To do if true : to do if false

amateur_ keyboarder

Re: Cycle Through Characters on Keypress

Post by amateur_ keyboarder » 28 Jun 2020, 05:34

Hello @Helgef ,

Your script has helped me a lot! How would I have to change it if insted of using the Alt key, I wanted to use the AltGr key in order to cycle through the character sets? I tried with Hotkey, % "<^>!" key, typeit and ~<^>! up:: in lines 10 and 14 respectively, but that didn't work.

Many Thanks

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Cycle Through Characters on Keypress

Post by Helgef » 28 Jun 2020, 06:01

Hotkey, % "<^>!" key, typeit is probably right. Try LControl & RAlt up:: instead of ~<^>! up::. Perhaps you want to add ~, at least on LControl.

Cheers.

amateur_ keyboarder

Re: Cycle Through Characters on Keypress

Post by amateur_ keyboarder » 28 Jun 2020, 11:18

Thanks for the quick reply. Unfortunately, it still doesn't work for me. I tried both with and without ~ in front of LControl and RAlt but that did not help.

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Cycle Through Characters on Keypress

Post by Helgef » 28 Jun 2020, 12:09

key:=SubStr(A_PriorHotkey,2), you need to change the 2 to 5 I guess.

Cheers.

amateur_ keyboarder

Re: Cycle Through Characters on Keypress

Post by amateur_ keyboarder » 02 Jul 2020, 13:08

Thank you very much. This version works for me:

Code: Select all

#SingleInstance, Force
; Define sets
sets:={	 a:{sym:["ɑ", "æ", "ɐ","ɑ̃"],ind:0}
		,b:{sym:["∫","∬"],ind:0}
		,c:{sym:["∑","∈"],ind:0}
		,d:{sym:["hello","world!"],ind:0} }
		; etc...
; Hotkeys are defined as !key, eg, !a
for key in sets
	Hotkey, % "<^>!" key, typeit
return

; When alt is released, the cycle is reset.
~LControl & ~RAlt up::
	key:=SubStr(A_PriorHotkey,5)			; Remove the modifier (!)
	if sets.HasKey(key)
		sets[key].ind:=0
return

typeit()
{
	global sets
	static pSym
	key:=SubStr(A_ThisHotkey,5)				; Remove the modifier (!)
	sym:=sets[key].sym
	ind:= (bs:= A_ThisHotkey=A_PriorHotkey) ? mod(sets[key].ind++, sym.length())+1 : sets[key].ind:=1
	SendInput, % (bs ? "{BS " StrLen(pSym) "}" : "") "{RAW}" sym[ind]
	pSym:=sym[ind] ; To get correct number of BS.
	return
}

Cheers

homies50
Posts: 15
Joined: 07 Feb 2019, 00:04

Re: Cycle Through Characters on Keypress

Post by homies50 » 13 Jan 2023, 05:45

Helgef wrote:
21 Jan 2017, 18:22
An alternative interpretation of the question.

Code: Select all

#SingleInstance, Force
; Define sets
sets:={	 a:{sym:["ɑ", "æ", "ɐ","ɑ̃"],ind:0}
		,b:{sym:["∫","∬"],ind:0}
		,c:{sym:["∑","∈"],ind:0}
		,d:{sym:["hello","world!"],ind:0} }
		; etc...
; Hotkeys are defined as !key, eg, !a
for key in sets
	Hotkey, % "!" key, typeit
return

; When alt is released, the cycle is reset.
~Alt up::
	key:=SubStr(A_PriorHotkey,2)			; Remove the modifier (!)
	if sets.HasKey(key)
		sets[key].ind:=0
return

typeit()
{
	global sets
	static pSym
	key:=SubStr(A_ThisHotkey,2)				; Remove the modifier (!)
	sym:=sets[key].sym
	ind:= (bs:= A_ThisHotkey=A_PriorHotkey) ? mod(sets[key].ind++, sym.length())+1 : sets[key].ind:=1
	SendInput, % (bs ? "{BS " StrLen(pSym) "}" : "") "{RAW}" sym[ind]
	pSym:=sym[ind] ; To get correct number of BS.
	return
}
Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.
Good luck
hi, I am using this code, but I can't manage to edit this with effect I want. so basically I want to edit this and make it send using ascii code. because the special characters here won't work in Excel file. please help thank you

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Cycle Through Characters on Keypress

Post by Helgef » 05 Feb 2023, 06:24

Hi maybe try :arrow: Text mode.

Cheers

Post Reply

Return to “Ask for Help (v1)”