Adding a hotkey to concommand

So I almost had this work when I saw a guy said to use the hook think however that will not allow me to close it.


hook.Add( "Think", "MenuKeyListener", function()
    if ( input.IsKeyDown( KEY_F5 ) and !menuOpen ) then
        createMenu()
    end
end )

How would I make this work both to close and also open all at the same time? e.g. I open it but once I’m finished, I can close it with the same button? This is what is in the file right now.


concommand.Add("credits",OpenCreditsMenu)

Provide more code… OpenCreditsMenu and createMenu functions.

I think this will help:

DFrame/OnClose
Hide your DFrame (Panel) here.
and

DFrame/SetDeleteOnClose
Set DeleteOnClose to false (where you define your DFrame).
and

Panel:SetVisible
Pass true to show your DFrame; otherwise false to hide it.

Consider using

GM/PlayerButtonDown instead of a Think hook and the input library too.

Here’s more code ->


hook.Add( "Think", function()
    if ( input.IsKeyDown( KEY_F4 ) and !menuOpen ) then
        OpenCreditsMenu()
    end
end )

I already have it set to work with it and then open the panel but it does not close using the same F4 so both answers you provide did not help.


hook.Add( "Think", function()
    if ( input.IsKeyDown( KEY_F4 ) and !menuOpen ) then
        OpenCreditsMenu()
    else
        CloseCreditsMenu()
end

Tell me if I’m wrong, but this is my idea about this

That still does not work

Show us the OpenCreditsMenu() and CloseCreditsMenu() commands…
Also by the name I’m assuming CloseCreditsMenu() closes the credit menu.
I am guessing you are using local var = vgui.Create(…) as that is what most tutorials say.
This is merely an assumpion as I can’t see the code, but perhaps CloseCreditsMenu() is trying to use a non-existent variable?

Okay, here is the whole file of code, ->


local MyCredits = 0
local MyPurchases = {}

net.Receive("Donation_Store_Update",function()
   MyCredits = net.ReadDouble()
   MyPurchases = net.ReadTable()
end)

net.Receive("Donation_Notify", function()
   chat.AddText(Color(225,50,50),"[PM Donation Store]:  ",Color(255,255,255),net.ReadString())
end)

surface.CreateFont("Cred_Tit", {
	font = "Calibri", 
	size = 21, 
	weight = 500, 
})

function OpenPurchaseHistory()

   local Purchase_Frame = vgui.Create("DFrame")
   Purchase_Frame:SetSize(900,500)
   Purchase_Frame:SetPos( ScrW() / 2 - 450 , ScrH() / 2 - 250)
   Purchase_Frame:SetTitle("")
   Purchase_Frame:SetDraggable(false)
   Purchase_Frame:ShowCloseButton(false)
   Purchase_Frame:MakePopup()
   
   local Close_Button = vgui.Create("DButton",Purchase_Frame)
   Close_Button:SetPos(Purchase_Frame:GetWide() - 37,5)
   Close_Button:SetSize(32,16)
   Close_Button:SetText("")
   Close_Button.DoClick = function()
      Purchase_Frame:Close()
   end
   Close_Button.Paint = function( Ignore, w , h )
      draw.RoundedBox(0,0,0,w,h,Color(238,17,17))
      surface.SetDrawColor(255,255,255)
   end  
   Purchase_Frame.Paint = function( Ignore , w , h )
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29))
      draw.RoundedBox(0,0,26,w,2,Color(255,255,255))
      draw.RoundedBox(0,Purchase_Frame:GetWide() - 46,0,2,26,Color(255,255,255))
   end
   

   DermaList = vgui.Create( "DPanelList", Purchase_Frame )
   DermaList:SetPos( 5,36 )
   DermaList:SetSize( Purchase_Frame:GetWide() - 10, Purchase_Frame:GetTall() - 36 - 5 )
   DermaList:SetSpacing( 10 )
   DermaList:EnableHorizontal( true )
   DermaList:EnableVerticalScrollbar( true )
 
   for _,v in pairs(MyPurchases) do
   
      local ItemPanel = vgui.Create("DPanel")
      ItemPanel:SetSize( DermaList:GetWide() / 2 - 10 , 75)
      ItemPanel:SetPos( 5 , 5 )
      ItemPanel.Paint = function ( Ignore, w , h )     
         draw.RoundedBox(0,0,0,w,h,Color(22, 160, 133,155))
         draw.SimpleText( v.Name, "Cred_Tit", w/2, 10, color_white, TEXT_ALIGN_CENTER, 1 )
         draw.SimpleText( "Date Purchased: " .. v.Date,"Cred_Tit", 5, 26, color_white, TEXT_ALIGN_LEFT, 1 )
         draw.SimpleText( "Price: " .. string.Comma(tostring(v.Price)) .. " Credits / $" .. v.Price / 100 , "Cred_Tit", 5, 41, color_white, TEXT_ALIGN_LEFT, 1 )
         draw.RoundedBox(0,0,73,w,2,Color(255,255,255))
      end
      
      DermaList:AddItem(ItemPanel)
      
   end
   
