User Input System * GUI drawText

Share your gamemodes and snippets here
Post Reply
icuruok
Posts: 4
Joined: August 16th, 2019, 10:47 am

User Input System * GUI drawText

Post by icuruok » August 17th, 2019, 1:25 pm

This is a UI based user input system I created for a upcoming gamemode I plan to release.

With this UI user input system you can have your IV:MP server request user text input to collect and store.

The text request is made on screen from drawTexthttps://bitbucket.org/Suge/eflc-mp-lua/wiki/drawText/

The user can enter any A-Z : 1-9 keys they wish and server can collect it, The text is also displayed live to the user as they type.



How To :

To call and use the user input is simple, Once userInput filterscript is loaded simply call with

gkeyboard(playerid, true) -- enable keyboard hook and request user input

You can disable by calling gkeyboard(playerid, false) -- by default is removed once user hits enter

to call/store/get the data the user entered
userInputdata[playerid] -- this is the string the user entered on input request


userInput.lua

Code: Select all

userInputdata = { } -- global table for each users input data 
userInputstream = { } -- global table of live input text from hook


function gkeyboard(pid, ctype)
   if(ctype == true) then
      wipeDrawClass(pid, 6) -- remove chat display
      drawText(pid, 6, 0.4, 0.5, 0.2, 0.54, "Enter Text - Enter to send", 0, 0xFFFFFFFF)
      userInputstream[pid] = ""
   end
   setPlayerFrozen(pid, ctype) -- freezes or unfreezes player for input
   setPlayerKeyHook(pid, 0x30, ctype) -- 0 key
   setPlayerKeyHook(pid, 0x31, ctype) -- 1 key	
   setPlayerKeyHook(pid, 0x32, ctype) -- 2 key
   setPlayerKeyHook(pid, 0x33, ctype) -- 3 key	
   setPlayerKeyHook(pid, 0x34, ctype) -- 4 key
   setPlayerKeyHook(pid, 0x35, ctype) -- 5 key
   setPlayerKeyHook(pid, 0x36, ctype) -- 6 key
   setPlayerKeyHook(pid, 0x37, ctype) -- 7 key	
   setPlayerKeyHook(pid, 0x38, ctype) -- 8 key
   setPlayerKeyHook(pid, 0x39, ctype) -- 7 key	

   setPlayerKeyHook(pid, 0x0D, ctype) -- enter key
   setPlayerKeyHook(pid, 0x20, ctype) -- space key	
   setPlayerKeyHook(pid, 0x08, ctype) -- backspace key	

   setPlayerKeyHook(pid, 0x41, ctype) -- A key	
   setPlayerKeyHook(pid, 0x42, ctype) -- B key	
   setPlayerKeyHook(pid, 0x43, ctype) -- C key	
   setPlayerKeyHook(pid, 0x44, ctype) -- D key	
   setPlayerKeyHook(pid, 0x45, ctype) -- E key	
   setPlayerKeyHook(pid, 0x46, ctype) -- F key	
   setPlayerKeyHook(pid, 0x47, ctype) -- G key	
   setPlayerKeyHook(pid, 0x48, ctype) -- H key	
   setPlayerKeyHook(pid, 0x49, ctype) -- I key	
   setPlayerKeyHook(pid, 0x4A, ctype) -- J key	
   setPlayerKeyHook(pid, 0x4B, ctype) -- K key	
   setPlayerKeyHook(pid, 0x4C, ctype) -- L key	
   setPlayerKeyHook(pid, 0x4D, ctype) -- M key	
   setPlayerKeyHook(pid, 0x4E, ctype) -- N key	
   setPlayerKeyHook(pid, 0x4F, ctype) -- O key	
   setPlayerKeyHook(pid, 0x50, ctype) -- P key	
   setPlayerKeyHook(pid, 0x51, ctype) -- Q key	
   setPlayerKeyHook(pid, 0x52, ctype) -- R key	
   setPlayerKeyHook(pid, 0x53, ctype) -- S key	
   setPlayerKeyHook(pid, 0x54, ctype) -- T key	
   setPlayerKeyHook(pid, 0x55, ctype) -- U key	
   setPlayerKeyHook(pid, 0x56, ctype) -- V key	
   setPlayerKeyHook(pid, 0x57, ctype) -- W key	
   setPlayerKeyHook(pid, 0x58, ctype) -- X key	
   setPlayerKeyHook(pid, 0x59, ctype) -- Y key	
   setPlayerKeyHook(pid, 0x5A, ctype) -- Z key	
end



