combination of keys press and hold behaviour

Announcements, general discussion
Post Reply
weaking1
Posts: 2
Joined: 06 Oct 2021, 09:10

combination of keys press and hold behaviour

Post by weaking1 » 06 Oct 2021, 09:48

Hello! i'm really sorry for this question, but i cannot find solution for my problem
I need to make a script that, when one button is pressed, will press the combination of keys and "holds" it (keeps it pressed), until i release the button


-Why i need this
i have an extention floating window, which appears only when i keep pressed combination ctrl+H (i press it and hold), and when i release this combination - window dissapears.

-My trials and errors
i'm really a stranger to luamacro and using it only to program macros for photoshop, so only possible way for me to create macro - is to compilate one from ready solutions, but this one is too complex for me to handle by this way. I've tried to rewrite standart example coming with luamacro, but i think i'm just doing dumb stuff. If you can help me with pure solution i'll be really thankful, but still, i'll attach my really stupid script, maybe it'll be helpful

Code: Select all

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

-- send any key (letter) as keystroke
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 ctrl press
function withCTRL(callback)
  lmc_send_input(17, 0, 0) -- press
  callback()
  lmc_send_input(17, 0, 2) -- release
end

-- define callback for 'A' key
lmc_set_handler('MACROS',65,0,function(button, direction)
  withCTRL(function() press('H') end)
end)
This script does action only when the A button is released, and it carries out only one ctrl+h press,so the window is appearing only for a fraction of a second, and when i'm changing:

Code: Select all

lmc_set_handler('MACROS',65,0,function(button, direction)
to

Code: Select all

lmc_set_handler('MACROS',65,2,function(button, direction)
it simulates lots of consistent presses (good for qte's i guess, but not for the job) - window flickers, but cannot be usable

I cannot simulate the press and hold for the "ctrl+h" key combination, needed for this window to be used

Please, can you help me to create script that can simulate press and hold behaviour for this combination of keys?
i can simulate this behavior for one button using

Code: Select all

lmc_set_handler('MACROS1',function(button, direction)   

if (button == string.byte('D')) then -- if m
    if (direction == 1) then lmc_send_input(16, 0, 0) -- press shift
    elseif (direction == 0) then lmc_send_input(16, 0, 2) -- release shift
    end   
It helps me to replace shift button, for example, on my additional keyboard, and i can draw straight lines when i'm holding shift replacement button.

but i can't understand how to do this for the combination like ctrl+h

also i can use ahk, and it works, but i don't know how to separate this script only for my second hid keyboard and i think it will be much more comfortable just to use only luamacros.

Thanks

weaking1
Posts: 2
Joined: 06 Oct 2021, 09:10

Re: combination of keys press and hold behaviour

Post by weaking1 » 09 Oct 2021, 14:21

ok i've got it

Code: Select all

lmc_assign_keyboard('MACROS');

-- define callback for whole device
lmc_set_handler('MACROS',function(button, direction)

keysToPush1 = string.byte('V') -- button you press on keyboard
keysToSend1 = string.byte('H') -- button you need to push in the combination

function withCtrlAct(callback)
  lmc_send_input(17, 0, 0)
  callback()
end

function withCtrlPass(callback)
  lmc_send_input(17, 0, 2)
  callback()
end

function press(key)
  lmc_send_input(keysToSend1, 0, 0)
end

function release(key)
  lmc_send_input(keysToSend1, 0, 2)
end

  if (button == keysToPush1) then
    if (direction == 1) then withCtrlAct(function() press() end)
    elseif (direction == 0) then withCtrlPass(function() release() end)
    end

 end
end) 

Josick
Posts: 6
Joined: 19 Feb 2021, 21:07
Location: Britanny
Contact:

Re: combination of keys press and hold behaviour

Post by Josick » 17 Oct 2021, 16:46

Same result, but I prefer this order ;) :

Code: Select all

function withCtrlAct(callback)
  lmc_send_input(17, 0, 0)
  callback()
end

function withCtrlPass(callback)
  lmc_send_input(17, 0, 2)
  callback()
end

function press(key)
  lmc_send_input(key, 0, 0)
end

function release(key)
  lmc_send_input(key, 0, 2)
end

keysToPush1 = string.byte('V') -- button you press on keyboard
keysToSend1 = string.byte('H') -- button you need to push in the combination

lmc_assign_keyboard('MACROS');

-- define callback for whole device
lmc_set_handler('MACROS',function(button, direction)
  if (button == keysToPush1) then
    if (direction == 1) then withCtrlAct(function() press(keysToSend1) end)
    elseif (direction == 0) then withCtrlPass(function() release(keysToSend1) end)
    end

 end
end) 

Post Reply