imgui = require'imgui'
KEY = '[]'
function main()
while not isSampAvailable() do wait(10) end
hotkey().register('K',KEY,false,false,function() sampAddChatMessage('123!',-1) end)
imgui.Process = true
wait(-1)
end
function imgui.OnDrawFrame()
local sw,sh = getScreenResolution()
imgui.SetNextWindowPos(imgui.ImVec2(sw/2,sh/2), imgui.Cond.FirstUseEver)
imgui.Begin('test hotkeys',nil,64)
if hotkey().imgui('name text','name button','K') then
-- KEY = hotkey().getKeys('K')
-- ini.test.key = hotkey().getKeys('K')
--save ini
end
imgui.End()
end
function hotkey()
local vkeys = require'vkeys'
if HOTKEY == nil then
HOTKEY = {
wait_for_key = 'press any key..',
no_key = 'none',
list = {},
eventHandlers = false,
}
end
local function getKeysNameByBind(keys)
local t = {}
for k,v in ipairs(keys) do; table.insert(t,vkeys.id_to_name(v)); end
return (#t == 0 and HOTKEY.no_key or (#t == 1 and table.concat(t,'') or table.concat(t,' + ')))
end
local c = {}
function c.register(hk,keys,keyDown,activeOnCursorActive,callback)
if HOTKEY.list[hk] == nil then
keys = decodeJson(keys or '{}')
HOTKEY.list[hk] = {
edit = false,
tick = os.clock(),
keys = keys,
keyDown = keyDown,
activeOnCursorActive = activeOnCursorActive,
callback = callback,
}; return true
end; return false
end
function c.unregister(hk)
if HOTKEY.list[hk] == nil then; return false; end
HOTKEY.list[hk] = nil
return true
end
function c.imgui(name,textInButton,hk,width)
textInButton = (textInButton == nil and '' or (#textInButton == 0 and '' or (textInButton .. ' ')) )
local b = false
local h = HOTKEY.list[hk]
imgui.Text(name)
imgui.SameLine()
if h == nil then; imgui.Button(textInButton.."NOT FIND HOTKEY "..hk); return false; end
if not h.edit then; h.tick = os.clock(); end
if os.clock()-h.tick >= 1 then; h.tick = os.clock(); end
imgui.PushStyleColor(imgui.Col.Text,(os.clock()-h.tick) <= 0.5 and imgui.GetStyle().Colors[imgui.Col.Text] or imgui.ImVec4(1,1,1,0))
if imgui.Button(textInButton.. (h.edit and (#h.keys == 0 and HOTKEY.wait_for_key or getKeysNameByBind(h.keys)) or getKeysNameByBind(h.keys) .. '##'..hk),imgui.ImVec2(width or 0,0)) then
h.edit = true; h.keys = {}
end
imgui.PopStyleColor(1)
if h.edit then
for k,v in pairs(vkeys) do
if isKeyDown(v) and (v ~= VK_MENU and v ~= VK_CONTROL and v ~= VK_SHIFT) then
for kk,vv in ipairs(h.keys) do
if v == vv then; goto s; end
end
table.insert(h.keys,v)
h.tick = os.clock()
::s::
if #h.keys > 2 then
for i = 3,#h.keys do; table.remove(h.keys,i); end
end
else
for kk,vv in ipairs(h.keys) do
if v == vv then; h.edit = false; b = true; end
end
end
end--
if isKeyJustPressed(VK_BACK) then; h.keys = {}; h.edit = false; end
end
return b
end
function c.getKeys(hk)
return HOTKEY.list[hk].keys == nil and 'nil_'..hk or encodeJson(HOTKEY.list[hk].keys or '{}')
end
if not HOTKEY.eventHandlers then
addEventHandler("onWindowMessage",
function (message, wparam, lparam)
for k,v in pairs(HOTKEY.list) do
if v.edit then
if message == 0x0102 then--CHAR
consumeWindowMessage(true,true)
elseif message == 0x0008 then--KILLFOCUS
v.edit = false
v.keys = {}
end
end
end
end
)
lua_thread.create(function()
while true do wait(0)
-- addEventHandler('onD3DPresent',function()
if HOTKEY~=nil then
for k,v in pairs(HOTKEY.list) do
if HOTKEY.list[k] ~= nil and v.activeOnCursorActive and true or not (sampIsCursorActive() or sampIsDialogActive() or sampIsChatInputActive()) and not v.edit then
if v.keyDown and (#v.keys == 1 and isKeyDown(v.keys[1]) or #v.keys == 2 and (isKeyDown(v.keys[1]) and isKeyDown(v.keys[2])) or false) or (#v.keys == 1 and isKeyJustPressed(v.keys[1]) or #v.keys == 2 and (isKeyDown(v.keys[1]) and isKeyJustPressed(v.keys[2])) or false) then
v.callback()
end
end
end
end
end
end)
HOTKEY.eventHandlers = true
end
return c
end