Page 1 of 1

Cycle Through Characters on Keypress

Posted: 21 Jan 2017, 12:46
by zeus
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.

Re: Cycle Through Characters on Keypress

Posted: 21 Jan 2017, 14:58
by Almost_there
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]
}

Re: Cycle Through Characters on Keypress  Topic is solved

Posted: 21 Jan 2017, 18:22
by Helgef
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

Re: Cycle Through Characters on Keypress

Posted: 21 Jan 2017, 19:53
by zeus
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?

Re: Cycle Through Characters on Keypress

Posted: 22 Jan 2017, 03:22
by Helgef
That's the Ternary operator, it basically consists of condition ? To do if true : to do if false

Re: Cycle Through Characters on Keypress

Posted: 28 Jun 2020, 05:34
by amateur_ keyboarder
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

Re: Cycle Through Characters on Keypress

Posted: 28 Jun 2020, 06:01
by Helgef
Hotkey, % "<^>!" key, typeit is probably right. Try LControl & RAlt up:: instead of ~<^>! up::. Perhaps you want to add ~, at least on LControl.

Cheers.

Re: Cycle Through Characters on Keypress

Posted: 28 Jun 2020, 11:18
by amateur_ keyboarder
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.

Re: Cycle Through Characters on Keypress

Posted: 28 Jun 2020, 12:09
by Helgef
key:=SubStr(A_PriorHotkey,2), you need to change the 2 to 5 I guess.

Cheers.

Re: Cycle Through Characters on Keypress

Posted: 02 Jul 2020, 13:08
by amateur_ keyboarder
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

Re: Cycle Through Characters on Keypress

Posted: 13 Jan 2023, 05:45
by homies50
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

Re: Cycle Through Characters on Keypress

Posted: 05 Feb 2023, 06:24
by Helgef
Hi maybe try :arrow: Text mode.

Cheers