• keypress library.
    0 replies, posted
Hi everyone. I decided to release my small library of keypress. You can do the same as gamemode.KeyPress but with every key. Explanation of library: [lua]function keypress.Add(key number,stringid string,func function,[funcunpress] function , [sendtoserver] boolean)[/lua] This function links a key to a specific function. - key is the key you want to detect. - stringid is an identificative string - func is the funcion will be executed when button is pressed. - funcunpress is an optional parameter. Is a function that executes when button is unpressed. - sendtoserver is an optional parameter. Is a boolean, if it is true, a console command will be sent to server when key is pressed (and unpressed). The command is: keypressed_+KEY (if KEY is pressed) keypressed_-KEY (if KEY is unpressed) [lua]function keypress.Remove(key number,stringid string, [hideerrors] boolean)[/lua] This function removes the link between a key and function. - key is the key you want to remove from detect. - stringid is the stringid - hideerrors is an optional parameter. If it is true, no errors will appear if you try to remove more than once the same key. [lua]function keypress.Clean()[/lua] This function cleans all links between key and functions. The code of script: [lua]// Developed by lilEzek __EzekListKey = {} //Don't touch this keypress = {} // The same with this function keypress.Add(key,stringid,func,funcunpress,sendtoserver) if type(key) != "number" then Error("Key is not a valid number or KEY") return false end if type(stringid) != "string" then Error("Stringid is not a valid string") return false end if type(func) != "function" then Error("Func is not a valid function") return false end __EzekListKey[key] = {} __EzekListKey[key][stringid] = {} __EzekListKey[key][stringid]["func"] = func __EzekListKey[key][stringid]["unpress"] = funcunpress __EzekListKey[key][stringid]["pressed"] = false __EzekListKey[key][stringid]["toserver"] = sendtoserver end function keypress.Remove(key,stringid,hideerrors) if __EzekListKey[key][stringid] == nil then if !hideerrors then Error(stringid.." doesn't match with "..tostring(key).." key") end return false end if #_EzekListKey[key] == 1 then __EzekListKey[key] = nil end end function keypress.Clean() __EzekListKey = nil __EzekListKey = {} end local function KeypressAddThink() for k,v in pairs(__EzekListKey) do if input.IsKeyDown(k) then for r,j in pairs(v) do if j["pressed"] == false then j["func"]() if j["toserver"] then RunConsoleCommand("keypressed_+"..tostring(k)) end end j["pressed"] = true end else for r,j in pairs(v) do if j["pressed"] == true and type(j["unpress"]) == "function" then j["unpress"]() if j["toserver"] then RunConsoleCommand("keypressed_-"..tostring(k)) end end j["pressed"] = false end end end end hook.Add("Think", "KeypressAddThink", KeypressAddThink) [/lua] I hope you like it.
Sorry, you need to Log In to post a reply to this thread.