Lovinglife Posted July 12, 2010 Share Posted July 12, 2010 ...Sup, is it possible to bind a combo of keys? I need smth like this: key1+key2+key3, server: player pressed combo#1... I dont need the code, i just need to know if this is possible , and if isn't a way to create it...ofc if u got free time i'll appreciate a lil' help Regards Lovinglife Link to comment
dzek (varez) Posted July 12, 2010 Share Posted July 12, 2010 afaik - not possible.. in normal way.. when i needed that i just made handler of onClientRender event and in that handler i was checking pressed keys, and when both was pressed then .. something Link to comment
Antibird Posted July 12, 2010 Share Posted July 12, 2010 Since server can't check for the client button state, it's probably better to check for combo client-side and then trigger some server event, or anything. Making a list of combos (key1 + key2 / key2 + key3 + key5 / etc) and bind all keys in the list to a function that checks for other combo keys pressed or not (which are related to the pressed one), this may be something you could use. Link to comment
dzek (varez) Posted July 12, 2010 Share Posted July 12, 2010 did he or me say anything about server side? Making a list of combos (key1 + key2 / key2 + key3 + key5 / etc) and bind all keys in the list to a function that checks for other combo keys pressed or not (which are related to the pressed one), this may be something you could use. this also sounds good.. Link to comment
Antibird Posted July 12, 2010 Share Posted July 12, 2010 I need smth like this: key1+key2+key3, server: player pressed combo#1... Probably, probably not. Might be just any kind of output of course. Link to comment
JasperNL=D Posted July 12, 2010 Share Posted July 12, 2010 I don't know if the question is already answered but i think this will work. https://wiki.multitheftauto.com/wiki/GetKeyState it gets the state of a key. So, if you want teh key combination a + b + c, you need to bind c, and getKeyState a and b bindKey (getLocalPlayer(), "c", "down", function ( player, key, keyState ) if (getKeyState (player,"b") == true) and (getKeyState (player,"a") == true) then outputChatBox ("Look at me! i've pressed A, B and C in one time!",255,0,0,false) end end) bindKey I hope it works. I haven't tested it, but I think it will work Btw, my example is client-side! Edit: When you do press A and C and after it B, it don't works! C must be pressed at last! Link to comment
dzek (varez) Posted July 12, 2010 Share Posted July 12, 2010 what a weird methods.. maybe this? pressingA=false pressingB=false pressingC=false function checkCombo() if (pressingA and pressingB and pressingC) then -- our action outputChatBox("magic!") end end bindKey("a", "down", function() pressingA=true checkCombo() end) bindKey("a", "up", function() pressingA=false end) bindKey("b", "down", function() pressingB=true checkCombo() end) bindKey("b", "up", function() pressingB=false end) bindKey("c", "down", function() pressingC=true checkCombo() end) bindKey("c", "up", function() pressingC=false end) this is the best way i think, give it a try. Link to comment
Lovinglife Posted July 12, 2010 Author Share Posted July 12, 2010 Thanks everyone varez script is working, ty 1 more question, thx to varez'script i got my combo#1, what if i wanna add infinite combos? i cannot copy the code again in same sheet of work bcuz i'd get problem with the 2 or more "pressing A" and the "magic" that appears in chat is seen by every person on my server right? Regards Lovinglife Link to comment
50p Posted July 12, 2010 Share Posted July 12, 2010 (edited) If you want infinite number of combos then I'd suggest you make a table of all the keys and change its value when key is pressed. Basically, make your own isKeyDown function. Something like this: local keysList = { "a", "b", "c", "d", "e", "f", } -- etc., list of the keys you want to check local playerKeyState = { }; -- every player's key state local function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; end -- bind the keys for every player online for i, plr in ipairs( getElementsByType( "player" ) ) do for j, key in ipairs( keysList ) do bindKey( plr, key, "both", changeKeyState ); end end function isKeyDown( player, key ) return ( playerKeyState[ player ] ) and playerKeyState[ player ][ key ] or false; end After you've set everything up, you can now use isKeyDown anywhere in your code to check if the specific key is down. -- for instance: if isKeyDown( player, "a" ) and isKeyDown( player, "lshift" ) then -- do something when left shift and a are down end Or you can even make an event which will be triggered when a key is down... -- This is the same function that I showed you above but added triggerEvent local function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; triggerEvent( "onPlayerKeyStateChange", player, key, keyState ); end -- Then you can attach handlers to the event and detect key press addEvent( "onPlayerKeyStateChange" ); addEventHandler( "onPlayerKeyStateChange", rootElement, function( key, keyState ) -- 1st way: if isKeyDown( source, "lshift" ) and ( key == "a" ) and ( keyState == "down" ) then -- do something here... end -- 2nd way: if isKeyDown( source, "lshift" ) and isKeyDown( source, "a" ) then -- do something here... end end ) Well, well.. I just gave you entire code for a new function and event that will make your life easier... Edited July 13, 2010 by Guest Link to comment
Lovinglife Posted July 13, 2010 Author Share Posted July 13, 2010 If you want infinite number of combos then I'd suggest you make a table of all the keys and change its value when key is pressed. Basically, make your own isKeyDown function. Something like this: local keysList = { "a", "b", "c", "d", "e", "f", } -- etc., list of the keys you want to check local playerKeyState = { }; -- every player's key state local function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; end -- bind the keys for every player online for i, plr in ipairs( getElementByType( "player" ) do for j, key in ipairs( keysList ) do bindKey( plr, key, "both", changeKeyState ); end end function isKeyDown( player, key ) return ( playerKeyState[ player ] ) and playerKeyState[ player ][ key ] or false; end After you've set everything up, you can now use isKeyDown anywhere in your code to check if the specific key is down. -- for instance: if isKeyDown( player, "a" ) and isKeyDown( player, "lshift" ) then -- do something when left shift and a are down end Or you can even make an event which will be triggered when a key is down... -- This is the same function that I showed you above but added triggerEvent local function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; triggerEvent( "onPlayerKeyStateChange", player, key, keyState ); end -- Then you can attach handlers to the event and detect key press addEvent( "onPlayerKeyStateChange" ); addEventHandler( "onPlayerKeyStateChange", rootElement, function( key, keyState ) -- 1st way: if isKeyDown( source, "lshift" ) and ( key == "a" ) and ( keyState == "down" ) then -- do something here... end -- 2nd way: if isKeyDown( source, "lshift" ) and isKeyDown( source, "a" ) then -- do something here... end end ) Well, well.. I just gave you entire code for a new function and event that will make your life easier... thanks for you suggestions and help, i tried to understand ur code....i copied 1st n 2nd piece and put em togethere i get 1 error ...here... -- bind the keys for every player online for i, plr in ipairs( getElementByType( "player" ) do <-----(misses 1 ) near do, i added) attempt to call global value "getElementByType" (a nil value) for j, key in ipairs( keysList ) do bindKey( plr, key, "both", changeKeyState ); end end ç_ç Link to comment
50p Posted July 13, 2010 Share Posted July 13, 2010 It should be getElementsByType. It's hard to type long code in browser without syntax highlighter. Updated the code in the previous post. Link to comment
Lovinglife Posted July 13, 2010 Author Share Posted July 13, 2010 uhm im confused...i tried ur code but nothing happens...i tried a lot of ways (server side, client side its not explained and i dont know), both 1+2 code piece and 3rd but i cannot find a way... Link to comment
50p Posted July 14, 2010 Share Posted July 14, 2010 Ok. I didn't test the previous code and I found a few "bugs". Here is the code which I tested and works perfectly (server-side): local keysList = { "a", "b", "c", "d", "e", "f", "lshift" } -- etc., list of the keys you want to check local playerKeyState = { }; -- every player's key state -- bind the keys for every player online addEventHandler( "onResourceStart", getResourceRootElement(), function() for i, plr in ipairs( getElementsByType( "player" ) ) do bindPlayerKeysList( plr ); end end ) addEventHandler( "onPlayerJoin", getRootElement( ), function( ) bindPlayerKeysList( source ); end ) function bindPlayerKeysList( player ) for j, key in ipairs( keysList ) do bindKey( player, key, "both", changeKeyState ); end end function isKeyDown( player, key ) return ( playerKeyState[ player ] ) and playerKeyState[ player ][ key ] or false; end -- This is the same function that I showed you above but added triggerEvent function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; triggerEvent( "onPlayerKeyStateChange", player, key, keyState ); end -- Then you can attach handlers to the event and detect key press addEvent( "onPlayerKeyStateChange" ); addEventHandler( "onPlayerKeyStateChange", getRootElement(), function( key, keyState ) -- 1st way: must hold LSHIFT and then press A if isKeyDown( source, "lshift" ) and ( key == "a" ) and ( keyState == "down" ) then outputChatBox( getPlayerName( source ) .. " just hit LSHIFT + A (order: LSHIFT, A)" ); end -- 2nd way: doesn't have to hold LSHIFT before pressing A if isKeyDown( source, "lshift" ) and isKeyDown( source, "a" ) then outputChatBox( getPlayerName( source ) .. " just hit LSHIFT + A (order: doesn't matter)" ); end end ) Link to comment
Lovinglife Posted July 14, 2010 Author Share Posted July 14, 2010 Ok. I didn't test the previous code and I found a few "bugs".Here is the code which I tested and works perfectly (server-side): local keysList = { "a", "b", "c", "d", "e", "f", "lshift" } -- etc., list of the keys you want to check local playerKeyState = { }; -- every player's key state -- bind the keys for every player online addEventHandler( "onResourceStart", getResourceRootElement(), function() for i, plr in ipairs( getElementsByType( "player" ) ) do bindPlayerKeysList( plr ); end end ) addEventHandler( "onPlayerJoin", getRootElement( ), function( ) bindPlayerKeysList( source ); end ) function bindPlayerKeysList( player ) for j, key in ipairs( keysList ) do bindKey( player, key, "both", changeKeyState ); end end function isKeyDown( player, key ) return ( playerKeyState[ player ] ) and playerKeyState[ player ][ key ] or false; end -- This is the same function that I showed you above but added triggerEvent function changeKeyState( player, key, keyState ) if not playerKeyState[ player ] then playerKeyState[ player ] = { }; -- remember to nil this table when player leaves the server!!! end playerKeyState[ player ][ key ] = ( keyState == "down" ) and true or false; triggerEvent( "onPlayerKeyStateChange", player, key, keyState ); end -- Then you can attach handlers to the event and detect key press addEvent( "onPlayerKeyStateChange" ); addEventHandler( "onPlayerKeyStateChange", getRootElement(), function( key, keyState ) -- 1st way: must hold LSHIFT and then press A if isKeyDown( source, "lshift" ) and ( key == "a" ) and ( keyState == "down" ) then outputChatBox( getPlayerName( source ) .. " just hit LSHIFT + A (order: LSHIFT, A)" ); end -- 2nd way: doesn't have to hold LSHIFT before pressing A if isKeyDown( source, "lshift" ) and isKeyDown( source, "a" ) then outputChatBox( getPlayerName( source ) .. " just hit LSHIFT + A (order: doesn't matter)" ); end end ) excellent, thank you very much for your work ^^ just bcuz im curious, why have you been thinking about the "keys order (lshift,a)"? Link to comment
50p Posted July 14, 2010 Share Posted July 14, 2010 ...excellent, thank you very much for your work ^^ just bcuz im curious, why have you been thinking about the "keys order (lshift,a)"? Because that's how you use them in most OS'es. You don't press A and then Shift to "Select all" or you don't press S and then Shift to "Save", you hold Shift and then press S. 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