[SOLVED] Macro to hold down/release (TOGGLE)

Announcements, questions
Post Reply
uronic
Posts: 2
Joined: 19 Mar 2021, 08:30

[SOLVED] Macro to hold down/release (TOGGLE)

Post by uronic » 19 Mar 2021, 08:33

Hi guys. I am new to this forum. The reason I am here is because I was wondering if anyone could tell me how I could make a macro on LuaMacros to toggle between holding down and releasing a key.

I have a key bound in Voicemeeter for push to talk. However, I want the ability to toggle the mic on and off as well with another key.

For example, I bind numpad 5 to hold down F24 when I press it the first time, then when I press it again it lets go of F24.

I hope you understood my request and I thank anyone in advance who is willing to help me do this. :D
Last edited by uronic on 19 Mar 2021, 19:28, edited 1 time in total.

Traveller
Posts: 5
Joined: 01 Mar 2021, 18:31

Re: Macro to hold down/release (TOGGLE)

Post by Traveller » 19 Mar 2021, 18:03

There is a way, with a couple caveats.
First of all, from what I've heard autohotkey is just the right sort of tool for this job, although I have never used it myself. You might check it out. Don't get me wrong, LuaMacros can do it too, but it might be overkill.
The other consideration is that this may not work 100%, for the simple reason that while we can send keydown and keyup events, the programme might still find out that the key isn't actually being held down (for example, if at any point it checks the key state of F24, it might see through your ruse).

What you're going to want to do is to start the programme in LuaMacros with the -k parameter (which prevents it from locking up your keyboard). I would advise that you simply create a shortcut which you put in your Startup folder so that it runs automatically when you boot your computer. Create a shortcut to LuaMacros.exe, and then open its properties and under target put:
"C:\path to LuaMacros\LuaMacros.exe" -k -r "C:\path to programme\PTTHotkey.lua"
The -k prevents it from blocking your keystrokes, and the -r parameter runs the programme specified.

Here's the code for the programme (I assumed above it was saved to "C:\path to programme\PTTHotkey.lua"):

Code: Select all

--Set LUAMacros to minimise to tray, then minimise- this is to hide it.
lmc.minimizeToTray = true
lmc_minimize()

--Set the name Keyboard to your keyboard, using a unique part of the identifier
--(See elsewhere on the forum for help on that)
lmc_device_set_name('Keyboard', 'EXSTRING123')
--Initialise PTTToggled
PTTToggled = false

--Set the event handler for the keyboard events
lmc_set_handler('Keyboard',function(button, direction)
    if(button == 0x65 and direction == 0)then   --on KeyUp of the Numpad 5
        PTTToggled = not PTTToggled  --change state of PTTToggled
        if (PTTToggled)then
            lmc_send_input(0x87, 0, 0)   --send KeyDown of F24
        else
            lmc_send_input(0x87, 0, 2)   --send KeyUp of F24
        end
    end
end)
If you get the impression that this is spaghetti code written by someone whose native language is C#, that's because it is. Sorry about that.
On line 7 you'll need to replace "EXSTRING123" with a portion of the hardware identifying string for your keyboard.
0x65 and 0x87 are the virtual keycodes of the keys we're interested in. If you need to change them, you can find the table at https://docs.microsoft.com/en-us/window ... -key-codes

uronic
Posts: 2
Joined: 19 Mar 2021, 08:30

Re: Macro to hold down/release (TOGGLE)

Post by uronic » 19 Mar 2021, 19:14

Traveller wrote:
19 Mar 2021, 18:03
There is a way, with a couple caveats.
First of all, from what I've heard autohotkey is just the right sort of tool for this job, although I have never used it myself. You might check it out. Don't get me wrong, LuaMacros can do it too, but it might be overkill.
...
I would like to implement your code in my existing script which I got from a youtube video

Code: Select all

lmc_device_set_name('macropad','683F61F')
lmc_print_devices()
lmc.minimizeToTray = true
lmc_minimize()

lmc_set_handler('macropad', function(button,direction)
  if (direction == 1) then return end
  if (button == 106) then
    lmc_send_keys('{F23}', 50)

    elseif (button == 103) then
    lmc_send_keys('{F21}', 50)

    elseif (button == 104) then
    lmc_send_keys('{F22}', 50)
	
	elseif (button == 105) then
    lmc_send_keys('{F20}', 50)
	
	elseif (button == 98) then
    lmc_send_keys('{F18}', 50)
	
	elseif (button == 110) then
    lmc_send_keys('{F17}', 50)
	
	elseif (button == 101) then
    lmc_send_keys('{F16}', 50)
  end
end
)
I tried doing it myself but my lack of knowledge in lua is stopping me from doing so. Again, thank you.

Edit: Wanted to include what I had so far which gave me an error

Code: Select all

lmc_device_set_name('macropad','683F61F')
lmc_print_devices()
lmc.minimizeToTray = true
lmc_minimize()

--Initialise PTTToggled
PTTToggled = false

lmc_set_handler('macropad', function(button,direction)
  if (direction == 1) then return end
  if (button == 106) then
    lmc_send_keys('{F23}', 50)

    elseif (button == 103) then
    lmc_send_keys('{F21}', 50)

    elseif (button == 104) then
    lmc_send_keys('{F22}', 50)
	
	elseif (button == 105) then
    lmc_send_keys('{F20}', 50)
	
	elseif (button == 98) then
    lmc_send_keys('{F18}', 50)
	
	elseif (button == 110) then
    lmc_send_keys('{F17}', 50)
	
	elseif (button == 101) then
    lmc_send_keys('{F16}', 50)
  end
  if (button == 106 and direction == 0) then
	PTTToggled = not PTTToggled
	if (PTTToggled) then
		lmc_send_input(106, 0 0)
	else
		lmc_send_input(106, 0 2)
	end
   end
end
)
Yet another edit:

Nvm I managed to fix my stupid mistake, thank you very much the macro works perfectly.

For anyone coming here from google in the future this is what I have

Code: Select all

lmc_device_set_name('macropad','683F61F')
lmc_print_devices()
lmc.minimizeToTray = true
lmc_minimize()

--Initialise PTTToggled
PTTToggled = false

lmc_set_handler('macropad', function(button,direction)
  if (direction == 1) then return end
  if (button == 106) then
    lmc_send_keys('{F23}', 50)

    elseif (button == 103) then
    lmc_send_keys('{F21}', 50)

    elseif (button == 104) then
    lmc_send_keys('{F22}', 50)
	
	elseif (button == 105) then
    lmc_send_keys('{F20}', 50)
	
	elseif (button == 98) then
    lmc_send_keys('{F18}', 50)
	
	elseif (button == 110) then
    lmc_send_keys('{F17}', 50)
  end
  if (button == 101 and direction == 0) then
	PTTToggled = not PTTToggled
	if (PTTToggled) then
		lmc_send_input(130, 0, 0)
	else
		lmc_send_input(130, 0, 2)
	end
   end
end
)

Post Reply