- 673
- 372
Прошастал весь раздел Разработки LUA и не нашел подобной темы, поэтому решил дропнуть хоткеи с рабочими боковыми клавишами.
ВБольшая часть функции была взята отсюда
ВБольшая часть функции была взята отсюда
Lua:
local inicfg = require "inicfg"
local imgui = require 'mimgui'
local vkeys = require 'vkeys'
local encoding = require 'encoding'
encoding.default = 'CP1251'
local u8 = encoding.UTF8
local hotkey_w = imgui.new.bool()
sw, sh = getScreenResolution()
local ini = inicfg.load({
main = {
bind = "Свободно"
}
}, "settings.ini")
local tHotKeyData = {
edit = nil,
save = {},
lasted = os.clock(),
}
function main()
if not isSampLoaded() then return end
while not isSampAvailable() do wait(100) end
sampRegisterChatCommand("hotkey", function() hotkey_w[0] = not hotkey_w[0] end)
while true do
wait(0)
if isKeysDown(ini.main.bind) and (os.clock() - tHotKeyData.lasted > 0.1) then
sampAddChatMessage("Говно выпущено", -1)
end
end
end
local hotkeyWindow = imgui.OnFrame(
function() return hotkey_w[0] end,
function(self)
imgui.SetNextWindowSize(imgui.ImVec2(500, 500), imgui.Cond.FirstUseEver)
imgui.SetNextWindowPos(imgui.ImVec2(sw * 0.5 , sh * 0.5),imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
imgui.Begin('Mimgui Hotkeys', hotkey_w)
imgui.Text(u8'Сделать очередное говно: ')
imgui.SameLine()
imgui.HotKey("##21312", ini.main, 'bind', "Свободно", string.find(ini.main.bind, '+') and 150 or 75)
imgui.End()
end
)
function getDownKeys()
local curkeys = ''
local bool = false
for k, v in pairs(vkeys) do
if isKeyDown(v) and (v == VK_MENU or v == VK_CONTROL or v == VK_SHIFT or v == VK_LMENU or v == VK_RMENU or v == VK_RCONTROL or v == VK_LCONTROL or v == VK_LSHIFT) then
if v ~= VK_MENU and v ~= VK_CONTROL and v ~= VK_SHIFT then
curkeys = v
end
end
end
for k, v in pairs(vkeys) do
if isKeyDown(v) and (v ~= VK_MENU and v ~= VK_CONTROL and v ~= VK_SHIFT and v ~= VK_LMENU and v ~= VK_RMENU and v ~= VK_RCONTROL and v ~= VK_LCONTROL and v ~= VK_LSHIFT) then
if string.len(tostring(curkeys)) == 0 then
curkeys = v
return curkeys,true
else
curkeys = curkeys .. ' ' .. v
return curkeys,true
end
bool = false
end
end
return curkeys, bool
end
function imgui.GetKeysName(keys)
if type(keys) ~= 'table' then
return false
else
local tKeysName = {}
for k = 1, #keys do
tKeysName[k] = vkeys.id_to_name(tonumber(keys[k]))
end
return tKeysName
end
end
function string.split(inputstr, sep)
if sep == nil then
sep = '%s'
end
local t={} ; i=1
for str in string.gmatch(inputstr, '([^'..sep..']+)') do
t[i] = str
i = i + 1
end
return t
end
function isKeysDown(keylist, pressed)
if keylist == nil then return end
keylist = (string.find(keylist, '.+ %p .+') and {keylist:match('(.+) %p .+'), keylist:match('.+ %p (.+)')} or {keylist})
local tKeys = keylist
if pressed == nil then
pressed = false
end
if tKeys[1] == nil then
return false
end
local bool = false
local key = #tKeys < 2 and tKeys[1] or tKeys[2]
local modified = tKeys[1]
if #tKeys < 2 then
if wasKeyPressed(vkeys.name_to_id(key, true)) and not pressed then
bool = true
elseif isKeyDown(vkeys.name_to_id(key, true)) and pressed then
bool = true
end
else
if isKeyDown(vkeys.name_to_id(modified,true)) and not wasKeyReleased(vkeys.name_to_id(modified, true)) then
if wasKeyPressed(vkeys.name_to_id(key, true)) and not pressed then
bool = true
elseif isKeyDown(vkeys.name_to_id(key, true)) and pressed then
bool = true
end
end
end
if nextLockKey == keylist then
if pressed and not wasKeyReleased(vkeys.name_to_id(key, true)) then
bool = false
else
bool = false
nextLockKey = ''
end
end
return bool
end
function imgui.HotKey(name, path, pointer, defaultKey, width)
local width = width or 90
local cancel = isKeyDown(0x08)
local tKeys, saveKeys = string.split(getDownKeys(), ' '),select(2,getDownKeys())
local name = tostring(name)
local keys, bool = path[pointer] or defaultKey, false
local sKeys = keys
for i=0,2 do
if imgui.IsMouseClicked(i) then
tKeys = {i==2 and 4 or i+1}
saveKeys = true
end
end
if tHotKeyData.edit ~= nil and tostring(tHotKeyData.edit) == name then
if not cancel then
if not saveKeys then
if #tKeys == 0 then
sKeys = (math.ceil(imgui.GetTime()) % 2 == 0) and '______' or ' '
else
sKeys = table.concat(imgui.GetKeysName(tKeys), ' + ')
end
else
path[pointer] = table.concat(imgui.GetKeysName(tKeys), ' + ')
tHotKeyData.edit = nil
tHotKeyData.lasted = os.clock()
inicfg.save(ini, "settings.ini")
end
else
path[pointer] = defaultKey
tHotKeyData.edit = nil
tHotKeyData.lasted = os.clock()
inicfg.save(ini, "settings.ini")
end
end
imgui.PushStyleColor(imgui.Col.Button, imgui.GetStyle().Colors[imgui.Col.FrameBg])
imgui.PushStyleColor(imgui.Col.ButtonHovered, imgui.GetStyle().Colors[imgui.Col.FrameBgHovered])
imgui.PushStyleColor(imgui.Col.ButtonActive, imgui.GetStyle().Colors[imgui.Col.FrameBgActive])
if imgui.Button((sKeys ~= '' and u8(sKeys) or u8'Свободно') .. '## '..name, imgui.ImVec2(width, 0)) then
tHotKeyData.edit = name
end
imgui.PopStyleColor(3)
return bool
end