Use DisplayOnscreenKeyboard properly

So, I’m trying to figure out to use the Native DisplayOnscreenKeyboard properly so that it actually returns a value, and that’s the problem, it doesn’t return a value for me. Here is my code:

function onscreenKeyboard()
	local _return = nil

	DisplayOnscreenKeyboard(1,"FMMC_KEY_TIP8", "", "", "", "", "", 99)
	while true do
		DisableAllControlActions(0)
		HideHudAndRadarThisFrame()
		Citizen.Wait(0)

		if UpdateOnscreenKeyboard() == 1 then
			_return = GetOnscreenKeyboardResult()
			break
		elseif UpdateOnscreenKeyboard() == 2 or UpdateOnscreenKeyboard() == 3 then
			break
		end
	end

	return _return
end

I tried to test it out but it seems to stop or crash on something… I could use some help on figuring out how to put this into a function of some type.

ty guys.

I am using this, it is working pretty well:

function KeyboardInput(TextEntry, ExampleText, MaxStringLenght)

	-- TextEntry		-->	The Text above the typing field in the black square
	-- ExampleText		-->	An Example Text, what it should say in the typing field
	-- MaxStringLenght	-->	Maximum String Lenght

	AddTextEntry('FMMC_KEY_TIP1', TextEntry) --Sets the Text above the typing field in the black square
	DisplayOnscreenKeyboard(1, "FMMC_KEY_TIP1", "", ExampleText, "", "", "", MaxStringLenght) --Actually calls the Keyboard Input
	blockinput = true --Blocks new input while typing if **blockinput** is used

	while UpdateOnscreenKeyboard() ~= 1 and UpdateOnscreenKeyboard() ~= 2 do --While typing is not aborted and not finished, this loop waits
		Citizen.Wait(0)
	end
		
	if UpdateOnscreenKeyboard() ~= 2 then
		local result = GetOnscreenKeyboardResult() --Gets the result of the typing
		Citizen.Wait(500) --Little Time Delay, so the Keyboard won't open again if you press enter to finish the typing
		blockinput = false --This unblocks new Input when typing is done
		return result --Returns the result
	else
		Citizen.Wait(500) --Little Time Delay, so the Keyboard won't open again if you press enter to finish the typing
		blockinput = false --This unblocks new Input when typing is done
		return nil --Returns nil if the typing got aborted
	end
end

Example would be this:
local Username = KeyboardInput("Name:", "Input Your Username", 20)

When I press enter, the variable Username will be set to Flatracer

I hope you understand the use of it.

Have Fun!

7 Likes

Thanks, this helps me a lot. :stuck_out_tongue:

No Problem, I am glad I was able to help :slight_smile:

So the function is in my client.lua file, but how would I trigger the function?

Like you would with any other function?

dont put it in a loop tho :wink:

Example? Im retarded you need to understand that.

: / so be like:

KeyboardInput("Put ur fucking shit here", "More shit here", 5)

1 Like

KK, thanks for the information about shit. Im retarded so ya’ know xD

1 Like

Youll probably want to create a variable like:

memes = KeyboardInput("Put ur fucking shit here", "More shit here", 5)

Yeah I already did :stuck_out_tongue:

How would i print that variable? I tried this but it does not want to work: (In server.lua)

AddEventHandler('chatMessage', function(source, name, msg)
	sm = stringsplit(msg, " ");
	if sm[1] == "/showid" then
	CancelEvent()
	chatPrint( 'RPname' )
	end
end)

function stringsplit(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

What even is chatprint? :stuck_out_tongue:

In my client.lua:

function chatPrint( msg )
	TriggerEvent( 'chatMessage', "ID: ", { 255, 255, 255 }, msg )
end 

Is there an easier way to do it? :stuck_out_tongue:

Love ya :heart: @Flatracer

You’re welcome

You can’t use a client function serversided, also you can trigger chatMessage serversided.

AddEventHandler('chatMessage', function(source, name, msg)
	sm = stringsplit(msg, " ");
	if sm[1] == "/showid" then
	CancelEvent()
	TriggerEvent( 'chatMessage', 'ID: ', { 255, 255, 255 }, 'RPname' )
	end
end)

function stringsplit(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
1 Like

I’ve a question, how could i “reset” the text entry?
Because every time that i open any menu, title is the same

i have a problem i am getting an error to the script that is triggering this, that the value is string and i want to be a number, any fix for that?

Just found a fix : local valueNumber= tonumber(value)

Nice thanks just how can I make it so that you can’t delete the text in the Input?