Skip to content

Instantly share code, notes, and snippets.

@garveen
Last active October 6, 2015 11:27
Show Gist options
  • Save garveen/6782765fff5242e582dc to your computer and use it in GitHub Desktop.
Save garveen/6782765fff5242e582dc to your computer and use it in GitHub Desktop.
2 function Logitech script
-- 罗技脚本示例
-- 此脚本被本人用于弱智核电
-- 国服 慕容行者#5979
-- 启动键编号
startButton = 7;
-- 结束键编号,必须小于等于5,否则有可能报错
endButton = 4;
-- 重置键编号
resetButton = 3;
-- 循环体时间,越小越灵敏,但是会更消耗CPU
checkInterval = 10;
-- 可以不管,你会写程序的话就知道我要干什么
function getLoop()
return loopMonk;
end
-- 可以不管,你会写程序的话就知道我要干什么
function getTimer()
return timerMonk;
end
-- 循环体,格式是 { 按键 , 持续毫秒 }
-- 键盘按键用双引号引起来,鼠标不要用引号
-- 最后一行不能有逗号
loopMonk = {
{"4", 100},
{"1", 100}
}
-- 定时触发,格式是 { 按键 , 按下多久 , 两次按键间隔 }
timerMonk = {
{1, 50, 100}
}
loop = getLoop();
timers = {}
function OnEvent(event, arg)
if(event == "MOUSE_BUTTON_PRESSED" and arg == startButton) then
local startTime;
local loop = getLoop();
local loopPoint = 1;
local loopPress = false;
local lastPressed;
local pressTime;
for i, v in pairs(getTimer()) do
addTimer(v[1], v[2], v[3], 'forever');
end
while(not IsMouseButtonPressed(endButton)) do
checkTimers();
if(IsMouseButtonPressed(resetButton)) then
release(lastPressed);
loopPoint = 1;
loopPress = false;
else
key = loop[loopPoint][1];
if(loopPress) then
if(GetRunningTime() >= loop[loopPoint][2] + pressTime) then
loopPress = false;
loopPoint = loopPoint + 1;
if(loopPoint > #loop) then
loopPoint = 1
end
release(key);
end
else
lastPressed = key;
pressTime = GetRunningTime();
loopPress = true;
press(key);
end
end
Sleep(checkInterval);
end
releaseTimers();
release(lastPressed);
end
end
loopPoint = 1;
function nextLoop()
local current = loop[loopPoint];
if(#current < 3) then
current[3] = current[2];
end
addTimer(current[1], current[2], current[3], "loop");
loopPoint = loopPoint + 1;
if(loopPoint > #loop) then
loopPoint = 1;
end
end
function addTimer(key, lasts, interval, type)
local newTimer = {}
newTimer["key"] = key;
newTimer["lasts"] = lasts;
newTimer["interval"] = interval;
newTimer["type"] = type;
newTimer["start"] = GetRunningTime();
newTimer["status"] = "pressing";
table.insert(timers, newTimer);
press(key);
end
function checkTimers()
local now = GetRunningTime();
for index, timer in pairs(timers) do
if(timer["status"] == "pressing") then
if(now >= timer["start"] + timer["lasts"]) then
release(timer["key"]);
if(timer["type"] == "forever") then
timer["status"] = "waiting";
else
table.remove(timers, index);
nextLoop();
end
end
elseif (timer["status"] == "waiting") then
if(now >= timer["start"] + timer["interval"]) then
press(timer["key"]);
timer["status"] = "pressing";
timer['start'] = GetRunningTime();
end
end
end
end
function releaseTimers()
for index, timer in pairs(timers) do
release(timer["key"]);
end
timers = {}
end
function press(key)
if (type(key) == "string") then
if(key == "") then return end
PressKey(key);
else
PressMouseButton(key);
end
end
function release(key)
if (type(key) == "string") then
if(key == "") then return end
ReleaseKey(key);
elseif (type(key) == "number") then
ReleaseMouseButton(key);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment