Thanks Adam, that helped me to the following character → keycode conversion routine.
However, it is not very fast when converting long strings.
I am almost sure that this can be done more efficiently, but how…?
It discerns capitals (adding the shift code) and special keys need to go between brackets:
chartoISOKeyCode("Amsterdam[enter]")
to chartoISOKeyCode(x)
set vect to {{"esc", "35"}, {"F1", "7A"}, {"F2", "78"}, {"F3", "63"}, {"F4", "76"}, {"F5", "60"}, {"F6", "61"}, {"F7", "62"}, {"F8", "64"}, {"F9", "65"}, {"F10", "6D"}, {"F11", "67"}, {"F12", "6F"}, ¬
{"§", "0A"}, {"1!", "12"}, {"2@", "13"}, {"3#", "14"}, {"4$", "15"}, {"5%", "17"}, {"6^", "16"}, {"7&", "1A"}, {"8*", "1C"}, {"9(", "19"}, {"0)", "1D"}, {"-_", "1B"}, {"=+", "18"}, {"backspace", "33"}, ¬
{"tab", "30"}, {"qQ", "0C"}, {"wW", "0D"}, {"eE", "0E"}, {"rR", "0F"}, {"tT", "11"}, {"yY", "10"}, {"uU", "20"}, {"iI", "22"}, {"oO", "1F"}, {"pP", "23"}, {"[{", "21"}, {"]}", "1E"}, {"Return", "24"}, ¬
{"capslock", "39"}, {"aA", "00"}, {"sS", "01"}, {"dD", "02"}, {"fF", "03"}, {"gG", "05"}, {"hH", "04"}, {"jJ", "26"}, {"kK", "28"}, {"lL", "25"}, {";:", "29"}, {"'\"", "27"}, {"\\'|", "2A"}, ¬
{"shift", "38"}, {"`~", "32"}, {"zZ", "06"}, {"xX", "07"}, {"cC", "08"}, {"vV", "09"}, {"bB", "0B"}, {"nN", "2D"}, {"mM", "2E"}, {",<", "2B"}, {".>", "2F"}, {"/?", "2C"}, ¬
{"Fn", "3F"}, {"ctrl", "3B"}, {"opt", "3A"}, {"cmd", "37"}, {" ", "31"}, {"enter", "4C"}, {"leftarrow", "7B"}, {"downarrow", "7D"}, {"uparrow", "7E"}, {"rightarrow", "7C"}}
set KcodeStr to ""
set skipflag to false
repeat with h from 1 to count of characters of x
set hStr to x's character h
if h > 2 and x's character (h - 1) as string = "]" then set skipflag to false
if x's character h as string = "[" then
set t to ""
repeat with k from h + 1 to count of characters of x
if x's character k = "]" then exit repeat
set t to (t & x's character k) as string
end repeat
set hStr to t
end if
if not skipflag then
considering case
if number of characters of hStr > 1 then
set skipflag to true
set namedkeys to ""
else
set namedkeys to {"esc", "tab", "backspace", "capslock", "return", "enter", "shift", "cmd", "opt", "ctrl", "Fn", "leftarrow", "rightarrow", "downarrow", "uparrow"}
end if
-- display dialog (hStr & return & skipflag) as string
repeat with j in items of vect
if hStr is in j's item 1 and j's item 1 is not in namedkeys then
set prefix to ""
if number of characters of j's item 1 = 2 and (hStr) is in character 2 of j's item 1 then set prefix to "38 "
set KcodeStr to KcodeStr & prefix & j's item 2 & ","
exit repeat
end if
end repeat
end considering
end if
end repeat
return KcodeStr's text 1 thru -2
end chartoISOKeyCode