Page 1 of 1

Example script for OBS with usb keypad (homemade streamdeck)

Posted: 01 Jan 2018, 15:40
by mrKaine83
I'm setting up an external keypad to use with OBS. I tried using HID macros, but ran into the issue where the keypress occurs too fast for OBS to detect it when not the active window.

So now I need to use LUA macros, however I don't have scripting experience. I can see from another thread I found I'll need to use something like this:

Code: Select all

lmc_send_input(16, 0, 0) -- press shift
lmc_sleep(50)
lmc_send_input(65, 0, 0) -- press A
lmc_sleep(50)
lmc_send_input(65, 0, 2) -- release A
lmc_send_input(16, 0, 2) -- release shift
but I can't see how I tell it to listen for a specific keypress (from a specific keyboard). I've looked at many threads and youtube videos, but am unable to figure this out for myself.

Would someone be kind enough to post for me a complete example script that listens for a key on a specific device, then as above presses a key, pauses and releases the key? Then I can use it as a template to create for each key on my keypad.

Thank you in advance.

Re: Example script please?

Posted: 01 Jan 2018, 16:15
by mrKaine83
(edited this post to clarify & replaced with updated code)

I have surprised myself and figured it out!

For others who might make use of this, my complete script is as follows:

Code: Select all

lmc_print_devices() 									//List attached keyboards
lmc_device_set_name('streamdeck','54EFE92') 				//Give the keyboard a friendly name


lmc_set_handler('streamdeck', function(button,direction)		//Listen for key presses from named keyboard
  if (direction == 1) then return end 						//ignore button release
  if (button == 13) then								//if button is enter
    lmc_send_keys('^{ENTER}', 50)						//send key combo ctrl + enter then pause for 50ms

    elseif (button == 97) then
    lmc_send_keys('^{NUM1}', 50)

    elseif (button == 98) then
    lmc_send_keys('^{NUM2}', 50)

    elseif (button == 99) then
    lmc_send_keys('^{NUM3}', 50)

    elseif (button == 100) then
    lmc_send_keys('^{NUM4}', 50)

    elseif (button == 101) then
    lmc_send_keys('^{NUM5}', 50)

    elseif (button == 102) then
    lmc_send_keys('^{NUM6}', 50)

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

    elseif (button == 104) then
    lmc_send_keys('^{NUM8}', 50)

    elseif (button == 105) then
    lmc_send_keys('^{NUM9}', 50)

    elseif (button == 111) then
    lmc_send_keys('^{NUMDIVIDE}', 50)

    elseif (button == 106) then
    lmc_send_keys('^{NUMMULTIPLY}', 50)

    elseif (button == 109) then
    lmc_send_keys('^{NUMMINUS}', 50)

    elseif (button == 107) then
    lmc_send_keys('^{NUMPLUS}', 50)

    elseif (button == 8) then
    lmc_send_keys('^{BACKSPACE}', 50)

    elseif (button == 110) then
    lmc_send_keys('^{NUMDECIMAL}', 50)

    elseif (button == 96) then
    lmc_send_keys('^{NUM0}', 50)
  end
end
)
I found the keyboard ID number (for me 54EFE92) by running lmc_print_devices() and copying the part just before '&0&0000'. It might be helpful to unplug your main keyboard when you run it to identify which is the numpad.

I found the key numbers (i.e. enter is 13, num1 is 97) by running HID macros and observing the numbers in the 'event' field' when you press a key.

This script does all the usable keys on a num pad. To add more keys simply copy & paste one of the 'elseif' statements and replace the key number and key combo to send.

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 13 Jan 2018, 23:18
by TuecerPrime
I'm having the exact same issue myself, and don't understand your post sadly.

Is there a way you could assist me (and others) with a more detailed walkthrough?

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 06 Mar 2018, 17:32
by mrKaine83
Apologies for the long delay in replying.
I actually put together a video tutorial that should hopefully clear things up.
https://youtu.be/_cAJ0j3E5xs

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 07 Mar 2018, 09:36
by admin
Nice one.
Just 2 small comments:
- the "number" part from device id doesn't have to be from that specific place. It can be any part of that long value which is unique between other values (for other keyboards)
- you don't need HidMacros to get the key number. I believe those number are virtual key codes so you can find it at web (google it) or the easiest way is to log the value in script even if it is not used by the code. Something like the else part here: https://github.com/me2d13/luamacros/blo ... kstart.lua. Or just similar print command at the beginning of callback handler

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 02 Feb 2019, 03:11
by I3ordo
Hey, i am using luamacros for OBS aswell.. have you noticed OBS dont have a feature similar to "push-to-talk" where the "key press" transitions to a "scene" and goes back to the previous scene on "release".( imagine you have a separate scene that has bigger camera and an "un-muted" microhpne , you press talk and release to go back to gameplay scene with smaller camera window and no mic.)


I have been asking for a clarification on how button "press" or "releases" work but the person whom i ask help for does not seem to care...

Code: Select all

lmc_device_set_name('OBSDeck','133F0E8F')
lmc_print_devices()
lmc.minimizeToTray = true
lmc_minimize()

lmc_set_handler('OBSDeck', function(button,direction)
  if (direction == 1) then return end

--------------------------------------------------------

  if (button == 86) then                                                        --V
    lmc_send_keys('+{F13}', 50)

    elseif (button == 86) then                                                  --n
    lmc_send_keys('+{F14}', 50) 
      end
end
)
    
this code currently work on "release"s because of the handler at the top... but i am trying to make the first assignment work on press and second one release.

thank you for your effort in understanding the problem here.

thank you for trying

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 02 Feb 2019, 21:17
by admin
Person who you asked for help wrote some software and stated you need to know lua to use it. It's not enough to combine some scripts if you don't understand what they are doing.

To your problem: what about removing the direction condition from the begging and use something like
if (button == 86 and direction ==1) then
?

Btw I updated readme at github with disclaimer: https://github.com/me2d13/luamacros

Re: Example script for OBS with usb keypad (homemade streamdeck)

Posted: 06 Feb 2019, 08:10
by I3ordo
admin wrote:
02 Feb 2019, 21:17

To your problem: what about removing the direction condition from the begging and use something like
if (button == 86 and direction ==1) then
?
Thank you, this simple line of "help" solve all my problems regarding to this.