Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turning cheap USB numeric keyboard into 4x4 drum pad controller for Ableton with LuaMacros
-- assign logical name to macro keyboard
lmc_assign_keyboard('DRUMPADCONTROLLER');
-- alternatively, set the logical name to a specific keyboard
-- lmc_device_set_name('DRUMPADCONTROLLER', '295601EB')
-- list all devices
lmc_print_devices();
--keymapping:
map = {};
map[97] = 'a'; -- numeric keyboard '1' <= ableton 'C'
map[98] = 'w'; -- numeric keyboard '2' <= ableton 'C#'
map[99] = 's'; -- numeric keyboard '3' <= ableton 'D'
map[13] = 'e'; -- numeric keyboard 'ENTER' <= ableton 'D#'
map[100] = 'd'; -- numeric keyboard '4' <= ableton 'E'
map[101] = 'f'; -- numeric keyboard '5' <= ableton 'F'
map[102] = 't'; -- numeric keyboard '6' <= ableton 'F#'
map[8] = 'g'; -- numeric keyboard 'BACKSPACE' <= ableton 'G'
map[103] = 'y'; -- numeric keyboard '7' <= ableton 'G#'
map[104] = 'h'; -- numeric keyboard '8' <= ableton 'A'
map[105] = 'u'; -- numeric keyboard '9' <= ableton 'A#'
map[107] = 'j'; -- numeric keyboard '+' <= ableton 'B'
map[144] = 'k'; -- numeric keyboard 'NUMLOCK' <= ableton 'C 8a+'
map[111] = 'o'; -- numeric keyboard '/' <= ableton 'C# 8a+'
map[106] = 'l'; -- numeric keyboard '*' <= ableton 'D 8a+'
-- map[109] = ''; -- numeric keyboard '-' <= not assigned
map[96] = 'z'; -- numeric keyboard '0' <= ableton 'Octave UP'
map[48] = 'x'; -- numeric keyboard '000' <= ableton 'Octave DOWN'
map[110] = ' '; -- numeric keyboard '.' <= ableton 'Play/Stop'
-- assigning the same values for the NUMLOCK OFF keycodes of the same keys:
map[45] = map[96];
map[46] = map[110];
map[35] = map[97];
map[40] = map[98];
map[34] = map[99];
map[37] = map[100];
map[12] = map[101];
map[39] = map[102];
map[36] = map[103];
map[38] = map[104];
map[33] = map[105];
-- initializing the 't' tables with zeros
t = {};
for i = 0, 300
do
t[i] = 0;
end
-- initializing triple zero key workaround counter
tripleZeroCounter = 0;
-- defining callback for whole device
lmc_set_handler('DRUMPADCONTROLLER', function(button, direction)
-- key pressed and was not pressed before
if(direction == 1 and t[button] == 0)
then
target=map[button];
-- NUMLOCK workaround (send another NUMLOCK to toggle back to original state)
if (button == 144)
then
lmc_send_input(144, 0, 2); -- press NUMLOCK
lmc_send_input(144, 0, 0); -- release NUMLOCK
-- print('reversing NUMLOCK to original state');
end
-- triple zero workaround (use first and ignore other two)
if (button == 48)
then
tripleZeroCounter = tripleZeroCounter + 1;
if (tripleZeroCounter == 3)
then
tripleZeroCounter = 0;
end
if (tripleZeroCounter ~= 1)
then
return;
end
end
if map[button]
then
lmc_send_keys(target); -- press the key
print('Pressed button ' .. button .. ' and send key "' .. target .. '"');
else
print('Pressed button ' .. button .. ' and nothing defined to be done');
end
t[button] = 1;
elseif (direction == 0)
then
print('Released button ' .. button);
t[button] = 0;
else
-- in this project just do nothing in this case
-- print('Still pressed and repeating button ' .. button);
end
end)
@mustache1up

This comment has been minimized.

Copy link
Owner Author

@mustache1up mustache1up commented Nov 12, 2017

This code is part of this post in LuaMacros forum:

[Turning cheap USB numeric keyboard into 4x4 drum pad controller for Ableton] (http://www.hidmacros.eu/forum/viewtopic.php?f=12&t=529)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment