Binding Keys

How do you go about binding keys in lua? Such as if I press F1, it runs a certain function…VGUI in this case, and when I press F1 again, the VGUI go’s away?

Using something like this:

[lua]hook.Add( “Think”, “vguiBind”, function( )
if ( input.IsKeyDown( KEY_F1 ) )
// Toggle vgui visibility
end
end )[/lua]

It works, with some slight problems. Firstly it creates multiple VGUI screens, and I am unsure on how to check if 1 is already out so It doesn’t make another.

My other problem, if answered wont need answer to the first problem,is how do I make my derma only once, and then turn it on/off.


if IsOpen == false then // Create derma frame else //Close derma frame

Then just toggle that when you create the frame.

take a look at these:

[lua]hook.Add( “Think”, “vguiBind”, function( )
if ( input.IsKeyDown( KEY_F1 ) )
if ( DermaWindow ) then
DermaWindow:Close( )
DermaWindow = nil
else
DermaWindow = vgui.Create( “DFrame” )
DermaWindow:SetPos( 100, 100 )
DermaWindow:SetSize( 200, 300 )
DermaWindow:SetVisible( true )
DermaWindow:MakePopup( )
end
end
end )[/lua]

Overv, that will still spam you with windows.

People have been asking this same question several times in the past month. My ‘pasting’ file is full of solutions for this same problem. Here’s one…

[lua]
– somewhere else
local pressed = false;

– your think hook
if( input.IsKeyDown( KEY_LEFT ) ) then

if( not pressed ) then
	
	pressed = true;
	
	-- do stuff on press
	
end

else

if( pressed ) then
	
	pressed = false;
	
	-- do stuff on release
	
end

end
[/lua]



if input.IsKeyDown( KEY_Q ) then
   if not DermaPanel then --if the dermapanel doesnt exist, create it :D
      DermaPanel = vgui.Create( "DFrame" )
      DermaPanel:SetPos( 10 , 10 )
      DermaPanel:SetSize( 200 , 200 )
      DermaPanel:SetTitle( "lol" )
   else
      DermaPanel:SetVisible( true ) --if the panel have already been created then just set it visible
   end
else
   if DermaPanel then --if we have already created out panel turn it off.
      DermaPanel:SetVisible( false )
   end
end



I assure you, this will not spam windows :slight_smile:

Perfect, I was looking a setvisible command for derma, but was probably looking in the wrong place :P.

The wiki is very well organized, it shouldn’t be hard to find anything once you get to know your way around it.

Well, one I can’t seem to find is a way to “refresh” the panel? As in it reloading it self… I don’t want to destroy it and then create it again… As the way it is now, works just like I want to it to!

What do you mean by refresh?

I think he means update with new info.

Yeah, but I meant what is he trying to refresh.

I’m displaying variables in my derma panel. How do I refresh the panel constantly, so if the variable changes, its always displayed at its current value.

Rebuild it!

Yup, ive been trying to do it yesterday with no success… but then I found the right command to close the frame :P. Thanks for the help guys! Tho, I am unsure if I might be back…