end

function OpenTransferMenu()

   local CurrentPlayer = ""

   local Transfer_Frame = vgui.Create("DFrame")
   Transfer_Frame:SetSize(700,140)
   Transfer_Frame:SetPos( ScrW() / 2 - 350 , ScrH() / 2 - 65)
   Transfer_Frame:SetTitle("")
   Transfer_Frame:SetDraggable(false)
   Transfer_Frame:ShowCloseButton(false)
   Transfer_Frame:MakePopup()
   
   local Close_Button = vgui.Create("DButton",Transfer_Frame)
   Close_Button:SetPos(Transfer_Frame:GetWide() - 37,5)
   Close_Button:SetSize(32,16)
   Close_Button:SetText("X")
   Close_Button.DoClick = function()
      Transfer_Frame:Close()
   end
   Close_Button.Paint = function( Ignore, w , h )
      draw.RoundedBox(0,0,0,w,h,Color(238,17,17))
      surface.SetDrawColor(255,255,255)
   end  
   Transfer_Frame.Paint = function( Ignore , w , h )
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29,225))
      draw.RoundedBox(0,0,26,w,2,Color(255,255,255,225))
      draw.RoundedBox(0,Transfer_Frame:GetWide() - 46,0,2,26,Color(255,255,255,225))
   end
   
   local DComboBox = vgui.Create( "DComboBox", Transfer_Frame )
   DComboBox:SetPos( 5, 35 )
   DComboBox:SetSize( Transfer_Frame:GetWide() - 10 , 20 )
   DComboBox:SetValue( "Choose PM Player" )
   for k,v in pairs(player.GetAll()) do
      DComboBox:AddChoice(v:Nick())
   end
   DComboBox.OnSelect = function( panel, index, value )
      CurrentPlayer = value
      print(CurrentPlayer)
   end

   local TextEntry = vgui.Create( "DTextEntry", Transfer_Frame )
   TextEntry:SetPos( 5, 60 )
   TextEntry:SetSize( Transfer_Frame:GetWide() - 10, 24 )
   TextEntry:SetText( "Enter Amount" )
   
   
   local DButton = vgui.Create( "DButton", Transfer_Frame )
   DButton:SetPos( 5, 89 )
   DButton:SetText( "Send PM Credits!" )
   DButton:SetSize( Transfer_Frame:GetWide() - 10, 32 )
   DButton.DoClick = function()

      net.Start("Donation_Store_Transfer")
         net.WriteString(CurrentPlayer)
         net.WriteDouble(tonumber(TextEntry:GetValue()))
      net.SendToServer()
   end   

   
end


