Aliasing bind

Hey, I was wondering how can I Alias the command “bind” garry thought it would be funny to block it, so I can’t use it.


 
concommand.Add("blah", function() 
     RunConsoleCommand("alias", "bind", ""luabind"") 
     RunConsoleCommand("luabind", "mouse4", "concommand") 
end)


Would that work? So that I can bind keys through lua? Whenever I Just RunConsoleCommand(“bind”, “-”, “command”) it says bind is blocked.

alias is blocked too.

How can I go about using bind?

Use KEY_ inputs.

so something like


 
if(input.IsKeyDown(KEY_SPACE)) then 
     print "Hello" 
end 


Yes.

You can also use the KeyPress hook, check the wiki for info.

It seems (to me), that it may be easier to do this

Stolen Wiki Code Alert


 local players = player.GetAll()
for _, player in ipairs( players ) do
   if( player:KeyPressed( IN_FORWARD ) ) then
      Msg( "You pressed the forward key!
" )
   end
end

rather then


function KeyPressed (P, key)
	Msg (P:GetName().." pressed "..key.."
")
end
 
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )

That first snippet checks if the key is down (meaning it’ll keep looping), KeyPress hook gets called once when the key is pressed.

So then the second one is more efficient, from what it sounds like.

Where it says function KeyPressed(P, key) do I replace that with function whatever(P, KEY_SPACE)

Nope KEY_* are just ENUMS, and should not be used in function arguments. Do:


if key == KEY_SPACE then end

I see. Thanks.

No problem. Another small thing is that hook will only work for KEY_* enums, IN_* and MOUSE_* enums will not work!

I see, thanks. Also I would like to make a test script for you to check out and make sure it is being written properly and works.


function KeyPressed (P, key)
	if key == KEY_SPACE then 
             LocalPlayer():ConCommand("+whatever")
        end
end
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )

I don’t think it will work, that’s why I am trying to make sure.

KEY_ CANNOT be used in KeyPress. Only IN_ enums can. This hook is for binds, not for real-life buttons. So, IN_JUMP should be used instead of KEY_SPACE.

And, proper calling is KeyPressed(pl,IN_JUMP), because this hook ONLY launched when SPECIFIED button is pressed. “key” is not a button.

Right code:
[lua]
function KeyPressed (P, IN_JUMP)
P:ConCommand("+whatever")
end
hook.Add( “KeyPress”, “KeyPressedHook”, KeyPressed )
[/lua]

That is not correct.

[lua]
local function KeyPress( pl, key )
if key == IN_JUMP then
pl:ConCommand( “+whatever;” )
end
end
local function KeyRelease( pl, key )
if key == IN_JUMP then
pl:ConCommand( “-whatever;” )
end
end

hook.Add( “KeyPress”, “+whatever.KeyPress”, KeyPress )
hook.Add( “KeyRelease”, “+whatever.KeyRelease”, KeyRelease )
[/lua]

Might have some errors, but that’s more along the lines of what it is supposed to be.

Alright, both of your inputs helped alot, but, what if you are not specifically trying to do it with the space bar, then what? Like what if I want to bind m for example or q? What do you do then?

Oh, thanks!

And why would that work at all? It’s not like this in any programming/scripting languages, this is syntax 101.

What do you mean notRzilla