Jump to content

[Solved] Problem with multiple keys bound to same command


guix

Recommended Posts

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 by Guest
Link to comment

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

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...