Page 1 of 1

Device names

Posted: 19 Apr 2015, 22:08
by admin
LuaMacros work with devices. The application usually executes some action (via Lua script) triggered by some device event. The device can be keyboard, joystick, game pad but also COM port. Device event is usually key/button press or release. For COM port the event usually happens when some data is received.
Actions which are executed by device event are pieces of Lua code. Those pieces needs to be bound to specific device and (optionally, in some cases) specific button. When you start LuaMacros it detects physical devices connected to your computer. Those devices usually have some name defined by manufacturer (default names for game devices) or some ugly very long string - kind of system identifier - for devices.
To make LuaMacros script nice for reading and easy for transfer between different computers every device that should act as action trigger needs to have some (nice) logical name assigned first.

Let's have an example. These are devices what LuaMacros can detect

Code: Select all

<unassigned>  :  Saitek X52 Flight Control System [0] :  game
<unassigned>  :  Saitek Pro Flight Rudder Pedals [0] :  game
<unassigned>  :  BU0836A Interface [0] :  game
<unassigned>  :  \\?\HID#VID_1C4F&PID_0002&MI_00#9&826BD90&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [327833] :  keyboard
<unassigned>  :  \\?\HID#VID_04FC&PID_05D8&MI_00#7&1BDC3055&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [65605] :  keyboard
<unassigned>  :  \\?\HID#VID_046D&PID_C52E&MI_00#9&12234230&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [720963] :  keyboard
Total number of devices: 6
This is output of lmc_print_devices() command. The "<unassigned>" is place where logical name is printed so in this example unassigned means no logical name was assigned yet. Second item is system id; quite readable for game devices and ugly value for keyboards. Last item is type - keyboard, game device, COM, etc.

To assign logical name to some device there are several functions available. The most common is lmc_device_set_name. It has 2 arguments:
  • logical name - to be assigned
  • regular expression to match system id of the device. On multiple match first device is used.
Back to our example, following set of commands

Code: Select all

lmc_device_set_name('LB', 'BU0836A')
lmc_device_set_name('KBD1', '826BD90')
lmc_device_set_name('KBD2', '1BDC3055')
Will result to 3 devices with assigned logical names:

Code: Select all

<unassigned>  :  Saitek X52 Flight Control System [0] :  game
<unassigned>  :  Saitek Pro Flight Rudder Pedals [0] :  game
LB  :  BU0836A Interface [0] :  game
KBD1  :  \\?\HID#VID_1C4F&PID_0002&MI_00#9&826BD90&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [327833] :  keyboard
KBD2  :  \\?\HID#VID_04FC&PID_05D8&MI_00#7&1BDC3055&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [65605] :  keyboard
<unassigned>  :  \\?\HID#VID_046D&PID_C52E&MI_00#9&12234230&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD} [720963] :  keyboard
Total number of devices: 6
For keyboards there's one more function. It's easier to use as you don't need any regular expression, but it requires user interaction. Command lmc_assign_keyboard('MACROS'); will show popup panel in LuaMacros application asking user to press any key at some keyboard. After the press this keyboard will have logical name provided as the argument of lmc_assign_keyboard function.

Having logical names assigned you can write Lua functions as event handlers (callbacks). See another how-to for instructions and examples (to be written :-) - Apr 2015).

In the future (written in Apr 2015) there might be functions for multiple regexp assignment (working with string map) and also for storing/loading these maps from/to file (kind of properties file). However you can easily do the same with proper lmc_device_set_name functions so I'm not that motivated.