function ts_keyEvent(playerid, keyCode, isUp)

    if(keyCode == 0x30 and isUp == true) then --0 key
            userInputstream[playerid] = userInputstream[playerid] .. "0"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x31 and isUp == true) then --1 key
            userInputstream[playerid] = userInputstream[playerid] .. "1"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x32 and isUp == true) then --2 key
            userInputstream[playerid] = userInputstream[playerid] .. "2"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x33 and isUp == true) then --3 key
            userInputstream[playerid] = userInputstream[playerid] .. "3"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x34 and isUp == true) then --4 key
            userInputstream[playerid] = userInputstream[playerid] .. "4"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x35 and isUp == true) then --5 key
            userInputstream[playerid] = userInputstream[playerid] .. "5"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x36 and isUp == true) then --6 key
            userInputstream[playerid] = userInputstream[playerid] .. "6"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x37 and isUp == true) then --7 key
            userInputstream[playerid] = userInputstream[playerid] .. "7"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x38 and isUp == true) then --8 key
            userInputstream[playerid] = userInputstream[playerid] .. "8"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x39 and isUp == true) then --9 key
            userInputstream[playerid] = userInputstream[playerid] .. "9"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end



    if(keyCode == 0x20 and isUp == true) then --spacebar key
            userInputstream[playerid] = userInputstream[playerid] .. " "
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end

    if(keyCode == 0x08 and isUp == true) then --backspace key
            userInputstream[playerid] = userInputstream[playerid]:sub(1, -2)
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end

    if(keyCode == 0x0D and isUp == true) then --enter key
            wipeDrawClass(playerid, 6) -- remove chat display
            gkeyboard(playerid, false) -- disable keyboard hook and dont capture keys
            userInputdata[playerid] = userInputstream[playerid] -- set data got from keyboard hook to users data table
            sendPlayerMsg(playerid, "you entered :" .. userInputdata[playerid], 0xFF0000FF)
    end


    if(keyCode == 0x41 and isUp == true) then --A key
            userInputstream[playerid] = userInputstream[playerid] .. "a"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x42 and isUp == true) then --B key
            userInputstream[playerid] = userInputstream[playerid] .. "b"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x43 and isUp == true) then --C key
            userInputstream[playerid] = userInputstream[playerid] .. "c"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x44 and isUp == true) then --D key
            userInputstream[playerid] = userInputstream[playerid] .. "d"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x45 and isUp == true) then --E key
            userInputstream[playerid] = userInputstream[playerid] .. "e"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x46 and isUp == true) then --F key
            userInputstream[playerid] = userInputstream[playerid] .. "f"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x47 and isUp == true) then --G key
            userInputstream[playerid] = userInputstream[playerid] .. "g"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x48 and isUp == true) then --H key
            userInputstream[playerid] = userInputstream[playerid] .. "h"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x49 and isUp == true) then --I key
            userInputstream[playerid] = userInputstream[playerid] .. "i"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4A and isUp == true) then --J key
            userInputstream[playerid] = userInputstream[playerid] .. "j"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4B and isUp == true) then --K key
            userInputstream[playerid] = userInputstream[playerid] .. "k"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4C and isUp == true) then --L key
            userInputstream[playerid] = userInputstream[playerid] .. "l"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4D and isUp == true) then --M key
            userInputstream[playerid] = userInputstream[playerid] .. "m"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4E and isUp == true) then --N key
            userInputstream[playerid] = userInputstream[playerid] .. "n"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x4F and isUp == true) then --O key
            userInputstream[playerid] = userInputstream[playerid] .. "o"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x50 and isUp == true) then --P key
            userInputstream[playerid] = userInputstream[playerid] .. "p"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x51 and isUp == true) then --Q key
            userInputstream[playerid] = userInputstream[playerid] .. "q"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x52 and isUp == true) then --R key
            userInputstream[playerid] = userInputstream[playerid] .. "r"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x53 and isUp == true) then --S key
            userInputstream[playerid] = userInputstream[playerid] .. "s"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x54 and isUp == true) then --T key
            userInputstream[playerid] = userInputstream[playerid] .. "t"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x55 and isUp == true) then --U key
            userInputstream[playerid] = userInputstream[playerid] .. "u"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x56 and isUp == true) then --V key
            userInputstream[playerid] = userInputstream[playerid] .. "v"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x57 and isUp == true) then --W key
            userInputstream[playerid] = userInputstream[playerid] .. "w"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x58 and isUp == true) then --X key
            userInputstream[playerid] = userInputstream[playerid] .. "x"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x59 and isUp == true) then --Y key
            userInputstream[playerid] = userInputstream[playerid] .. "y"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
    if(keyCode == 0x5A and isUp == true) then --Z key
            userInputstream[playerid] = userInputstream[playerid] .. "z"
            wipeDrawClass(playerid, 6) -- remove chat display
            drawText(playerid, 6, 0.4, 0.5, 0.2, 0.54, userInputstream[playerid], 0, 0xFFFFFFFF)
    end
end
registerEvent("ts_keyEvent", "onPlayerKeyPress")

Last edited by icuruok on November 23rd, 2020, 6:19 am, edited 1 time in total.

#Dav
Posts: 3
Joined: June 1st, 2019, 9:42 pm
Location: Greece

Re: User Input System * GUI drawText

Post by #Dav » September 1st, 2019, 4:14 am

Hey, you doing well man, thanks for releasing your work.
"Many humans do not care about what is "unfair,"
for it varies so much between them."

icuruok
Posts: 4
Joined: August 16th, 2019, 10:47 am

Re: User Input System * GUI drawText

Post by icuruok » November 23rd, 2020, 6:24 am

Thanks just updated script code to fix a bug issue it had

By replacing
if(keyCode == #x##)
With
if(keyCode == #x## and isUp == true)

fixes the double input bug that would happen when pressing same key more than once.
Now the script works perfectly for getting custom user input text outside of the chat itself.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests