Исходник mimgui - HotKeys | Боковые клавиши мыши + сохранение inicfg

ewin

Известный
Автор темы
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
 
  • Bug
Реакции: paulohardy

Vespan

Чешский луашер
Проверенный
2,121
1,719
прикоольно,неделю назад подобное делал,вот если кому то нужно
Lua:
function main()
    while not isSampAvailable() do wait(0) end

    hotkeys = {
        test = {key = 'J',tickn = '',tick = os.clock(),edit = false},
    }

    while true do wait(0)

        imgui.Process = window.v

        if hotkey().key('test') then
            window.v = not window.v
        end

    end
end

--in imgui
hotkey().imgui('test',150)

--
function hotkey()  -- ##HOTKEY
local function stringKeySplit(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   
local function strToIdKeys(str)
    tKeys = stringKeySplit(str, "+")
    if #tKeys ~= 0 then
        for i = 1, #tKeys do
            if i == 1 then
                str = vkeys.name_to_id(tKeys[i], false)
            else
                str = str .. " " .. vkeys.name_to_id(tKeys[i], false)
            end
        end
        return tostring(str)
    else
        return "(("
    end
end
local 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_RMENU or v == VK_RCONTROL or v == VK_RSHIFT) 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_RMENU and v ~= VK_RCONTROL and v ~= VK_RSHIFT) then
            if tostring(curkeys):len() == 0 then
                curkeys = v
            else
                curkeys = curkeys .. " " .. v
            end

            bool = true
        end
    end
    return curkeys, bool
end
local function getDownKeysText()
    tKeys = stringKeySplit(getDownKeys(), " ")
    if #tKeys ~= 0 then
        for i = 1, #tKeys do
            if i == 1 then
                str = vkeys.id_to_name(tonumber(tKeys[i]))
            else
                str = str .. "+" .. vkeys.id_to_name(tonumber(tKeys[i]))
            end
        end
        return str
    else
        return "None"
    end
end
------------------------------------------------------

local class = {}

function class.key(hk, pressed)
    pressed = pressed and pressed or false
    hk = hotkeys[hk]
    local key = strToIdKeys(hk.key)
    local tKeys = stringKeySplit(key, " ")
    if tKeys[1] == nil then
        return false
    end
    local bool = false
    local key = #tKeys < 2 and tonumber(tKeys[1]) or tonumber(tKeys[2])
    local modified = tonumber(tKeys[1])

    if not hk.edit then
        if #tKeys < 2 then
            if wasKeyPressed(key) and not pressed then
                bool = true
            elseif isKeyDown(key) and pressed then
                bool = true
            end

        else
            if isKeyDown(modified) and not wasKeyReleased(modified) then
                if wasKeyPressed(key) and not pressed then
                    bool = true
                elseif isKeyDown(key) and pressed then
                    bool = true
                end
            end
        end
        if nextLockKey == key then
            if pressed and not wasKeyReleased(key) then
                bool = false
            else
                bool = false
                nextLockKey = ""
            end
        end
        return bool
    end
    return false

end

function class.imgui(hk,with)


    h = hotkeys[hk]
    with = with or 75

    imgui.Button(h.edit and h.tickn or (tostring(h.key) ),imgui.ImVec2(with,0))

    if imgui.IsItemClicked() then
        h.edit = true
        h.tick = os.clock()
    end

    local downKey = getDownKeysText()
    lockPlayerControl(h.edit)
    if h.edit then
        if downKey ~= 'None' then
            h.tickn = downKey and downKey or 'NIL'

        else
            d = false
            if os.clock()-h.tick < 0.75 then
                h.tickn = 'NO'
            elseif os.clock()-h.tick > 0.76 and os.clock()-h.tick < 1.50 then
                h.tickn = ''
            elseif os.clock()-h.tick > 1.50 then
                h.tick = os.clock()
            end
        end
        if window.v == false then; h.edit = false; end
        imgui.SameLine() if imgui.SmallButton('clear') then h.edit = false h.key = ('None') end
        imgui.SameLine() if imgui.SmallButton('save') then h.edit = false h.key = (downKey) end
    end

end

    return class
end
ток у меня можно забиндить хоть на любую кнопку,не важно сколько("B+R","ALT+SHIFT+CONTROL+A",'1+2+3+4+5")
На IMGUI*
 
  • Нравится
Реакции: ewin

CaJlaT

07.11.2024 14:55
Модератор
2,831
2,671
При попытке создать любой банд с альтом
1673572141672.png

кринжанул...
 
  • Нравится
Реакции: Vespan

CaJlaT

07.11.2024 14:55
Модератор
2,831
2,671
Бля пацаны, просто возьмите хоткеи хомки за основу и перепишите нормально на мимгуи
 

AnWu

Известный
Всефорумный модератор
4,774
5,386
Ну пиздец, спиздили код аддонса и ркейс и такие "я сделяль"
 
  • Ха-ха
Реакции: ARMOR