How to Create Shortcut Key Combinations?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
How to Create Shortcut Key Combinations?
Hello All,
One of my frustrating experiments of the day was trying to create shortcut key combinations to insert particular text in a field.
For instance, if I wanted to press "alt" and the letter key "h" to insert "hello world," how would I accomplish that task? Also, how do I make sure it inserts at the right insertion point?
I've been experimenting for hours with altKey, rawKeys, keysDown, on returnInField, etc., but no luck so far. Is there a standard way to use a combination of two keys to make a task happen? I tried searching on the forum but couldn't find the answer.
Any advice along these lines will be greatly appreciated.
Thanks in advance.
All the best,
deeverd
One of my frustrating experiments of the day was trying to create shortcut key combinations to insert particular text in a field.
For instance, if I wanted to press "alt" and the letter key "h" to insert "hello world," how would I accomplish that task? Also, how do I make sure it inserts at the right insertion point?
I've been experimenting for hours with altKey, rawKeys, keysDown, on returnInField, etc., but no luck so far. Is there a standard way to use a combination of two keys to make a task happen? I tried searching on the forum but couldn't find the answer.
Any advice along these lines will be greatly appreciated.
Thanks in advance.
All the best,
deeverd
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How to Create Shortcut Key Combinations?
Probably best for the user if those cominations are also discoverable in menus - for menu item shortcuts see the Dictionary entry for "menu".
You can also trap key messages wiithout menu items, using the commandKeyDown and controlKeyDown messages, examining the state of other keys with functions like optionKey and shiftKey.
You can also trap key messages wiithout menu items, using the commandKeyDown and controlKeyDown messages, examining the state of other keys with functions like optionKey and shiftKey.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to Create Shortcut Key Combinations?
Hello Fourth World,
Thanks for the response. I've spent the past hour looking at menu commands, playing with commandKey, controlKey, etc., but am still having no joy. I also went back again through all the tutorials I could find such as limiting the number of characters in a field, which I thought might provide a clue, but I'm still back to square one.
Do you know of any tutorial I'm missing that might provide enough clues as to how to assign my own shortcut keys (alt + "b" as an example, note: one of the keys is a letter) to place selected strings of text into a field, depending on which combination of shortcut keys were used? Or perhaps you know of a single example line of code that might do the trick?
Anyway, I appreciate your effort. Looking forward to your response.
All the best,
deeverd
Thanks for the response. I've spent the past hour looking at menu commands, playing with commandKey, controlKey, etc., but am still having no joy. I also went back again through all the tutorials I could find such as limiting the number of characters in a field, which I thought might provide a clue, but I'm still back to square one.
Do you know of any tutorial I'm missing that might provide enough clues as to how to assign my own shortcut keys (alt + "b" as an example, note: one of the keys is a letter) to place selected strings of text into a field, depending on which combination of shortcut keys were used? Or perhaps you know of a single example line of code that might do the trick?
Anyway, I appreciate your effort. Looking forward to your response.
All the best,
deeverd
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How to Create Shortcut Key Combinations?
You only need to handle key messages directly if you don't have shortcuts defined in the stack's menu items.
To set up the menu items you can use the GUI tool, Menu Builder, available from the Tools menu, or you can script those shortcuts manually as described in the Dictionary entry for "menu".
That entry notes:
My Menu Item/#B
To set up the menu items you can use the GUI tool, Menu Builder, available from the Tools menu, or you can script those shortcuts manually as described in the Dictionary entry for "menu".
That entry notes:
And:The syntax for menu item strings is:
[<flags>] <label> ['/' <accelerator> ['|' <tag>]]
So to make Alt/Option + B as a shortcut for an item named "My Menu Item", the text for that line of the menu button would be:Here <modifiers> is either:
1) a space separated list of:
'command' or 'cmd', 'control' or 'ctrl', 'option' or 'opt', 'alt', 'shift'
in this case the keyname/char should be separated from the list by a space
2) a sequence of characters:
^(Command)
@ (Shift)
#(Option)
% (Control)
in this case no space between the sequence and keyname/char is required.
My Menu Item/#B
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 86
- Joined: Tue May 15, 2012 5:56 pm
Re: How to Create Shortcut Key Combinations?
I found it best to use rawKeyDown for doing shortcut commands as keyDown does not work when trying to use the alt key as part of the shortcut. rawKeyDown is a little more annoying to use since you have to figure out theKeyNumber for each key you want to use. Here is sample code of the alt + b shortcut key. This code should be placed in the Mainstack script. You will need to trap rawKeyDown in input fields you don't want these shortcuts to work in.
Code: Select all
on rawKeyDown theKeyNumber
put "theKeyNumber:" && theKeyNumber & CR after msg
switch (theKeyNumber)
case 98 -- b
case 66 -- B
if the altKey is down then
put "Alt + B Key Combo Found" & CR after msg
end if
if the optionKey is down then
put "Option + B Key Combo Found" & CR after msg
end if
break
end switch
pass rawKeyDown
end rawKeyDown
Re: How to Create Shortcut Key Combinations?
Thanks FourthWorld and CenturyMan,
Your advice and sample script should help a lot. I'll be working with it over the next hour and report back on my success. In the meantime, since I hadn't known about the numbers assigned to each key (which certainly explains my failed attempts to use rawKey), and just in case anyone else is looking for a similar solution and happens to come across this thread, I put together a list that shows the numbers assigned to all the upper and lower case letters, plus the numbers 0-9, space key, enter key, tab, and shift key. I've included that list below in the hope that it helps others, too.
All the best,
deeverd
List of rawKey Numbers for Windows
space = theKeyNumber: 32
0 = theKeyNumber: 48
1 = theKeyNumber: 49
2 = theKeyNumber: 50
3 = theKeyNumber: 51
4 = theKeyNumber: 52
5 = theKeyNumber: 53
6 = theKeyNumber: 54
7 = theKeyNumber: 55
8 = theKeyNumber: 56
9 = theKeyNumber: 57
A = theKeyNumber: 65
B = theKeyNumber: 66
C = theKeyNumber: 67
D = theKeyNumber: 68
E = theKeyNumber: 69
F = theKeyNumber: 70
G = theKeyNumber: 71
H = theKeyNumber: 72
I = theKeyNumber: 73
J = theKeyNumber: 74
K = theKeyNumber: 75
L = theKeyNumber: 76
M = theKeyNumber: 77
N = theKeyNumber: 78
O = theKeyNumber: 79
P = theKeyNumber: 80
Q = theKeyNumber: 81
R = theKeyNumber: 82
S = theKeyNumber: 83
T = theKeyNumber: 84
U = theKeyNumber: 85
V = theKeyNumber: 86
W = theKeyNumber: 87
X = theKeyNumber: 88
Y = theKeyNumber: 89
Z = theKeyNumber: 90
a = theKeyNumber: 97
b = theKeyNumber: 98
c = theKeyNumber: 99
d = theKeyNumber: 100
e = theKeyNumber: 101
f = theKeyNumber: 102
g = theKeyNumber: 103
h = theKeyNumber: 104
i = theKeyNumber: 105
j = theKeyNumber: 106
k = theKeyNumber: 107
l = theKeyNumber: 108
m = theKeyNumber: 109
n = theKeyNumber: 110
o = theKeyNumber: 111
p = theKeyNumber: 112
q = theKeyNumber: 113
r = theKeyNumber: 114
s = theKeyNumber: 115
t = theKeyNumber: 116
u = theKeyNumber: 117
v = theKeyNumber: 118
w = theKeyNumber: 119
x = theKeyNumber: 120
y = theKeyNumber: 121
z = theKeyNumber: 122
enter = theKeyNumber: 65293
shift = theKeyNumber: 65505
alt = theKeyNumber: 65513
tab = theKeyNumber: 65289
Your advice and sample script should help a lot. I'll be working with it over the next hour and report back on my success. In the meantime, since I hadn't known about the numbers assigned to each key (which certainly explains my failed attempts to use rawKey), and just in case anyone else is looking for a similar solution and happens to come across this thread, I put together a list that shows the numbers assigned to all the upper and lower case letters, plus the numbers 0-9, space key, enter key, tab, and shift key. I've included that list below in the hope that it helps others, too.
All the best,
deeverd
List of rawKey Numbers for Windows
space = theKeyNumber: 32
0 = theKeyNumber: 48
1 = theKeyNumber: 49
2 = theKeyNumber: 50
3 = theKeyNumber: 51
4 = theKeyNumber: 52
5 = theKeyNumber: 53
6 = theKeyNumber: 54
7 = theKeyNumber: 55
8 = theKeyNumber: 56
9 = theKeyNumber: 57
A = theKeyNumber: 65
B = theKeyNumber: 66
C = theKeyNumber: 67
D = theKeyNumber: 68
E = theKeyNumber: 69
F = theKeyNumber: 70
G = theKeyNumber: 71
H = theKeyNumber: 72
I = theKeyNumber: 73
J = theKeyNumber: 74
K = theKeyNumber: 75
L = theKeyNumber: 76
M = theKeyNumber: 77
N = theKeyNumber: 78
O = theKeyNumber: 79
P = theKeyNumber: 80
Q = theKeyNumber: 81
R = theKeyNumber: 82
S = theKeyNumber: 83
T = theKeyNumber: 84
U = theKeyNumber: 85
V = theKeyNumber: 86
W = theKeyNumber: 87
X = theKeyNumber: 88
Y = theKeyNumber: 89
Z = theKeyNumber: 90
a = theKeyNumber: 97
b = theKeyNumber: 98
c = theKeyNumber: 99
d = theKeyNumber: 100
e = theKeyNumber: 101
f = theKeyNumber: 102
g = theKeyNumber: 103
h = theKeyNumber: 104
i = theKeyNumber: 105
j = theKeyNumber: 106
k = theKeyNumber: 107
l = theKeyNumber: 108
m = theKeyNumber: 109
n = theKeyNumber: 110
o = theKeyNumber: 111
p = theKeyNumber: 112
q = theKeyNumber: 113
r = theKeyNumber: 114
s = theKeyNumber: 115
t = theKeyNumber: 116
u = theKeyNumber: 117
v = theKeyNumber: 118
w = theKeyNumber: 119
x = theKeyNumber: 120
y = theKeyNumber: 121
z = theKeyNumber: 122
enter = theKeyNumber: 65293
shift = theKeyNumber: 65505
alt = theKeyNumber: 65513
tab = theKeyNumber: 65289
-
- Livecode Opensource Backer
- Posts: 10078
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to Create Shortcut Key Combinations?
I think those rawKey down codes for letters are just the ASCII codes: I remember having to memorise the lot in 1976
when I was 14, learning FORTRAN IV.
However those are now a subset of the Unicode consortium's codeset: http://www.unicode.org/charts/
You can download a PDF chart for a particular writing system; the only slight snag is that they are given in Hexadecimal
and have to converted into Decimal.
when I was 14, learning FORTRAN IV.
However those are now a subset of the Unicode consortium's codeset: http://www.unicode.org/charts/
You can download a PDF chart for a particular writing system; the only slight snag is that they are given in Hexadecimal
and have to converted into Decimal.
-
- Posts: 86
- Joined: Tue May 15, 2012 5:56 pm
Re: How to Create Shortcut Key Combinations?
You are correct that they are just ASCII values. You can always just use numToChar(theKeyNumber) to get the character of the key.richmond62 wrote:I think those rawKey down codes for letters are just the ASCII codes: I remember having to memorise the lot in 1976
when I was 14, learning FORTRAN IV.
However those are now a subset of the Unicode consortium's codeset: http://www.unicode.org/charts/
You can download a PDF chart for a particular writing system; the only slight snag is that they are given in Hexadecimal
and have to converted into Decimal.
Re: How to Create Shortcut Key Combinations?
Hello again FourthWorld and CenturyMan,
I haven't had a chance yet to work on the menu portion, but I can happily report that the rawKey shortcut script works as advertised. Thanks so much! The only real changes I made were to experiment with how the script worked when it was in the stack script compared to how it worked when placed into the only field where I wanted to use it. It seemed to work just fine in the field itself, which prevented the shortcut from being accidentally activated in any other field. Other than that change, I replaced "msg" with a variable that checked to find the correct combination, and when it did it knew to insert the chosen text into the field. Below is my basic code that seems to work perfectly:
My only problem that remains now is how to insert the script exactly at the insertion point in the correct field. I know how to put it before and after the field with no problem, but not how to tell the script to automatically insert it where I clicked into the field just before pressing the combination of shortcut keys. If anyone knows that answer (probably a rather basic question), I will have found all the solutions to this most recent scripting challenge.
Thanks so much in advance for what you can add to this question.
All the best,
deeverd
I haven't had a chance yet to work on the menu portion, but I can happily report that the rawKey shortcut script works as advertised. Thanks so much! The only real changes I made were to experiment with how the script worked when it was in the stack script compared to how it worked when placed into the only field where I wanted to use it. It seemed to work just fine in the field itself, which prevented the shortcut from being accidentally activated in any other field. Other than that change, I replaced "msg" with a variable that checked to find the correct combination, and when it did it knew to insert the chosen text into the field. Below is my basic code that seems to work perfectly:
Code: Select all
on rawKeyDown theKeyNumber
put "theKeyNumber:" && theKeyNumber & CR after tVariable
switch (theKeyNumber)
case 98 -- b
if the altKey is down then
put "Alt + B Key Combo Found" & CR after tVariable
if tVariable contains "Alt + B" then
put "hello world" after field "textField"
end if
end if
break
end switch
pass rawKeyDown
end rawKeyDown
Thanks so much in advance for what you can add to this question.
All the best,
deeverd
-
- Posts: 86
- Joined: Tue May 15, 2012 5:56 pm
Re: How to Create Shortcut Key Combinations?
I only had the put lines in there so you can see output in the message window. You also don't need to check tVariable after the "altKey is down" line since you have already determined that the altKey and the "b" key are down at this point.
Code: Select all
on rawKeyDown theKeyNumber
switch (theKeyNumber)
case 98 -- b
if the altKey is down then
put "hello world" after field "textField"
end if
break
end switch
pass rawKeyDown
end rawKeyDown
-
- Posts: 86
- Joined: Tue May 15, 2012 5:56 pm
Re: How to Create Shortcut Key Combinations?
Also to insert text where the cursor is in a text field you can just use the following line,
Code: Select all
put "hello world" into the selection
Re: How to Create Shortcut Key Combinations?
Hello CenturyMan1979,
Thanks for saving me several lines of coding for each shortcut key combination. That saves quite a bit of time and effort alone.
Also, didn't know it was so easy to insert text wherever the cursor had last been clicked. "The selection" makes my life a lot happier.
Great information. It really helps a lot! Everything is working perfect now.
All the best,
deeverd
Thanks for saving me several lines of coding for each shortcut key combination. That saves quite a bit of time and effort alone.
Also, didn't know it was so easy to insert text wherever the cursor had last been clicked. "The selection" makes my life a lot happier.
Great information. It really helps a lot! Everything is working perfect now.
All the best,
deeverd
Re: How to Create Shortcut Key Combinations?
Hi CenturyMan1979,
I'm getting an annoying "Ding" (livecode "beep") each time I use the alt key script (Win 7).
Does this go away in a standalone?
Is this something in my settings?
Simon
I'm getting an annoying "Ding" (livecode "beep") each time I use the alt key script (Win 7).
Does this go away in a standalone?
Is this something in my settings?
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: How to Create Shortcut Key Combinations?
Hello Simon,
I just wanted to help perhaps solve some of the mystery of the "dings" when using the alt key. Now that I've been writing some script with it, what I find is that if I use a key combination that uses (alt + shift + any letter), I don't get the ding. However, when I use (alt + a letter) the ding occurs. What it seems to me to be is that there are shortcut keystrokes in the LiveCode program that use combinations of the alt key because the ding is often accompanied with some sort of menu that pops up.
Hope this helps.
All the best,
deeverd
I just wanted to help perhaps solve some of the mystery of the "dings" when using the alt key. Now that I've been writing some script with it, what I find is that if I use a key combination that uses (alt + shift + any letter), I don't get the ding. However, when I use (alt + a letter) the ding occurs. What it seems to me to be is that there are shortcut keystrokes in the LiveCode program that use combinations of the alt key because the ding is often accompanied with some sort of menu that pops up.
Hope this helps.
All the best,
deeverd