Vgui doesn't show up when I press a key

Hi, what is the problem? I made a console command to show a vgui, but it wont show up.

init.lua

[lua]AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)

include(“shared.lua”)

function GM:PlayerSpawn( ply )

self.BaseClass:PlayerSpawn( ply )   

ply:SetGravity(0.75)  
ply:SetMaxHealth(100, true)  

ply:SetWalkSpeed(720)  
ply:SetRunSpeed(720) 

end

function GM:PlayerInitialSpawn(ply)

end

function GM:PlayerLoadout(ply)
if ply:Team() == 1 then
ply:Give(“weapon_pistol”)

elseif ply:Team() == 2 then
	ply:Give("weapon_pistol")
	
end

end

function sb_team1(ply)
if ply:Team() != 1 then
ply:KillSilent()
ply:SetTeam(1)
ply:Spawn()
ply:PrintMessage(HUD_PRINTTALK, ply:Nick()…" is now a Combine")
end
end

function sb_team2(ply)
if ply:Team() != 2 then
ply:KillSilent()
ply:SetTeam( 2 )
ply:Spawn()
ply:PrintMessage(HUD_PRINTTALK, ply:Nick()…" is now a Rebel")
end
end

concommand.Add(“show_classchange_menu”, classchange)
RunConsoleCommand(“bind M show_classchange_menu”)[/lua]

shared.lua

[lua]GM.Name = “Half Life 2: Team Fortress”
GM.Author = “Tera”
GM.Email = “gabrycir@hotmail.it”

DeriveGamemode( “base” )

team.SetUp( 1, “Combine”, Color( 125, 125, 125, 255 ) )
team.SetUp( 2, “Resistance”, Color( 255, 255, 255, 255 ) )

function classchange()
if CLIENT then
local Frame = vgui.Create(“Frame”);
Frame:SetSize(200, 200)
Frame:SetPos(100, 100)
Frame:SetVisible(true)
Frame:MakePopup()
Frame:PostMessage(“SetTitle”, “text”, “This is the title”)

	local Button = vgui.Create("Button", Frame)
	Button:SetText("Click me")
	Button:SetPos(30, 5)
	Button:SetWide(100)
	function Button:DoClick()
		self:SetText("Clicked")
		local menu123 = DermaMenu() 
		menu123:AddOption("hello", function() Msg("Hello") end ) 
		menu123:AddOption("how", function() Msg("How") end )
		menu123:AddOption("are", function() Msg("Are") end )
		menu123:AddOption("you", function() Msg("You") end )
		menu123:AddOption("?", function() Msg("?") end )
		menu123:Open()
	end
end

end[/lua]

cl_init.lua

[lua]include( “shared.lua” )
[/lua]

Try the derma ones instead:

vgui.Create( “DFrame” );

vgui.Create( “DButton” );

You can’t execute the ‘bind’ command from lua.

Also make the concommand.add in the shared file where the function is created (inside the Client bit.) All you’re doing is adding an empty console command as you’re adding it on the server and your function starts with if( CLIENT ) so the server sees it as:

FunctionName()
// Nothing in here as it is all client side.
end

So what would the code for it be?

You have to bind it in-game.

Isnt there another way in which I can execute a function when a key is pressed?

It’s not that simple. You need to use the ‘input’ library and check for input in a think hook.

Can you help me more in depth?

