Open derma on key

I’ve already manage to bind the derma panel to a key however when I hit it, it opens like 10 dermas on eachother.
If anyone could help me with how to make it so that it only opens one



function ActMenu()
local Frame = vgui.Create( "DFrame" )
Frame:SetSize( 328, 101 )
Frame:Center()
Frame:SetTitle("")
Frame:MakePopup()
Frame:SetVisible(true)
Frame:ShowCloseButton(false)
Frame.Paint = function( self, w, h ) 
	draw.RoundedBox( 0, 0, 0, w, h, Color( 30, 30, 30, 220 ) )
end
end
hook.Add( "ActMenuShow", "ActMenuShow", ActMenu )
hook.Add( "Think", "MenuKeyListener", function()
    if ( input.IsKeyDown( KEY_G ) ) then
        ActMenu()
    end
end )




If possible I would even like to do it so that it is only open whilst you hold the key and when you release it it disappears

Since the think hook is called every frame, you’re gonna need to add a simple timer check to prevent the menu from opening every frame while the key is pressed.

Example:
[lua]
local key_down_timer = CurTime()

local key_down_delay = 1

hook.Add( “Think”, “iskeydown_example”, function()

if ( input.IsKeyDown( KEY_G ) and key_down_timer <= CurTime() ) then

	-- Open your derma here

	key_down_timer = CurTime() + key_down_delay -- reset the timer

end

end )
[/lua]


local f1pressed = false

hook.Add("Think", "input_menukey", function()
	if input.IsKeyDown(KEY_F1) and not f1pressed then
		f1pressed = true -- When F1 is pressed
		ToggleMenu() -- I toggle my menu here but you may want to do something like ActMenu() instead
	elseif (not input.IsKeyDown(KEY_F1)) and f1pressed then
		f1pressed = false -- When F1 is released
                -- And close your menu here
	end
end)

This is what I use for my gamemode’s options menu. Works perfectly.

Hopefully it helps.


local keyDown;

hook.Add("Think", "", function()
	if (input.IsButtonDown(KEY_G) and !keyDown) then
		ActMenu();
	end
	
	keyDown = input.IsButtonDown(KEY_G);
end);


This is my way of doing it.

How would I close the menu from outside the ActMenu() function?

:snip: misread the question

Make the frame variable OUTSIDE the function:



local Frame
function ActMenu()
Frame = vgui.Create( "DFrame" )
// the rest of the code


Then just do Frame:Remove()


local Frame
local keyDown

function ActMenu()
	if (Frame) then
		Frame:Remove()
		Frame = nil
	else
		Frame = vgui.Create( "DFrame" )
		Frame:SetSize( 328, 101 )
		Frame:Center()
		Frame:SetTitle("")
		Frame:MakePopup()
		Frame:SetVisible(true)
		Frame:ShowCloseButton(false)
		
		Frame.Paint = function( self, w, h ) 
			draw.RoundedBox( 0, 0, 0, w, h, Color( 30, 30, 30, 220 ) )
		end
	end
end

hook.Add("Think", "MenuKeyListener", function()
	if (input.IsButtonDown(KEY_G) and !keyDown) then
		ActMenu()
	end
	
	keyDown = input.IsButtonDown(KEY_G)
end);


That works great but I’m trying to get the hold down

[editline]3rd November 2016[/editline]

Tried that but when I used my buttons that I have on the derma on their DoClick I have 1 command and then Frame:Close() and it does an error

[editline]3rd November 2016[/editline]

I’m using this, it works perfectly. How would I make it so that when the menu is open already it doesn’t open a new one when I click it again?

[editline]3rd November 2016[/editline]

I would like to use this but don’t know how to

Misread the question, thought you wanted a toggle.


local Frame

function ActMenu()
	if (Frame and !input.IsButtonDown(KEY_G)) then
		Frame:Remove()
		Frame = nil
	elseif (input.IsButtonDown(KEY_G) and !Frame) then
		Frame = vgui.Create( "DFrame" )
		Frame:SetSize( 328, 101 )
		Frame:Center()
		Frame:SetTitle("")
		Frame:MakePopup()
		Frame:SetVisible(true)
		Frame:ShowCloseButton(false)
		
		Frame.Paint = function( self, w, h ) 
			draw.RoundedBox( 0, 0, 0, w, h, Color( 30, 30, 30, 220 ) )
		end
	end
end

hook.Add("Think", "MenuKeyListener", function()
	ActMenu()
end);


Opens up multiple menus and they aren’t hold down only and it makes the buttons on it do this error [ERROR] lua/derma.lua:29: attempt to index upvalue ‘Frame’ (a nil value)

  1. DoClick - lua/derma.lua:29
  2. unknown - lua/vgui/dlabel.lua:232