Skip to content

Instantly share code, notes, and snippets.

@ShadOoW
Last active November 13, 2015 07:56
Show Gist options
  • Save ShadOoW/c6034f6d9d9698d0591b to your computer and use it in GitHub Desktop.
Save ShadOoW/c6034f6d9d9698d0591b to your computer and use it in GitHub Desktop.
function bindKey(boundKey, boundPressAction, boundReleaseAction, boundTick, boundDelay)
--default values
boundPressAction = boundPressAction or function() end
boundReleaseAction = boundReleaseAction or function() end
boundTick = boundTick or 100
boundDelay = boundDelay or 300
--helper functions
--Note: couldn't use lua os.time os.clock to achieve this so i used io.popen(date... i'm not ashamed :D
function getSystemTime()
fh = assert(io.popen("date +%s%N | cut -b1-13"))
time = fh:read("*l")
fh:close()
return time
end
--time calculation vars
startMark = getSystemTime()
elapsedMark = 0
--action flags
isDelayElapsed = false
isTicked = false
--stop existing keygrabber
keygrabber.stop()
keygrabber.run(function(mod, key, event)
if boundKey == key then
if event == "press" then
elapsedMark = getSystemTime() - startMark
if isDelayElapsed then
if elapsedMark > boundTick then
isTicked = true;
startMark = getSystemTime()
boundPressAction()
end
else
if elapsedMark > boundDelay then
isDelayElapsed = true;
startMark = getSystemTime()
end
end
else
if not isTicked then
boundReleaseAction()
end
--stop keygrabber on release
keygrabber.stop()
end
end
end)
end
--Use like this - all except first parameter are optional
awful.key({ }, "XF86AudioPrev", function () bindKey("XF86AudioPrev", function() awful.util.spawn("mocp -k -2") end, function() awful.util.spawn("mocp --previous") end, 100, 300) end),
awful.key({ }, "XF86AudioNext", function () bindKey("XF86AudioNext", function() awful.util.spawn("mocp -k +2") end, function() awful.util.spawn("mocp --next") end, 100, 300) end),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment