Как сделать закрытие меню по нажатии?

У меня есть код который открывает панель на f4 и я хочу что бы она закрывалась не только по кнопке но и при нажатии на ESC или на ту же f4, буду очень благодарен за помощь

Примерно так:

local pnl = "Допустим твоя панель"
--к примеру берем хук PlayerButtonDown
hook.Add( "PlayerButtonDown", "PlayerButtonDownWikiExample", function( ply, btn)
      if (btn == KEY_F4 or btn == KEY_ESCAPE and IsValid(pnl)) then
            pnl:Remove()
      end
end)

-- В панели
-- Данный метод работает только когда панель в фокусе "внимания"
function PANEL:OnKeyCodePressed(key)
      if (IsValid(self) and (key == KEY_F4 or key == KEY_ESCAPE) then
            self:Remove() -- удаляем панель
      end
end

не помогает

Вот этот не помогает?

нет у меня через обычный DFrame

hook.Add("PlayerButtonDown", "PDA", function(ply,b)
    if !ply:Alive() then return end
    if b ~= KEY_F4 then return end

    if IsValid(f4Gvard) then f4Gvard:Remove() end
    f4Gvard = vgui.Create("DFrame")
    f4Gvard:SetSize( w * Vanila.Math.W, h * Vanila.Math.H )
    f4Gvard:Center()
    f4Gvard:SetTitle("")
    --f4Gvard:ShowCloseButton(false)
    f4Gvard.Paint = function(s, w, h)
        surface.SetDrawColor(120, 120, 120, 0)
    end
end)```

Ну так сделай тогда так.

f4Gvard.OnKeyCodePressed = function(key)
     if (IsValid(f4Gvard) and (key == KEY_F4 or key == KEY_ESCAPE) then
            f4Gvard:Remove() -- удаляем панель
      end
end
-- Или так
if (IsValid(f4Gvard)) then
    f4Gvard:Remove()
else
  f4Gvard = vgui.Create("DFrame")
    f4Gvard:SetSize( w * Vanila.Math.W, h * Vanila.Math.H )
    f4Gvard:Center()
    f4Gvard:SetTitle("")
    --f4Gvard:ShowCloseButton(false)
    f4Gvard.Paint = function(s, w, h)
        surface.SetDrawColor(120, 120, 120, 0)
    end
end

В том же хуке где ты панель создаешь

я ещё забыл добавить что у меня есть ещё одна панель с MakePopup()

По этому у меня не работало, как тут быть?

Скинь код

local w,h = 1500, 900
local pda_mat = Material("ui/stalker/pda/pda.png")
local Vanila = {}
Vanila.Math = {}
Vanila.Math.H = ScrH() / 1080
Vanila.Math.W = ScrW() / 1920

hook.Add("PlayerButtonDown", "PDA", function(ply,b)
    if !ply:Alive() then return end
    if b ~= KEY_F4 then return end

    if IsValid(f4Gvard) then f4Gvard:Remove() end
    f4Gvard = vgui.Create("DFrame")
    f4Gvard:SetSize( w * Vanila.Math.W, h * Vanila.Math.H )
    f4Gvard:Center()
    f4Gvard:SetTitle("")
    --f4Gvard:ShowCloseButton(false)
    f4Gvard.Paint = function(s, w, h)
        surface.SetDrawColor(120, 120, 120, 0)
    end

            local pda = vgui.Create( "EditablePanel", f4Gvard )
            pda:SetPos( 200 * Vanila.Math.W, 100 * Vanila.Math.H )
            pda:SetSize( w * Vanila.Math.W, h * Vanila.Math.H )
            pda:MakePopup()
            pda.Paint = function(s,w,h)
                --ебать текстура повявилась
                Derma_DrawBackgroundBlur(s,s.s)
                surface.SetDrawColor(40,40,40)
                surface.DrawRect(w * .08, h * .08, w * .81, h * .81)
                surface.SetDrawColor(color_white)
                surface.SetMaterial(pda_mat)
                surface.DrawTexturedRect(0,0,w,h)

                --задний фон
                surface.SetDrawColor(Color( 25, 25, 25))
                surface.DrawRect(w * .091, h * .110, w * .78, h * .775)

                --линия нарисовалась для кнопок:D
                surface.SetDrawColor(Color( 123, 123, 123, 200 ))
                surface.DrawLine( 135 * Vanila.Math.H, 750 * Vanila.Math.W, 1306 * Vanila.Math.H, 750 * Vanila.Math.W )

                draw.SimpleText( "Для работы КПК выберите нужный вам пункт ниже", "RPFont30", 500, 375, Color( 255, 255, 255, 50 ) )
            end
            pda:SetAlpha(0)
            pda:AlphaTo(255,.3,0)
                -- Кнопки
                local scrollPanel = pda:Add("DScrollPanel")
                scrollPanel:SetSize(w * Vanila.Math.W, 50 * Vanila.Math.H)
                scrollPanel:SetPos(135 * Vanila.Math.W, 745 * Vanila.Math.H)
                local horizontalScroller = vgui.Create("DHorizontalScroller", scrollPanel)
                horizontalScroller:SetSize(w * Vanila.Math.W, 55 * Vanila.Math.H)

                local buttonPanels = {
                    {name = "Настройки", panelType = "PDA_Settings"},
                    {name = "Статус", panelType = "PDA_Status"},
                    {name = "Энциклопедия", panelType = "PDA_Info"},
                    {name = "Отряд", panelType = "PDA_Info"},
                    {name = "Помощь", panelType = "PDA_Info"},
                    {name = "Констакты", panelType = "PDA_Info"}
                }

                for _, buttonData in ipairs(buttonPanels) do
                    local button = vgui.Create("DButton", horizontalScroller)
                    button:SetSize(150 * Vanila.Math.W, 50 * Vanila.Math.H)
                    button:SetText(buttonData.name)
                    button:Dock(LEFT)
                    button:DockMargin(5, 5, 0, 5)
                    button:SetFont( "RPFont20" )
                    button.active = false
                    button.Paint = function(self, w, h)
                        surface.DrawRect(0, 0, w, h)
                        self:SetTextColor(self.active and Color(0, 128, 0) or self.originalColor)
                    end
                    button.originalColor = Color(255, 255, 255)
                    button.DoClick = function()
                        for _, panelButton in ipairs(horizontalScroller:GetChildren()) do
                            panelButton.active = false
                        end
                        button.active = true
                        if IsValid(scrollPanel.activePanel) then
                            scrollPanel.activePanel:Remove()
                        end
                        scrollPanel.activePanel = vgui.Create(buttonData.panelType)
                    end
                end

end)

Это без кнопок на закрытие

На самом деле много вариантов:

pda.Think = function(this)
       if (!IsValid(f4Gvard) then pda:Remove() end
end
-- Или так
function f4Gvard:OnRemove() -- Данный метод рекомендую прописывать под pda после AlphaTo наверное
      if (IsValid(pda) then pda:Remove() end
end

у меня получилось:

            pda.Create = CurTime() + .5
            pda.Think = function(s)
                if (pda.Create<=CurTime() && (input.IsKeyDown(KEY_ESCAPE))) || !ply:Alive() then s:Remove() end
            end

Спасибо!!!