ruk·si

⌨️ Logitech Keyboard Scripting

Updated at 2015-01-17 00:56

Basic example how to script a Logitech keyboard.

-- Sleep(10) in milliseconds
-- PressKey("a", "b"); ReleaseKey("a", "b"); Sleep(5);
-- PressAndReleaseKey("a", "b"); Sleep(5);
-- IsModifierPressed("shift") -- or "ctrl" or "alt"
-- PressAndReleaseMouseButton(1); Sleep(5); -- from 1 to 5
-- PressMouseButton(1); ReleaseMouseButton(1); Sleep(5);
-- MoveMouseWheel(3) -- 3 clicks up
-- x, y = GetMousePosition() -- 0, 0 -> 65535, 65535
-- MoveMouseRelative(10, 10) -- right bottom
-- MoveMouseTo(1000, 1000)

function Log(msg, ...)
    OutputLogMessage(msg, ...)
end

function LogLn(msg, ...)
    OutputLogMessage(msg .. "\n", ...)
end

function LogMouse()
    x, y = GetMousePosition()
    LogLn("Mouse: " .. x .. ", " .. y)
end

function Mode1Event(event, arg)
    if (event == "G_PRESSED") then
        if (arg == 5) then
            LogLn("Mode 1 G5 pressed")
        elseif (arg == 6) then
            LogMouse()
        end
    end
    if (event == "G_RELEASED") then
         if (arg == 5) then
            LogLn("Mode 1 G5 released")
        end
    end
end

local x1, y1;
local x2, y2;
local x3, y3;
local x4, y4;
local x5, y5;
local x6, y6;
function Mode2Event(event, arg)
    if (event == "G_PRESSED") then
        if (IsModifierPressed("ctrl")) then
            if (arg == 1) then x1, y1 = GetMousePosition();
            elseif (arg == 2) then x2, y2 = GetMousePosition();
            elseif (arg == 3) then x3, y3 = GetMousePosition();
            elseif (arg == 4) then x4, y4 = GetMousePosition();
            elseif (arg == 5) then x5, y5 = GetMousePosition();
            elseif (arg == 6) then x6, y6 = GetMousePosition();
            end
        else
            if (arg == 1) then
                MoveMouseTo(x1, y1);
                PressAndReleaseMouseButton(1);
            elseif (arg == 2) then
                MoveMouseTo(x2, y2);
                PressAndReleaseMouseButton(1);
            elseif (arg == 3) then
                MoveMouseTo(x3, y3);
                PressAndReleaseMouseButton(1);
            elseif (arg == 4) then
                MoveMouseTo(x4, y4);
                PressAndReleaseMouseButton(1);
            elseif (arg == 5) then
                MoveMouseTo(x5, y5);
                PressAndReleaseMouseButton(1);
            elseif (arg == 6) then
                MoveMouseTo(x6, y6);
                PressAndReleaseMouseButton(1);
            end
        end
    end
end

function Mode3Event(event, arg)
end

function OnEvent(event, arg)
    currentMode = GetMKeyState()
    if (currentMode == 1) then
        Mode1Event(event, arg);
    elseif (currentMode == 2) then
        Mode2Event(event, arg);
    else
        Mode3Event(event, arg);
    end
end