function OpenCreditsMenuStore()
 
   local Credit_Frame = vgui.Create("DFrame")
   Credit_Frame:SetSize(900,500)
   Credit_Frame:SetPos( ScrW() / 2 - 450 , ScrH() / 2 - 250)
   Credit_Frame:SetTitle("ParticularlyMe Donation / Credit Store")
   Credit_Frame:SetDraggable(false)
   Credit_Frame:ShowCloseButton(false)
   Credit_Frame:MakePopup()
   
   local Close_Button = vgui.Create("DButton",Credit_Frame)
   Close_Button:SetPos(Credit_Frame:GetWide() - 37,5)
   Close_Button:SetSize(32,16)
   Close_Button:SetText("X")
   Close_Button.DoClick = function()
      Credit_Frame:Close()
   end
   Close_Button.Paint = function( Ignore, w , h )
      draw.RoundedBox(0,0,0,w,h,Color(238,17,17))
      surface.SetDrawColor(255,255,255)
   end  
   Credit_Frame.Paint = function( Ignore , w , h )
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29))
      draw.RoundedBox(0,0,26,w,2,Color(255,255,255))
      draw.RoundedBox(0,Credit_Frame:GetWide() - 46,0,2,26,Color(255,255,255))
   end
   
   DermaList = vgui.Create( "DPanelList", Credit_Frame )
   DermaList:SetPos( 5,36 )
   DermaList:SetSize( Credit_Frame:GetWide() - 10, Credit_Frame:GetTall() - 36 - 5 )
   DermaList:SetSpacing( 10 )
   DermaList:EnableHorizontal( false )
   DermaList:EnableVerticalScrollbar( true )
 
   for _,v in pairs(CreditStore.Items) do
   
      local ItemPanel = vgui.Create("DPanel")
      ItemPanel:SetSize( DermaList:GetWide() - 10 , 75)
      ItemPanel:SetPos( 5 , 5 )
      ItemPanel.Paint = function ( Ignore, w , h )     
         draw.RoundedBox(0,0,0,w,h,Color(22, 160, 133,155))
         draw.SimpleText( v.Name, "Cred_Tit", w/2, 10, color_white, TEXT_ALIGN_CENTER, 1 )
         draw.SimpleText( "Description: " .. v.Description, "Cred_Tit", 5, 26, color_white, TEXT_ALIGN_LEFT, 1 )
         draw.SimpleText( "Price: " .. string.Comma(tostring(v.Price)) .. " Credits", "Cred_Tit", 5, 41, color_white, TEXT_ALIGN_LEFT, 1 )
         draw.RoundedBox(0,0,73,w,2,Color(255,255,255))
      end
      
   local PurchaseItem = vgui.Create("DButton",ItemPanel)
   PurchaseItem:SetSize(ItemPanel:GetWide() / 2,28)
   PurchaseItem:SetPos(ItemPanel:GetWide() - PurchaseItem:GetWide() * 1.5 + 10 ,50)   
   PurchaseItem:SetText("")
   PurchaseItem.Paint = function(i,w,h)
      local Text = "You Can't Afford This"
      
      if MyCredits >= v.Price then
         Text = "Purchase PM Item"
      end
      
      draw.RoundedBox(0,0,0,w,h,Color(39,39,39))
      draw.SimpleText( Text, "Cred_Tit", w/2, h/2, color_white, TEXT_ALIGN_CENTER, 1 )
   end
   
   PurchaseItem.DoClick = function()
   
      net.Start("Donation_Store_Buy")
         net.WriteDouble(_)
      net.SendToServer()
   
   end
   
      DermaList:AddItem(ItemPanel)
      
   end

end


