ViRuZGamiing Posted January 28, 2016 Share Posted January 28, 2016 Is there a way to get the input of keys? I could make a table with all keys and bind them to a input function. But say for example if I press 'b' then b outputs in chat. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 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
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 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
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 What's calling getChatInput? Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 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
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 (edited) onClientKey gets called twice per key press. The variable 'pressed' is set to true for key down and false for key up. So... function getChatInput (button, pressed) if pressed == true then input = input..button end end Edited January 28, 2016 by Guest Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 Oh so what I did was on press and release output it BTW you don't need to check for booleans to be true Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 BTW you don't need to check for booleans to be true I do it for neatness. I also don't need to add semi-colons to end statements but I do it anyways. Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 (edited) This helped me all the way to the end thx I've almost made an entire basic chat system in +/- 1 hour. Not that this is a problem but I keep getting 'Command of cvar unknown: chatbox' in my console. I used showChat(false) to hide to default one. Edited January 28, 2016 by Guest Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 Alright man. I do not know what that means exactly. ("Command of cvar onbekend: chatbox") Maybe an MTA team member or someone more experienced with MTA scripting can elaborate. I've made me a custom chat box as well. It's designed just like World of Warcraft's chat box. I do not get anything like you're getting. Link to comment
tosfera Posted January 28, 2016 Share Posted January 28, 2016 You're actually still executing binds in the background, Viruz. You should make a check inside the onClientKey and see if the user is using your input. If it is, simply use cancelEvent and "return" to stop it from happening. The normal chatbox is bound to some keys and if you're using those without taking note of that, it'll start throwing random errors. Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 So can't I just unbind the chat? there is a MTA Hardcoded command 'chatbox' as far as I know. But unbindKey("t", "both", "chatbox") didn't do the trick sadly. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 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
tosfera Posted January 28, 2016 Share Posted January 28, 2016 Try to skim down which buttons activates that chatbox bind. Press every button once and see which one triggers the error. Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 Try to skim down which buttons activates that chatbox bind. Press every button once and see which one triggers the error. As I said 't' because t opens the chat Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 As I said 't' because t opens the chat Try: toggleControl("chatbox", false) Link to comment
ALw7sH Posted January 28, 2016 Share Posted January 28, 2016 Use onClientCharacter instead of onClientKey if you want to do custom edit Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 (edited) 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 January 28, 2016 by Guest Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 Damn man, it works for me no errors, let me scrap my system and send it to you. Link to comment
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 It might be because I did it server sided, let me try it client. EDIT: Nope still does Link to comment
tosfera Posted January 28, 2016 Share Posted January 28, 2016 What if you add a cancelEvent() after "if pressed then", might work. I've been creating custom inputs too but never had this problem.. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 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
ViRuZGamiing Posted January 28, 2016 Author Share Posted January 28, 2016 What if you add a cancelEvent() after "if pressed then", might work. I've been creating custom inputs too but never had this problem.. That makes me stuck in my typing situation. My dxDraw is drawn but my input is (as expected) cancelled. Link to comment
ViRuZGamiing Posted January 29, 2016 Author Share Posted January 29, 2016 if keys[button] ~= nil then[/lua] This didn't work Link to comment
tosfera Posted January 29, 2016 Share Posted January 29, 2016 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now