Cross-locale reprograming of second kb into a hotkeys kb

Documentation. First post is always kind of manual page, being continuously updated by author. Following posts are discussion about the topic.
Post Reply
Mik Urrey
Posts: 2
Joined: 24 Jan 2019, 12:34

Cross-locale reprograming of second kb into a hotkeys kb

Post by Mik Urrey » 24 Jan 2019, 13:05

Hello folks!
I had a goal to reprogram my second keyboard into a hotkeys keyboard without 3rd party apps.
It was not so easy cause I got errors using lmc_send_keys function. I don't know what is the reason, but seems RU locale is one.
On the board I found some posts about lmc_send_input and this way works fine for me.
Here's an example.

Code: Select all

local pathWithFilename="D:\\LuaMacros\\samples\\";
local kbID =  '226465';

lmc.minimizeToTray = true
lmc_minimize()
clear()
lmc_print_devices()
-- assign logical name to macro keyboard
lmc_device_set_name('MACROS', kbID);
local codes = {
      ["ctrl"]  = 17,
      ["alt"]   = 18,
      ["shift"] = 16,
      ["enter"] = 13,
      ["win"]   = 91,
      ["tab"]   = 9,
      ["space"] = 32,
      ["back"]  = 8,
      ["app"]   = 93,
      ["left"]  = 37,
      ["right"] = 39,
      ["up"]    = 38,
      ["down"]  = 40,
      ["insert"]= 45,
      ["delete"]= 46,
      ["home"]  = 36,
      ["end"]   = 35,
      ["pup"]   = 33,
      ["esc"]   = 27,
      ["plus"]  = 187,
      ["screen"]= 255,
};
-- define callback for whole device
lmc_set_handler('MACROS', function(button, direction)
  if (direction == 1) then
     return
  end  -- ignore down
  local config = {};
  for line in io.lines(pathWithFilename .. "all.txt") do
    local data = {};
      for part in string.gmatch(line, "[^%s]+") do
        data[#data+1] = part;
      end;
    config[tonumber(data[1])] = data[2];
  end;
  if type(config[button]) == "string" then
      print('Found: ' .. button .. ' = ' .. config[button]);

      for s in string.gmatch(config[button], "[^+]+") do
           if type(codes[s]) == "number" then
               k = codes[s];
           else
               k = string.byte(s);
           end;
           lmc_send_input(k, 0, 0);
      end;

      for s in string.gmatch(config[button], "[^+]+") do
           if type(codes[s]) == "number" then
               k = codes[s];
           else
               k = string.byte(s);
           end;
           lmc_send_input(k, 0, 2);
      end;
  else
      print('Not yet assigned: ' .. button)
  end
end);
Before you try to use it, you must set your own pathWithFilename (directory contains your script) and place an "all.txt" file into the directory.

all.txt example:

Code: Select all

17 ctrl+alt+A Ctrl
13 ctrl+enter Enter
18 ctrl+shift+I Web inspector
(numeric key code /space/ hotkey /space/ comment)

When you start the script you will see a devices list. Get your keyboard ID from the list and set kbID.
Restart the script and enjoy :)

All codes of all unassigned keys will be printed in LuaMacros console while typing.
Hotkeys adding into the txt file works on-the-fly.
You can run the script on system startup using a shortcut like this:
D:\LuaMacros\LuaMacros.exe "D:\LuaMacros\samples\my.lua" -r

admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Re: Cross-locale reprograming of second kb into a hotkeys kb

Post by admin » 25 Jan 2019, 10:09

Nice one.
You could read the file on script start and keep the definition in memory to not re-open file on every key press. But then you would need to re-run script to reflect changes in file.
Petr Medek
LUAmacros author

Mik Urrey
Posts: 2
Joined: 24 Jan 2019, 12:34

Re: Cross-locale reprograming of second kb into a hotkeys kb

Post by Mik Urrey » 25 Jan 2019, 11:29

Yes, I did it for ability to apply config without restart.
Performance losses are imperceptible cause the file size is very very small.

Thank you for this great utility. I hope the project will grow and you will add more functions allowing to make portable and configurable scripts :)

Post Reply