function OpenCreditsMenu()
 
   local Credit_Frame = vgui.Create("DFrame")
   Credit_Frame:SetSize(700,235)
   Credit_Frame:SetPos( ScrW() / 2 - 350 , ScrH() / 2 - 117.5)
   Credit_Frame:SetTitle("")
   Credit_Frame:SetDraggable(false)
   Credit_Frame:ShowCloseButton(false)
   Credit_Frame:MakePopup()
   
   local Close_Button = vgui.Create("DButton",Credit_Frame)
   Close_Button:SetPos(Credit_Frame:GetWide() - 37,5)
   Close_Button:SetSize(32,16)
   Close_Button:SetText("X")
   Close_Button.DoClick = function()
      Credit_Frame:Close()
   end
   Close_Button.Paint = function( Ignore, w , h )
      draw.RoundedBox(0,0,0,w,h,Color(238,17,17))
      surface.SetDrawColor(255,255,255)
   end  
   Credit_Frame.Paint = function( Ignore , w , h )
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29,225))
      draw.RoundedBox(0,0,26,w,2,Color(255,255,255,225))
      draw.RoundedBox(0,0,112,w,2,Color(255,255,255,225))
      draw.RoundedBox(0,Credit_Frame:GetWide() - 46,0,2,26,Color(255,255,255,225))
   end
   
   local PlayerInfo = vgui.Create("DPanel",Credit_Frame)
   PlayerInfo:SetPos(5,33)
   PlayerInfo:SetSize(690,74)
   PlayerInfo.Paint = function(Ignore,w,h) 
      draw.RoundedBox(0,0,0,w,h,Color(231, 76, 60,225))
      draw.SimpleText( LocalPlayer():Nick(), "Cred_Tit", 74, 11, color_white, TEXT_ALIGN_LEFT, 1 )
      draw.SimpleText( "Your Current PM Credits: " .. string.Comma(MyCredits), "Cred_Tit", 74, 26, color_white, TEXT_ALIGN_LEFT, 1 )
      draw.SimpleText( "Your Current PM Rank: " .. LocalPlayer():GetUserGroup(), "Cred_Tit", 74, 41, color_white, TEXT_ALIGN_LEFT, 1 )
   end
   
   local ControlsFrame = vgui.Create("DPanel",Credit_Frame)
   ControlsFrame:SetPos(5,120)
   ControlsFrame:SetSize(690,106)
   ControlsFrame.Paint = function(Ignore,w,h) 
      draw.RoundedBox(0,0,0,w,h,Color(231, 76, 60,125))
      
   end 

   local Transfer = vgui.Create("DButton",ControlsFrame)
   Transfer:SetPos(5,40)
   Transfer:SetSize(680,28)
   Transfer:SetText("")
   Transfer.Paint = function(i,w,h)
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29))
      draw.SimpleText( "Transfer PM Credits", "Cred_Tit", w/2, h/2, color_white, TEXT_ALIGN_CENTER, 1 )
   end
   
   Transfer.DoClick = function()
      Credit_Frame:Remove()
      OpenTransferMenu()
   end
   
 local History = vgui.Create("DButton",ControlsFrame)
   History:SetPos(5,75)
   History:SetSize(680,28)
   History:SetText("")
   History.Paint = function(i,w,h)
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29))
      draw.SimpleText( "Purchase History", "Cred_Tit", w/2, h/2, color_white, TEXT_ALIGN_CENTER, 1 )
   end
   
History.DoClick = function(i,w,h)
      Credit_Frame:Remove()
      OpenPurchaseHistory()
   end
   
   local OpenStore = vgui.Create("DButton",ControlsFrame)
   OpenStore:SetPos(5,5)
   OpenStore:SetSize(680,28)
   OpenStore:SetText("")
   OpenStore.Paint = function(i,w,h)
      draw.RoundedBox(0,0,0,w,h,Color(29,29,29))
      draw.SimpleText( "Open PM Store", "Cred_Tit", w/2, h/2, color_white, TEXT_ALIGN_CENTER, 1 )
   end
   
      OpenStore.DoClick = function()
      Credit_Frame:Remove()
      OpenCreditsMenuStore()
   end
   
   local Avatar = vgui.Create( "AvatarImage", PlayerInfo )
   Avatar:SetSize( 64, 64 )
   Avatar:SetPos( 5, 5 )
   Avatar:SetPlayer( LocalPlayer(), 64 )
   
end

hook.Add( "Think", function()
    if ( input.IsKeyDown( KEY_F4 ) and !menuOpen ) then
        OpenCreditsMenu()
    end
end )

Where is CloseCreditsMenu() defined then?

Oh, I completely misunderstood.
Should’ve read more carefully.
Simply add something such as “menuopen = true” on the think hook (If the key is prssed ofc), and add a check to do the following:
If the menu is open, close it and set menuopen to false
If the menu is closed, open it and set menuopen to true

Can you put that in code form to be more understandable?


hook.Add( "Think", function()
    if ( input.IsKeyDown( KEY_F4 ) and !menuOpen ) then
        menuopen = true
    else
        menuopen = false
    end
end )