The only way to detect keys that aren’t in the IN_KEYS (http://wiki.garrysmod.com/?title=IN_KEYS) would be to use input.IsKeyDown, like so:

[lua]hook.Add(“Think”,“CheckKeys”,function()
if input.IsKeyDown(65) then LocalPlayer():ChatPrint(“You pressed space!”) end
end)[/lua]

However, one problem is that even if you press the key rather quickly, because this hook runs every tick or so it’s very likely that you’ll get “You pressed space!” 3 or 4 times. For example, I made a function that detects the numpad, so to avoid this issue I did:

[lua]local keys = {
{38,1},
{39,2},
{40,3},
{41,4},
{42,5},
{43,6},
{44,7},
{45,8},
{46,9},
}

local keypressed = false
hook.Add(“Think”,“ExpKeys”,function()
for _,v in ipairs(keys) do if input.IsKeyDown(v[1]) then
if keypressed == false then RunConsoleCommand(“exp_useskill”,v[2])
keypressed = true
timer.Destroy(“Key”)
else if not timer.IsTimer(“Key”) then timer.Create(“Key”,0.25,1,function() keypressed = false end) end
end
end end
end)[/lua]

I tried doing this
[lua]GM.Name = “Half Life 2: Team Fortress”
GM.Author = “Tera”
GM.Email = “gabrycir@hotmail.it”

DeriveGamemode( “base” )

team.SetUp( 1, “Combine”, Color( 125, 125, 125, 255 ) )
team.SetUp( 2, “Resistance”, Color( 255, 255, 255, 255 ) )

function classchange()
Msg (“M HAS BEEN PRESD”)

if CLIENT then
	local Frame = vgui.Create("Frame");
	Frame:SetSize(200, 200)
	Frame:SetPos(100, 100)
	Frame:SetVisible(true)
	Frame:MakePopup()
	Frame:PostMessage("SetTitle", "text", "This is the title")
	
	local Button = vgui.Create("Button", Frame)
	Button:SetText("Click me")
	Button:SetPos(30, 5)
	Button:SetWide(100)
	function Button:DoClick()
		self:SetText("Clicked")
		local menu123 = DermaMenu() 
		menu123:AddOption("hello", function() Msg("Hello") end ) 
		menu123:AddOption("how", function() Msg("How") end )
		menu123:AddOption("are", function() Msg("Are") end )
		menu123:AddOption("you", function() Msg("You") end )
		menu123:AddOption("?", function() Msg("?") end )
		menu123:Open()
	end
end

end

function KeyPressed (P, key)
if (key == 23) then
classchange()
end
end

hook.Add( “KeyPress”, “KeyPressedHook”, KeyPressed )[/lua]

but it seems like the function doesn’t get executed

Question, does the message print to console the “M has been pressed.”

If that happens but not the stuff in the if( CLIENT ) then please read my earlier reply (my second one.)

KeyPress is a completely different hook. You can’t use it to detect keyboard keys.

“m has been pressed” is not shown either, so probably the whole function isnt executed

[editline]08:59PM[/editline]

@nevec, isnt it made to detect when a key is down? i read it on the wiki

It detects when a special bind is down - when you press your sprint key, use key, walk key, etc. To actually detect keyboard input, use Entoros’ code.

I don’t understand that code, so I don’t know where/how to put it. Is someone up for a short explanation? (sorry i come from python, it’s similar but different in the basic things like for)

Ok, here’s how it goes. The following hooks:
KeyPress
KeyDown
KeyRelease

These hooks only run when an IN_KEY is pressed. IN_KEYS are various functions for use in the default HL2 keyboard, like walking, firing, and sprinting.

However, these hooks do not detect any other keys, which is pretty much the whole keyboard and numpad. Yet there is a function that can do this, but the unfortunate thing is it can only be called on the client; the answer is input.IsKeyDown().

Thus, the only way to detect if a key that’s not an IN_KEY is to use input.IsKeyDown in a Think hook (think basically just runs a lot). Refer to the code in my previous post on this.

[lua]
local keydown;

local function Think( )

if( input.IsKeyDown( KEY_M ) and keydown ~= true ) then
	
	-- m key was pressed
	
	keydown = true;
	
elseif( keydown == true ) then
	
	-- m key was released
	
	keydown = false;
	
end

end
hook.Add( “Think”, “CheckKeyInput”, Think );
[/lua]

A bit simplified version.

~= True means all but True?

[editline]09:57PM[/editline]

or not true?

~= = != = not equal