Jump to content

Get Input


ViRuZGamiing

Recommended Posts

ROOT_ELEMENT = getRootElement(); 
  
OnKey = function(button, pressed) 
    -- source = client root 
    if pressed == true then 
        if button == "b" then 
            outputChatBox("You've pressed 'b'!"); 
        end 
    end 
  
    if pressed == false then 
        if button == "b" then 
            outputChatBox("You've released 'b'!"); 
        end 
    end 
    return; 
end 
  
addEventHandler("onClientKey",  ROOT_ELEMENT,   OnKey   ); 

https://wiki.multitheftauto.com/wiki/OnClientKey

https://wiki.multitheftauto.com/wiki/Key_names

- OR -

OnBKey = function(key, state) 
    if state == "down" then 
        outputChatBox("You've pressed 'b'!"); 
    end 
     
    if state == "up" then 
        outputChatBox("You've released 'b'!"); 
    end 
    return; 
end 
  
bindKey("b", "both", OnBKey); 

https://wiki.multitheftauto.com/wiki/BindKey

Link to comment
local input = "" 
  
function getChatInput (button) 
    input = input..button 
end 
  
function drawChatInput() 
    dxDrawText(input, 430, 651, 935, 674, tocolor(255, 255, 255, 255), 0.60, "bankgothic", "left", "center", true, false, false, false, false) 
end 
  
addEventHandler("onClientRender", root, drawChatInput) 

I tried this it almost works, the only problem is it outputs every key twice.

Link to comment

I've edited it here;

local input = "" 
local chatVisible = false 
  
function getChatInput (button) 
    input = input..button 
end 
  
function drawChat() 
    dxDrawRectangle(342, 648, 597, 29, tocolor(255, 255, 255, 255), false) 
    dxDrawRectangle(343, 649, 595, 27, tocolor(0, 0, 0, 255), false) 
    dxDrawLine(421, 652, 421, 672, tocolor(255, 255, 255, 255), 1, false) 
    dxDrawText("CHAT:", 343, 651, 421, 674, tocolor(255, 255, 255, 255), 0.60, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText(input, 430, 651, 935, 674, tocolor(255, 255, 255, 255), 0.60, "bankgothic", "left", "center", true, false, false, false, false) 
end 
  
function setChatVisible(state) 
    if (state) then 
        addEventHandler("onClientRender", root, drawChat) 
        addEventHandler("onClientKey", root, getChatInput) 
        toggleAllControls(false) 
        chatVisible = true 
        bindKey("escape") 
    else 
        removeEventHandler("onClientRender", root, drawChat) 
        removeEventHandler("onClientKey", root, getChatInput) 
        toggleAllControls(true) 
        chatVisible = false 
    end 
end 
  
function getChatVisible() 
    if (chatVisible) then 
        return true 
    else  
        return false 
    end 
end 
  
addEvent("setChatVisible", true) 
addEventHandler("setChatVisible", getRootElement(), function() 
    if (getChatVisible()) then 
        setChatVisible(false) 
    else 
        setChatVisible(true) 
    end 
end) 

the event setChatVisible is triggered by a bindKey added serverside onPlayerJoin:

function bindChat (thePlayer) 
    triggerClientEvent(thePlayer, "setChatVisible", thePlayer) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), function() 
    showChat(source, false) 
    bindKey(source, "t", "down", bindChat) 
end) 

Link to comment

From my knowledge you can't unbind any MTA controls, the player can but not the server nor the client scripts.

You have to toggleControl("chatbox", false).

The following are names of hard-coded MTA commands which do not use bindKey, but can act as bindKey by using them in an addCommandHandler. Other than that, this control list will only work with the functions toggleControl and toggleAllControls. Please note that toggleControl can't disable screenshot.

https://wiki.multitheftauto.com/wiki/Control_names

showChat(false) should have automatically disabled the control though.

Link to comment

Actually onClientKey works better in my case because I use backspace and enter and such as well, newb didn't work :/

#Alw7sh this is how I did it,

local input = "" 
local chatVisible = false 
local keys = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",  "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",  
"num_0", "num_1", "num_2", "num_3", "num_4", "num_5", "num_6", "num_7", "num_8", "num_9", "[", "]", ";", ",", "-", ".", "/", "#", "\\", "=", "space", "enter", "escape", "backspace"} 
  
function getChatInput (button, pressed) 
    if pressed then 
        for _, value in pairs(keys) do 
            if (button == value) then 
                if (button == "enter") then 
                    setChatVisible(false) 
                    outputChatMessage(input) 
                    input = "" 
                elseif (button == "escape") then 
                    setChatVisible(false) 
                    input = "" 
                elseif (button == "backspace") then 
                    input = string.sub(input, 1, #input-1) 
                elseif (button == "space") then 
                    input = input.." " 
                else 
                    input = input..button 
                end 
            end 
        end 
    end 
end 

Edited by Guest
Link to comment

I didn't see your code on the previous post until just now.

You DO NOT have to create a loop (it's inefficient), you can do this:

function getChatInput (button, pressed) 
    if pressed then 
        if keys[button] ~= nil then 
            if (button == "enter") then 
                setChatVisible(false) 
                outputChatMessage(input) 
                input = "" 
            elseif (button == "escape") then 
                setChatVisible(false) 
                input = "" 
            elseif (button == "backspace") then 
                input = string.sub(input, 1, #input-1) 
            elseif (button == "space") then 
                input = input.." " 
            else 
                input = input..button 
            end 
        end 
    end 
end 

It's 6:00am (06:00), I am going to sleep.

If you still need help when I wake up, I'll help you.

Private message me.

Link to comment
if keys[button] ~= nil then[/lua]

This didn't work

Really obvious that this didn't work, if you want to call the button as an index, you need to transform the table form a value based to an key=>value pair. You'll have to make it like;

table = { [ "a" ] = true, [ "b" ] = true }; 

Once you've done that, you can call it like; table [ button ]

Now, with that typing cancelled.. you're putting text in the box yourself, right? So what's wrong with the typing being cancelled? Are you still using other inputs somewhere else?

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...