Jump to content

Better control keybindings


Recommended Posts

I see a lot of mods generate a list of keys when they want to add a keybinding like so:

local FIRST_NUMBER = 48
local FIRST_LETTER = 65
for i = 1, 10 do
    local ch = string.char(FIRST_NUMBER + i - 1)
    keyslist[i] = {description = ch, data = ch}
end
for i = 11, 36 do
    local ch = string.char(FIRST_LETTER + i - 11)
    keyslist[i] = {description = ch, data = ch}
end

Is there a better way to assign a keybinding for your mod?

Link to comment
Share on other sites

Anyway,  here is a little improvement on the code, in case someone finds it useful

 

function generateKeys(result, from, to)
    local s = string.byte(from, 1)
    local e = string.byte(to, 1)
    do
        local i = s
        while i <= e do
            local key = string.char(i)
            result[#result + 1] = {description = key, data = key}
            i = i + 1
        end
    end
end
string = ""
local keys = {}
generateKeys(keys, "0", "9")
generateKeys(keys, "a", "z")

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...