guix Posted January 31, 2013 Share Posted January 31, 2013 (edited) Hello I have a problem in my script that I can't seem to solve. Here is a part of my code: local bCommandKeyIsDown = {} function handleKeyboard( key, state ) local cmds = getCommandsBoundToKey ( key, "down" ) if cmds then for cmd, _ in pairs ( cmds ) do bCommandKeyIsDown[ cmd ] = state end end end addEventHandler( "onClientKey", root, handleKeyboard ) Then I check for bCommandKeyIsDown in a onClientRender, like this: function handleSomething() if bCommandKeyIsDown[ "my_command" ] then DoSomething() end end addEventHandler( "onClientRender", root, handleSomething ) The problem is when I bind multiple keys to "my_command": let's say W and X are the 2 keys bound to that command. - If I press and release W OR X, it work as expected - But if I press and hold W down, and then I press X and release X, then it's like W have been released too, but I'm still holding it down. I understand why it does that, but I don't know how to solve. Help? Edited January 31, 2013 by Guest Link to comment
guix Posted January 31, 2013 Author Share Posted January 31, 2013 Solved: local bCommandKeyIsDown = {} local iCommandKeysDownCount = {} function handleKeyboard( key, state ) local cmds = getCommandsBoundToKey ( key, "down" ) if cmds then for cmd, _ in pairs ( cmds ) do iCommandKeysDownCount[cmd] = (iCommandKeysDownCount[cmd] or 0) + (state and 1 or -1) bCommandKeyIsDown[cmd] = (iCommandKeysDownCount[cmd] ~= 0) end end end addEventHandler( "onClientKey", root, handleKeyboard ) 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