One Button Push - Three Unique Macro Outputs

Announcements, questions
Post Reply
surge42
Posts: 3
Joined: 26 Sep 2019, 02:33

One Button Push - Three Unique Macro Outputs

Post by surge42 » 19 Oct 2019, 17:46

Hey Petr,

I just received a two button keyboard needed for my idea. I'm going to use this keyboard to cycle through three macros when one of the two keys (key 88) is clicked repeatedly. Petr was a hero and created the following script for me.
  • I noticed that lmc_assign_keyboard is different than what most use which is lmc_device_set_name. Is my syntax correct for lmc_assign_keyboard?
  • How do you specify the key number to this script? I'd like to execute the macros when key 88 is pressed. Would this be correct mc_send_input(string.byte(88), 0, 0) ?
  • In regards to what is printed when the key is pressed where is that defined? I'm looking for something like "+^%{m}"
Much thanks Petr.

Code: Select all

-- this example sends 3 different key combinations on 'A' key press

-- choose keyboard for macros, assign logical name
lmc_assign_keyboard('MACROS','39505B2E');


-- on every trigger following keys with alt + shift are send - looped with every press
keysToSend = { 'A', 'B', 'C'}
currentIndex = 1

-- send any key (letter) as keystroke. Any key defined by the keyboard is used as THE key.
function press(key)
  lmc_send_input(string.byte(key), 0, 0) -- press
  lmc_send_input(string.byte(key), 0, 2) -- release
end

-- wraps function call with shift press
function withShift(callback)
  lmc_send_input(16, 0, 0) -- press
  callback()
  lmc_send_input(16, 0, 2) -- release
end

-- wraps function call with alt press
function withAlt(callback)
  lmc_send_input(18, 0, 0) -- press
  callback()
  lmc_send_input(18, 0, 2) -- release
end

-- define callback for 'A' key
lmc_set_handler('MACROS',65,0,function(button, direction)
  withAlt(function() withShift(function() press(keysToSend[currentIndex]) end) end)
  currentIndex = (currentIndex == #keysToSend and 1 or currentIndex + 1)
end) 

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

Re: One Button Push - Three Unique Macro Outputs

Post by admin » 21 Oct 2019, 08:56

Usually you assign logical name to device using some part of its id by lmc_device_set_name. See details here http://hidmacros.eu/forum/viewtopic.php?f=12&t=248. lmc_assign_keyboard is quick-start function and you always need to press some key to identify keyboard. So good for testing, not that good for finished scripts.

lmc_send_input expect number - key code. String.byte is utility function to get this number for letters. For special keys you rather use number directly and you can lookup this number in send_key (windows function, not lmc_send_key) documentation. E.g.https://docs.microsoft.com/cs-cz/window ... -key-codes. The page has key code in hexa number, but you can google "virtual key codes" and find something with decimal numbers

Alt and shift are done by wrapper functions withAlt and withShift. They keys to repeat are defined in array keysToSend. If you need to cycle through different combinations of alt, shift, ctrl and key the code would be more complicated...
Petr Medek
LUAmacros author

surge42
Posts: 3
Joined: 26 Sep 2019, 02:33

Re: One Button Push - Three Unique Macro Outputs

Post by surge42 » 21 Oct 2019, 14:14

:D Much thanks and respect to you Petr.

Post Reply