Cycle Through Characters on Keypress Topic is solved
Cycle Through Characters on Keypress
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.
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.
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
Re: Cycle Through Characters on Keypress
This will do - insert char when releasing Alt button.
[edit]
I believe it looks more clean when array is not global.
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]
}
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
An alternative interpretation of the question.
Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.
Good luck
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
}
Good luck
Re: Cycle Through Characters on Keypress
Helgef wrote:An alternative interpretation of the question.Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.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 }
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
That's the Ternary operator, it basically consists of condition ? To do if true : to do if false
Re: Cycle Through Characters on Keypress
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
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
Hotkey, % "<^>!" key, typeit is probably right. Try LControl & RAlt up:: instead of ~<^>! up::. Perhaps you want to add ~, at least on LControl.
Cheers.
Cheers.
Re: Cycle Through Characters on Keypress
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
key:=SubStr(A_PriorHotkey,2), you need to change the 2 to 5 I guess.
Cheers.
Cheers.
Re: Cycle Through Characters on Keypress
Thank you very much. This version works for me:
Cheers
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
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 youHelgef wrote: ↑21 Jan 2017, 18:22An alternative interpretation of the question.Press Alt+a, Alt+b, Alt+c and Alt+d and observe how it behaves.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 }
Good luck