Reusable keyboard.stroke() function - same function can be used for all keystroke buttons
I noticed a lot of the remotes have tons of functions which essentially do the same thing. Here's a function that can be used by all your buttons that send keystrokes in case it's useful to anyone. Speeds up development a lot, and keeps things a lot cleaner when you don't have to add a whole new function (and remember a new function name) every time you want to send a keystroke.
You can pass a parameter string value to a function from a button by appending it to the function name after a comma, like this:
<button text="a button" ontap="theFunction,someValue" />
I used that approach to call this function from all my buttons that send keystrokes:
actions.stroker = function (instring) -- fnf4 - pgup - pgdn - dele - xtab - SPECIAL KEYS THAT CAN'T BE CALLED BY UTF-8 if string.sub(instring,1,4) == "XTAB" then kb.press("tab"); elseif string.sub(instring,1,4) == "fnf4" then kb.press("f4"); elseif string.sub(instring,1,4) == "pgup" then kb.press("pageup"); elseif string.sub(instring,1,4) == "pgdn" then kb.press("pagedown"); elseif string.sub(instring,1,4) == "dele" then kb.press("delete"); else local theChar = string.sub(instring,1,4)+0; --convert to number local mods = string.sub(instring,5); --layout.thebutton.text = mods; local isk = kb.iskey(theChar); local S = string.find(mods, "S") ;--add shift local C = string.find(mods, "C") ;--add control local A = string.find(mods, "A") ;--add alt --type output with mods if S then kb.down("shift") end ; if C then kb.down("ctrl") end ; if A then kb.down("alt") end ; if isk then kb.stroke(theChar) end ; if S then kb.up("shift") end ; if C then kb.up("ctrl") end ; if A then kb.up("alt") end ; end ; end
You send the function the UTF-8 code for the key you want sent by the function (Here's a chart of UTF-8 codes), followed by a C, S, and/or A if you want control, shift, or alt pressed when the key is sent.
I used UTF-8 where possible to work around some apparent bugs in UR relating to recognizing key names. UTF-8 seems reliable, where key name recognition seems not to be reliable at all, at least on Linux.
Here are some example function calls:
<button ontap="stroker,XTAB" /><!-- canvas only: tab --> <button ontap="stroker,0x68" /><!-- color history: H --> <button ontap="stroker,0x6eS" /><!-- min shade sel: shift+ N --> <button ontap="stroker,0x6dS" /><!-- mypaint shade sel: shift + M --> <button ontap="stroker,0x61CS" /><!-- deselect: ctrl + shift + A --> <button ontap="stroker,0x73CA" /><!-- save incremental version: ctrl+alt+s -->
There is however one problem with doing it that way.
If someone would like to make an widget for one of the buttons it is hard to do because you are missing the help metadata that I now discovered is not documented.
But it tells the app what an action do and what kind of parameters it accepts.
[email protected] Open file or folder on computer. [email protected] path:string The path to the file actions.open = function (path)
This is an example from the file manager remote. I will make sure to add some documentation for this.
Edit: Added some more documentation https://github.com/unifiedremote/Docs/blob/master/concepts/remote.md
I haven't played with widgets yet, so I'm not sure what the practical impact of what you're saying would be...
Are you saying that this function won't work for a widget at all because it is reusable, or just that I should add some help comments like this on my function?
[email protected] Create a keystroke from instring. [email protected] instring: input string - first 4 characters are UTF-8 code, followed immediately by a C, S and/or A if Control, Shift or Alt are to be pressed.
What exactly does the app do with the help comments?