How to handle key combinations in your Livecode applications?
In most desktop applications we find that there are certain actions that we can do using a keyboard shortcut, this makes working with those applications faster and more efficient. We no longer have to lift our hand from the keyboard to go to a menu and search through its options. That is why at FerrusLogic when we are developing a desktop application, we take into account the possible key combinations.
Handling keybindings in Livecode.
So far when we create a desktop app we do something like the following for the keybindings.
on rawKeyDown switch the keysDown case "65507,100" // Ctrl + D // Duplicate // more code on rawKeyDown
So a case for every possible key combination. In this way our applications had key combinations. But what if the user want to customize one of the combinations or if we need to create a new combination? We would have to create another case within the switch for the new combination, but the end user would still be tied to the choice of combinations made by us. So after turning our heads around for a couple of minutes we decided to solve this problem.
We created a library to handle key bindings dynamically instead of using a switch structure to create our key combinations, making possible to add, edit and delete a combination easily. This is how the libFastShortcuts library was born. This works on an array, which we can save in a property or in a JSON file, offering the possibility of adding key combinations without having to write a lot of lines of code. In addition to that, now users can change the key combinations at will. Making desktop applications more tailored to your needs.
What is libFastShortcuts library?
libFastShortcuts is a LiveCode script library for handling key combinations dynamically and simply. We can establish key combinations in an intuitive way since we do not have to use the numerical codes of the pressed keys. Ex. “65507,100” to refer to the combination of Ctrl and the letter D keys. We do it in the form “Ctrl + D ”. The library is not case sensitive so it does not matter if the caps lock is active or not, we will always have the same result.
How does libFastShortcuts work?
To handle the key combinations libFastShortcuts is based on an array with the following structure:
array [Key Combination]
Where key combination = The keys that must be pressed for the handler to fire. This has to be the value returned by the keysDownNames function, otherwise it will be ignored by the library.
array [Key combination] [Message to send]
Message to send = Name of the handler to call
array [Key combination] [Parameters]
Parameters = To the parameter (s) that will be sent with this message. (Optional)
array [Key Combination] [Object]
Object = The object on which the manipulator is triggered. It is Optional, since if this parameter is not specified, the message is fired for all objects in the message flow.
To load this array into the library we do it using the loadShortcuts command. This command expects an array as described above as a parameter.
How to create new keyboard shortcuts?
To add a new key combination we use the newShortcut command which expects four parameters.
- The key combination. (it is the value returned by the keysDownNames function) This parameter is Required.
- The manipulator to be triggered on this key combination. This parameter is Required.
- The parameter (s) that will be sent to the manipulator. This parameter is optional.
- The object on which the manipulator will be triggered. This parameter is optional, if the object is not specified, the handler will be sent on the message path for all objects.
Creating key combinations with libFastShortcuts library
The first thing we have to take into account is in which part of the message path we want to put our libFastShortcuts library. Since this library captures the rawKeyDown event generated by the keyboard.
We have to keep in mind that, if we use it as a behavior, library or back script, no object before it in the message flow can catch the rawKeyDown message and if it does it must have a pass. Otherwise libFastShortcuts won’t work. Therefore, it is recommended that this library be placed in the front script of the application.
Having defined the way in which we are going to use this library, we proceed to create our key combinations. For which we can create a small command in which we will put our default key combinations. This will also serve us in case the user after modifying the key combinations wants to restore to the original values.
We could have a command where on each line we would call the newShortcut handler.
command defaultShortcuts newShortcut "ctrl + d", "duplicateProduct" // other key combinations end defaultShortcuts on duplicateProduct answer “Duplicate product!” end duplicateProduct
How to save the defined combinations?
When we have our keybindings ready we need to save them to a property or to a file. For which we use the getShortcuts function. This function will return an array with all the key combinations. This arrangement is the one that we will save in a property or in a file with our key combinations. Immediately when starting the application we just have to load this information with the loadShortcuts command and that's it.
local tShortcuts put getShortcuts() into tShortcuts
Updating an existing key combination
In the development stage we define our default key combination map and then if the user wants to change one combination for another we use the updateShortcut command, it expects two parameters, the first is the new key combination and the second the previous one. Both parameters are required.
put "Ctrl+F" into newCombination put "Ctrl+D" into oldCombination updateShortcut newCombination,oldCombination
How to remove existing combinations?
If we want to remove a key combination, we do it with the removeShortcut command, passing the key combination as a parameter.
removeShortcut "Ctrl + D"
Each key name corresponds to the name given by the current platform, for example in Mac OS key name for code 65513 is Option, in Windows this key name corresponds to Alt. Also key “+” is named “Plus”.
Currently, there are no comment.Login to comment