Trouble with insert key to open derma

I currently made a lua script where is opens my menu when i press insert
but the problem is that i have to quickly press insert or it flickers really quick and its really annoying
someone know how to fix this?


local mainMenu = nil
local visible = true
 
function GetKeyPress()
    if input.IsKeyDown(KEY_INSERT) then
        OpenMainMenu()
    end
end
hook.Add( "Think", "GetKeyPress", GetKeyPress )
 
function OpenMainMenu()
 
   
    visible = !visible
   
       
    if(  mainMenu == nil ) then
       
        mainMenu = vgui.Create( "DFrame" )
        mainMenu:SetSize( 620,  710 )
        mainMenu:SetTitle( "Derp" )
        mainMenu:SetDraggable( false )
        mainMenu:ShowCloseButton ( true )
        mainMenu:MakePopup()
        mainMenu:Center()
       
        mainMenu:SetVisible( true )
       
        visible = true
    else
       
        mainMenu:SetVisible ( visible )
    end
   
end

I wouldn’t use IsKeyDown because it will get called every frame while the key is down (I believe)
input.WasKeyPressed should be sufficient as it only gets called the first frame the button is pressed. You should also move change your Think to a Move hook.

I haven’t tested your code but I’m assuming your current issue is that its creating a menu, removing it, creating it, and so on because you’re using IsKeyDown over WasKeyPressed.

Thanks man iskeydown does work its just had to make it so if menu is on its 1 and when i press insert its 0 and you dont have to hold dont the button here is the code so if anyone has any trouble they can use my code and move there way around it <3



local mainMenu = nil
local visible = true
local alreadypressed = nil
 
 function OpenMainMenu()
 
    // Toggle the visibility
    visible = !visible
   
        // NOTE: Updated this. I had it saying "menu" before which was wrong.
    if(  mainMenu == nil ) then
        // Remove the "local" declaration and use the 'global (wthin scope)' variable.
        mainMenu = vgui.Create( "DFrame" )
        mainMenu:SetSize( 620,  710 )
        mainMenu:SetTitle( "Derp" )
        mainMenu:SetDraggable( false )
        mainMenu:ShowCloseButton ( true )
        mainMenu:MakePopup()
        mainMenu:Center()
        // Since this is the first time we are opening the menu, set the visibility to true.
        mainMenu:SetVisible( true )
        // And store the fact that it tis currently visible
        visible = true
    else
        // If the menu already exists, just toggle the visibility
        mainMenu:SetVisible ( visible )
    end
   
end

function GetKeyPress()
    if input.IsKeyDown(KEY_INSERT) and alreadypressed == 0 then
        OpenMainMenu()
		alreadypressed = 1
    elseif input.IsKeyDown(KEY_INSERT) then
	else
		alreadypressed = 0
	end
end
hook.Add( "Think", "GetKeyPress", GetKeyPress )

It does work but with WasKeyPressed you don’t need to do any of these checks other than making sure you don’t open the menu twice.

Either way it’s fine though, glad you figured it out.