Detect KEY_keys with hooks

Is there anyway to detect KEY_ keys with hooks? All the hooks I find is for IN_keys.

The best thing I could think of was this:

[lua]

ENumerableKeys = {KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z };

KeyCharacters = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”};

function GM:Think()
for i = 1, table.getn(KeyCharacters) do
if (input.IsKeyDown( ENumerableKeys* ) ) then
print("pressed key: " … KeyCharacters* … " ENUMBER: " … ENumerableKeys*);
end
i = i + 1;
end
end
[/lua]

The thing is that it just spams the print in console.
BTW, the reason I’m doing this is to make a custom chatbox. Please tell me if I’m making this too complicated and there is a much easier way.

Edit:
Also, when I say Custom I mean made from scratch.

There is a much easier way.

DTextEntry

Well, I made my own derma you see.

[lua]
function OpenChatBox()

local DFrame1 = vgui.Create(‘DFrame’)
DFrame1:SetSize(518, 262)
DFrame1:SetPos(16, 370)
DFrame1:SetTitle(‘Do not steal my gamemode name…’)
DFrame1:SetSizable(false)
DFrame1:SetDeleteOnClose(true)
DFrame1:SetDraggable(false)
DFrame1:ShowCloseButton(false)
DFrame1:MouseCapture( false )
DFrame1:MakePopup();

local DPanel1 = vgui.Create(‘DPanel’, DFrame1)
DPanel1:SetSize( 630, 30)
DPanel1:SetPos(0, 230)
DPanel1:MouseCapture( false )

DTextEntry1 = vgui.Create(‘DTextEntry’, DFrame1)
DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:SetText(’’)
DTextEntry1:MouseCapture( false )
DTextEntry1.OnEnter = function() gui.EnableScreenClicker( false ) gui.HideGameUI() DFrame1:Close() DFrame1:Remove() end

local DLabel1 = vgui.Create(‘DLabel’, DFrame1)
DLabel1:SetPos(25, 25)
DLabel1:SetText(‘Surface Library goes here.’)
DLabel1:SizeToContents()
DLabel1:MouseCapture( false )

local DButton1 = vgui.Create(‘DButton’, DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 200)
DButton1:SetText(‘Send’)
DButton1:MouseCapture( false )
DButton1.DoClick = function() end

local DCombobox = vgui.Create(‘DComboBox’, DFrame1)
DCombobox:SetPos(380, 200)
DCombobox.OnMousePressed = function() end
DCombobox:MouseCapture( false )
function DCombobox.OnSelect(Index, Value, Data) end

end
[/lua]

The problem is I have to select the DTextEntry and then it takes over the controls even after I’ve closed the Frame. I have to press the escape button if I want to do anything after it’s closed.

You shouldn’t be removing your frame after your are done with it, just closing it.
DTextEntry:RequestFocus() --This might help, never used it.

Thanks.

I know I shouldn’t delete it but it was a desperate attempt to fix it.
The DTextEntry:RequestFocus( ) worked fine. But when the Frame is closed the controls are still
unavailable until I press escape.

Edit:

Maybe I should call it with the hud paint hook? Make a check to see if it needs to be shown and such.
Sorry if this suggestion is foolish.

Well, guys. I’m gonna work on something else because I’ve really searched all the places I could for this.

Here’s my final code:
[lua]
// chatbox.lua
function OpenChatBox()

local DFrame1 = vgui.Create(‘DFrame’)
DFrame1:SetSize(518, 262)
DFrame1:SetPos(16, 370)
DFrame1:SetTitle(‘FusionBase Chatbox’)
DFrame1:SetSizable(false)
DFrame1:SetDeleteOnClose(false)
DFrame1:SetDraggable(false)
DFrame1:ShowCloseButton(false)
DFrame1:MouseCapture( false )
DFrame1:SetMouseInputEnabled( false )
DFrame1:MakePopup();

local DPanel1 = vgui.Create(‘DPanel’, DFrame1)
DPanel1:SetSize( 630, 30)
DPanel1:SetPos(0, 230)
DPanel1:MouseCapture( false )

DTextEntry1 = vgui.Create(‘DTextEntry’, DFrame1)
DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:SetText(’’)
DTextEntry1:MouseCapture( false )
DTextEntry1:SetMouseInputEnabled( false )
DTextEntry1.OnEnter = function() TextEntryLoseFocus(DTextEntry1) CloseDermaMenus() gui.EnableScreenClicker( false ) gui.HideGameUI() DFrame1:Close() end
DTextEntry1:RequestFocus( )

local DLabel1 = vgui.Create(‘DLabel’, DFrame1)
DLabel1:SetPos(25, 25)
DLabel1:SetText(‘Surface Library goes here.’)
DLabel1:SizeToContents()
DLabel1:MouseCapture( false )

local DButton1 = vgui.Create(‘DButton’, DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 200)
DButton1:SetText(‘Send’)
DButton1:MouseCapture( true )
DButton1.DoClick = function() DFrame1:Close() end

local DCombobox = vgui.Create(‘DComboBox’, DFrame1)
DCombobox:SetPos(380, 200)
DCombobox.OnMousePressed = function() end
DCombobox:MouseCapture( false )
function DCombobox.OnSelect(Index, Value, Data) end

end
[/lua]

[lua]
// cl_init.lua

function GM:StartChat( isTeamChat )
OpenChatBox();
return true;
end

function GM:Think()
for i = 1, table.getn(KeyCharacters) do
if (input.IsKeyDown( ENumerableKeys* ) ) then
print("pressed key: " … KeyCharacters* … " ENUMBER: " … ENumerableKeys*);
end
i = i + 1;
end
end
[/lua]

As you can see I’ve made some desperate attempts to fix it.

Maybe Derma is made to disrupt your controls. So I’ll look into a different way of creating my chatbox.

have you tried DFrame1:SetKeyboardInputEnabled( false )

Now I have, and it doesn’t work.
I appreciate your help.

I’ve worked further on it:

[lua]
// chatbox.lua
print(“CHATBOX LOADED”)

local chatboxpos = {}

chatboxpos.x = 16;
chatboxpos.y = 370;

local chatboxtext = {"", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”}

function SendChatMessage(ply, text)
if (IsValid(ply)) then
if (text != nil && text != “”) then
net.Start(“ChatNMsg”);
net.WriteString(text);
net.WriteEntity(ply);
net.SendToServer();
end;
end;
end;

function OpenChatBox()

local DFrame1 = vgui.Create('DFrame')
DFrame1:SetSize(518, 262)
DFrame1:SetPos(chatboxpos.x , chatboxpos.y )
DFrame1:SetTitle('No faggots you are not gonna get my gamemode name')
DFrame1:SetSizable(false)
DFrame1:SetDeleteOnClose(false)
DFrame1:SetDraggable(false)
DFrame1:ShowCloseButton(false)
DFrame1:MouseCapture( false )
DFrame1:SetMouseInputEnabled( false )
DFrame1:SetKeyboardInputEnabled( false )
DFrame1:MakePopup();

local DPanel1 = vgui.Create('DPanel', DFrame1)
DPanel1:SetSize( 630, 30)
DPanel1:SetPos(0, 230)
DPanel1:MouseCapture( false )

DTextEntry1 = vgui.Create('DTextEntry', DFrame1)
DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:SetText('')
DTextEntry1:MouseCapture( false )
DTextEntry1:SetMouseInputEnabled( false )
DTextEntry1.OnEnter = function() DFrame1:SetKeyboardInputEnabled( false ) SendChatMessage(LocalPlayer(), DTextEntry1:GetValue()) TextEntryLoseFocus(DTextEntry1) CloseDermaMenus() gui.EnableScreenClicker( false ) gui.HideGameUI() DFrame1:Close() end
DTextEntry1:RequestFocus( )

local DLabel1 = vgui.Create('DLabel', DFrame1)
DLabel1:SetPos(20, 25)
DLabel1:SetText('Surface Library goes here.')
DLabel1:SizeToContents()
DLabel1:MouseCapture( false )

local DLabel2 = vgui.Create('DLabel', DFrame1)
DLabel2:SetPos(20, 200)
DLabel2:SetText(chatboxtext[1])
DLabel2:SizeToContents()
DLabel2:MouseCapture( false )

local DButton1 = vgui.Create('DButton', DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 200)
DButton1:SetText('Send')
DButton1:MouseCapture( true )
DButton1.DoClick = function() DFrame1:Close() end

local DCombobox = vgui.Create('DComboBox', DFrame1)
DCombobox:SetPos(350, 200)
DCombobox:SetSize(100, 25)
DCombobox.OnMousePressed = function() DCombobox:OpenMenu() end
function DCombobox.OnSelect(Index, Value, Data) end
DCombobox:AddChoice("Chat")
DCombobox:AddChoice("PM")
DCombobox:AddChoice("OOC")
DCombobox:AddChoice("LOOC")
DCombobox:AddChoice("Advert")

function DFrame1.UpdateChat(data)
	local pos = chatboxpos;
	local text = data:ReadString();
	local stringtable;
	print(text)
	print(pos.x .. " " .. pos.y)
	
	surface.SetFont( "Trebuchet24" )
	surface.SetTextColor( Color(255, 255, 255, 255) )
	surface.SetTextPos( 25 , 40 );
	surface.DrawText( "niggerest " .. text );
	
	if (string.len(text) > 20) then
	stringtable = string.Explode(" ", text, false);
	PrintTable(stringtable);
	
	text = "";
	
		for i = 1, table.getn(stringtable) do
			if (i == 8) then
				stringtable[8] = stringtable[8] .. "

";
end;
text = text … " " … stringtable* … " ";
end;

	end;
	
	chatboxtext[1] = text;
	DLabel2:SetText(text);
	//OpenChatBox();
end;
usermessage.Hook("ChatUMSG", DFrame1.UpdateChat)

end;
[/lua]

[lua]
// cl_init
function GM:StartChat( isTeamChat )
LocalPlayer():PrintMessage( 1, “Press escape after you close the chatbox!” );
OpenChatBox();
return true;
end
[/lua]

[lua]
// init.lua
net.Receive( “ChatNMsg”, function(len, ply)
print(“Got Netmessage”)
if (IsValid(ply)) then
for k, v in pairs(ents.FindInSphere(Vector(ply:GetPos().x, ply:GetPos().y, ply:GetPos().z), 100)) do
if (IsValid(v) && v != nil && v:IsPlayer()) then
umsg.Start(“ChatUMSG”, v);
umsg.String(ply:Name() … " Says: " … net.ReadString());
umsg.End();
end;
end;
end;
end);
[/lua]

All files are included and added as clientside & serverside files properly.

Seems like my latest edition of my work crashes GMOD.
Hmm, lucky for me I have MS Visual studio, and it let’s me debug applications which uses the
JIT Just-In-Time compiler. Which made me discover something quiet disturbing…

http://img694.imageshack.us/img694/7122/garryy.png

BambooHome garry?

Code:
[lua]
// vFG FusionBase Chatbox.

print(“CHATBOX LOADED”)

local chatboxpos = {}

chatboxpos.x = 16;
chatboxpos.y = 370;

local labelpositions = {}
labelpositions.label1 = {}

labelpositions.label1.posx = 20;
labelpositions.label1.posy = 200;

local chatboxtext = {"", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”}

function SendChatMessage(ply, text)
if (IsValid(ply)) then
if (text != nil && text != “”) then
net.Start(“ChatNMsg”);
net.WriteString(text);
net.WriteEntity(ply);
net.SendToServer();
end;
end;
end;

function OpenChatBox()

local DFrame1 = vgui.Create('DFrame')
DFrame1:SetSize(518, 262)
DFrame1:SetPos(chatboxpos.x , chatboxpos.y )
DFrame1:SetTitle('My gamemode name not yours')
DFrame1:SetSizable(false)
DFrame1:SetDeleteOnClose(false)
DFrame1:SetDraggable(false)
DFrame1:ShowCloseButton(false)
DFrame1:MouseCapture( false )
DFrame1:SetMouseInputEnabled( false )
DFrame1:SetKeyboardInputEnabled( false )
DFrame1:MakePopup();

local DPanel1 = vgui.Create('DPanel', DFrame1)
DPanel1:SetSize( 630, 30)
DPanel1:SetPos(0, 230)
DPanel1:MouseCapture( false )

DTextEntry1 = vgui.Create('DTextEntry', DFrame1)
DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:SetText('')
DTextEntry1:MouseCapture( true )
DTextEntry1:SetMouseInputEnabled( true )
DTextEntry1.OnEnter = function() DTextEntry1:SetMouseInputEnabled( false ) DTextEntry1:SetKeyboardInputEnabled( false ) SendChatMessage(LocalPlayer(), DTextEntry1:GetValue()) TextEntryLoseFocus(DTextEntry1) CloseDermaMenus() gui.EnableScreenClicker( false ) gui.HideGameUI() DFrame1:Close() end
DTextEntry1:RequestFocus( )

local DLabel1 = vgui.Create('DLabel', DFrame1)
DLabel1:SetPos(labelpositions.label1.posx, labelpositions.label1.posy)
DLabel1:SetText(chatboxtext[1])
DLabel1:SizeToContents()
DLabel1:MouseCapture( false )

local DButton1 = vgui.Create('DButton', DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 25)
DButton1:SetText('Send')
DButton1:MouseCapture( true )
DButton1.DoClick = function() DFrame1:Close() end

local DCombobox = vgui.Create('DComboBox', DFrame1)
DCombobox:SetPos(350, 25)
DCombobox:SetSize(100, 25)
DCombobox.OnMousePressed = function() DCombobox:OpenMenu() end
function DCombobox.OnSelect(Index, Value, Data) end
DCombobox:AddChoice("Chat")
DCombobox:AddChoice("PM")
DCombobox:AddChoice("OOC")
DCombobox:AddChoice("LOOC")
DCombobox:AddChoice("Advert")

function DFrame1.UpdateChat(data)
	local pos = chatboxpos;
	local text = data:ReadString();
	local stringtable;
	print(text)
	print(pos.x .. " " .. pos.y)
	
	surface.SetFont( "Trebuchet24" )
	surface.SetTextColor( Color(255, 255, 255, 255) )
	surface.SetTextPos( 25 , 40 );
	surface.DrawText( "niggerest " .. text );
	
	if (string.len(text) > 20) then
	stringtable = string.Explode(" ", text, false);
	PrintTable(stringtable);
	
	text = "";
	local newlinenumber = 8;
	local multiplyer = 1;
	local newypos = labelpositions.label1.posy + (40 * multiplyer);
	
		for i = 1, table.getn(stringtable) do
			if (i == (newlinenumber * multiplyer)) then
				print(i);
				print(text);
				print("New y pos:" .. newypos);
				print("multi: " .. multiplyer);
				local checkindex = i * multiplyer;
				print("checkindex: " .. checkindex);
				if (checkindex <= table.getn(stringtable)) then
				stringtable[math.Min(checkindex, table.getn(stringtable))] = stringtable[math.Min(checkindex, table.getn(stringtable))] .. "

";
newypos = labelpositions.label1.posy + (40 * multiplyer);
multiplyer = multiplyer + 1;
print(i);
print(text);
print(“New y pos:” … newypos);
print("multi: " … multiplyer);
end;
end;
DLabel1:SetPos(labelpositions.label1.posx, newypos);
text = text … " " … stringtable* … " ";
end;
chatboxtext[1] = text;
DLabel1:SetText(text);
end;
OpenChatBox();
timer.Create(“ShowPlayerTheTextTimer” … data:ReadEntity(), 6, 1, function()
print("Closing: " … data:ReadEntity())
DFrame1:Close();
return;
end)
end;
usermessage.Hook(“ChatUMSG”, DFrame1.UpdateChat);
end;

[/lua]

I suspect the data:ReadEntity().

edit:

I deleted the data:ReadEntity()s.
Now I’ve got a new problem the DFrame won’t close when the timer function executes.
Hmm, it just hit me that it’s local. But the function i call it from is in the scope.

edit:

Nvm making it a global solved it.

Update I just spotted a fatal error in my loops.
Found out that Lua Scripting > What do you need help with?

OMG GUYS, WTF?

Sorry for the language but… When I open the DFrame with a console command, the error doesn’t occur?
Isn’t that weird?

[lua]
function OpenChatBox()

DFrame1 = vgui.Create('DFrame')
DFrame1:SetSize(518, 262)
DFrame1:SetPos(chatboxpos.x , chatboxpos.y )
DFrame1:SetTitle('FusionBase Chatbox - Escape after closing')
DFrame1:SetSizable(false)
DFrame1:SetDeleteOnClose(false)
DFrame1:SetDraggable(false)
DFrame1:ShowCloseButton(false)
DFrame1:MouseCapture( false )
DFrame1:SetMouseInputEnabled( false )
DFrame1:SetKeyboardInputEnabled( false )
DFrame1:MakePopup();

local DPanel1 = vgui.Create('DPanel', DFrame1)
DPanel1:SetSize( 630, 30)
DPanel1:SetPos(0, 230)
DPanel1:MouseCapture( false )

DTextEntry1 = vgui.Create('DTextEntry', DFrame1)
DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:SetText('')
DTextEntry1:MouseCapture( true )
DTextEntry1:SetMouseInputEnabled( true )
DTextEntry1.OnEnter = function() DTextEntry1:SetMouseInputEnabled( false ) DTextEntry1:SetKeyboardInputEnabled( false ) SendChatMessage(LocalPlayer(), DTextEntry1:GetValue()) TextEntryLoseFocus(DTextEntry1) CloseDermaMenus() gui.EnableScreenClicker( false ) gui.HideGameUI() DFrame1:Close() end
DTextEntry1:RequestFocus( )

local DLabel1 = vgui.Create('DLabel', DFrame1)
DLabel1:SetPos(labelpositions.label1.posx, labelpositions.label1.posy)
DLabel1:SetText(chatboxtext[1])
DLabel1:SizeToContents()
DLabel1:MouseCapture( false )

local DButton1 = vgui.Create('DButton', DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 25)
DButton1:SetText('Send')
DButton1.DoClick = function() DFrame1:Close() end

local DCombobox = vgui.Create('DComboBox', DFrame1)
DCombobox:SetPos(350, 25)
DCombobox:SetSize(100, 25)
DCombobox.OnMousePressed = function() DCombobox:OpenMenu() end
function DCombobox.OnSelect(Index, Value, Data) end
DCombobox:AddChoice("Chat")
DCombobox:AddChoice("PM")
DCombobox:AddChoice("OOC")
DCombobox:AddChoice("LOOC")
DCombobox:AddChoice("Advert")

function DFrame1.UpdateChat(data)
	local pos = chatboxpos;
	local text = data:ReadString();
	local stringtable;
	print(text)
	print(pos.x .. " " .. pos.y);
	
	if (string.len(text) > 20) then
	stringtable = string.Explode(" ", text, false);
	PrintTable(stringtable);
	
	text = "";
	local newlinenumber = 8;
	local multiplyer = 1;
	local newypos = labelpositions.label1.posy + (40 * multiplyer);
	
		for i = 1, table.getn(stringtable) do
		print("I number: " .. i) 
			if (i == (newlinenumber * multiplyer)) then
				print(i);
				print(text);
				print("New y pos:" .. newypos);
				print("multi: " .. multiplyer);
				local checkindex = i * multiplyer;
				print("checkindex: " .. checkindex);
				if (checkindex <= table.getn(stringtable)) then
				stringtable[math.Min(checkindex, table.getn(stringtable))] = stringtable[math.Min(checkindex, table.getn(stringtable))] .. "

";
newypos = labelpositions.label1.posy + (40 * multiplyer);
multiplyer = multiplyer + 1;
print(i);
print(text);
print(“New y pos:” … newypos);
print("multi: " … multiplyer);
end;
end;
DLabel1:SetPos(labelpositions.label1.posx, newypos);
text = text … " " … stringtable* … " ";
end;
chatboxtext[1] = text;
DLabel1:SetText(text);
end;
OpenChatBox();
timer.Create(“ShowPlayerTheTextTimer”, 6, 1, function()
DFrame1:Close();
return;
end)
end;
usermessage.Hook(“ChatUMSG”, DFrame1.UpdateChat);
end;
concommand.Add(“OpenDerma”, OpenChatBox)
[/lua]

I guess it’s fixed but, please, try it yourself and tell me why this made it work.

Edit:
NO, It’s not fixed when i type the command name in console it functions properly, but when I ask my cl_init.lua to do it nope.

[lua]
// cl_init.lua
function GM:StartChat( isTeamChat )
LocalPlayer():PrintMessage( 1, “Press escape after you close the chatbox!” );
timer.Create(“DelayTimer”, 1, 1, function() // The timer was an attempt to fix it, but removing it won’t work neither.
LocalPlayer():ConCommand(“OpenDerma”);
end)
return true;
end
[/lua]

Why is that?

[editline]30th December 2012[/editline]

It seems like my main problem can’t be solved, I’m going to continue with a bugged chatbox then.

[lua]
gui.EnableScreenClicker( false )
self:KillFocus()
self:SetKeyboardInputEnabled( false )
self:SetMouseInputEnabled( false )
RunConsoleCommand( “say”, self.TextEntry:GetValue() )[/lua]

is what I use

self:KillFocus()
That might be what you are missing for the having to press esc problem. And you should restructure your code in a way easier to use.

Such as, a function for Creating the chat box, not setting it visible, not opening it, just creating it when the client joins the server.
[lua]
function CreateChatBoxNess()
–Create Chat Box
end
[/lua]
And when you are creating it, make sure you add an open and close function to the Frame of the chat box. Such as how you use the OnEnter function for DTextEntry. An open and close function would ideally look like this:
[lua]
DFrame.Open = function()
–Restore mouse
–Restore keyboard
–Request focus
–Set Visible True
end

DFrame.Close = function()
–Remove mouse
–Remove keyboard
–kill focus
–Set Visible False
end
[/lua]
This way, once your chat box is create, those functions are built in with it and can be used in another function**(As long as DFrame is global and not local)**

Now that you have created the chat box, created open an close functions for it, you can start using it.
[lua]
DFrame:Open()

DFrame:Close()
[/lua]

Why this thread is such a mess?

The only thing you need for your own chatbox is first suppressing the default chatbox(via the provided hooks) then you require an input field(which you probably should derive from a TextEntry, if not just use the PANEL hooks on the wiki, they allow you to hook to each single key without making a huge fucking mess.)

Thanks for your advice guys.

Well, you see. I wanted a chatbox there is designed for RP gamemodes with limited chat radius.
I tried to make what you’re saying before I opened this thread. But the only option of adding text to the chatbox is with chat.AddText and I’m afraid that it adds to all the player’s chatboxes.

ignore the currency shit.
ignore the useless tables they’re for something else.

to be more precise I had this before
[lua]
// cl_init.lua

CurValues = {};
CurValues.Dollar = 0.76; // All this is in reference to their value vs the euro.
CurValues.Euro = 1.00;
CurValues.GreatBritishPound = 1.22;
CurValues.DKK = 7.50; // Yea Denmark Sucks.

talkrange = 500; // The only variable up here you need to use.

ENumerableKeys = {KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z };

KeyCharacters = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”};

function Talk(ply, text)
if (IsValid(ply)) then
net.Start(“ChatNmsg”);
net.WriteString(text);
net.SendToServer();
chat.AddText(Color(255, 255, 255, 255), ply:Name() … " : " … text );
end
end

function CurrencyAwnserServer()

chat.AddText( Color( 195, 60, 75, 255 ), "[Government Broadcast] The national currency has been set to " .. CurrentCurrency  .. "!")

net.Start("CurNmsg");
net.WriteString( "CurSet" )
net.SendToServer();

end

function ChatCommands() // Prints all the commands for the player.

chat.AddText( Color( 255, 255, 255, 255 ), "Commands: " .. "

" … “/setcurrency; Sets the server currency.” … "
" … “.// or /looc; Writes your text in the Local Out-of-character chat.” … "
" … “/ooc or //; Writes your text in OOC chat.”
… “/pm (name, text); Write a message to a player.”)

end

function ChatRecieve(data)

local text = data:ReadString();
local ply = data:ReadEntity();

chat.AddText(Color(255, 255, 255, 255), ply, ": ", text);

end
usermessage.Hook(“ChatUMSG”, ChatRecieve);

function GM:OnPlayerChat( ply, strText, bTeamOnly, bPlayerIsDead )

if (IsValid(ply)) then
	
	if (ply:IsAdmin() || ply:IsSuperAdmin()) then
		
		if (editcurrency == true) then
		
			for i = 1, table.Count(CurrenciesSymbols) do
				
				if (CurrenciesSymbols* == strText) then
				CurrentCurrency = strText;
				print("The currency has been set.")
				editcurrency = false;
				CurrencyAwnserServer(); // AWNSER THAT GOD DAMN SERVER!
				strText = "";
				i = i + 1;
				return true;
				end
			end
		end
	end
	
	if (strText == "/commands") then
		ChatCommands();
		return true;
	end
	
	if (string.GetChar( string.lower(strText), 1 ) == "/" && string.GetChar( string.lower(strText), 2 ) == "/") then
	strText = string.Replace(strText, "//", "")
		chat.AddText(Color(255, 255, 255, 255), ply:Name() .. " OOC: " .. strText);

		return true;
	end
	
	if (string.GetChar( string.lower(strText), 1 ) == "/" && string.GetChar( string.lower(strText), 2 ) == "o" && string.GetChar( string.lower(strText), 3 ) == "o" && string.GetChar( string.lower(strText), 4 ) == "c") then
		strText = string.Replace(strText, "/ooc", "")
		chat.AddText( Color( 255, 255, 255, 255 ), ply:Name() .. " OOC: " .. strText)
		return true;
	end
	
	if (string.GetChar( string.lower(strText), 1 ) == "/" && string.GetChar( string.lower(strText), 2 ) == "p" && string.GetChar( string.lower(strText), 3 ) == "m") then
	
	string.Replace(strText, "/pm ", "")
	
	local tabeltext = string.Explode(strText, " ");
	
		for k, v in pairs(player.GetAll()) do
		
		if (string.gmatch( strText, v:Name() ) == string.len(v:Name())) then
			
			for i = 1, string.len(v:Name()) do
			
			if (string.GetChar(string.lower(strText), i) == string.GetChar(v:Name(), i)) then
			i = i + 1;
			else
				chat.AddText( Color( 255, 100, 75, 255 ), "There is no such player by the name of " .. tabeltext[1] .. ".");
			end
				
			end
		end
	end	
end
	
	if (string.match(strText, "/", 1) || strText == "/") then
		chat.AddText( Color( 255, 0, 0, 255 ), "THIS COMMAND DOESN'T EXSIST?!");
		return true;
	end
end

Talk(ply, strText)

return true;

end

function ListenCurUmsgHook(data)
editcurrency = data:ReadBool();
end
usermessage.Hook(“CurUmsg”, ListenCurUmsgHook)

function ListenGuestUmsgHook()
if (LocalPlayer().IsValid() && LocalPlayer():IsPlayer()) then
chat.AddText( Color( 100, 100, 255, 255 ), "Welcome " … LocalPlayer():GetName() … “.”);
end
end
usermessage.Hook(“GuestJoinMSG”, ListenGuestUmsgHook)
concommand.Add(“test_guestchat”, ListenGuestUmsgHook)
[/lua]

init.lua
[lua]
net.Receive( “ChatNMsg”, function(len, ply)
print(“Got Netmessage”)
if (IsValid(ply)) then
for k, v in pairs(ents.FindInSphere(Vector(ply:GetPos().x, ply:GetPos().y, ply:GetPos().z), 100)) do
if (IsValid(v) && v != nil && v:IsPlayer()) then
umsg.Start(“ChatUMSG”, v);
umsg.String(net.ReadString());
umsg.Entity(ply);
umsg.End();
end;
end;
end;
end)
[/lua]

Expect it not to work because most of it is replaced with the current code.
But the issue is all players receive the chat messages.
Or are you talking about a completely other way of doing it?
My teachers have always said that I make things way too complicated.

EDIT:

Okay I think I’ve made what Drakehawke & find me said, I think.
But the result is still that after I execute the Derma1.Exit() the controls are not working and my mouse is still showing until I press escape.

[lua]
function CreateDerma()
DFrame1 = vgui.Create(‘DFrame’);
DFrame1:SetSizable(false);
DFrame1:SetDeleteOnClose(true);
DFrame1:SetDraggable(false);
DFrame1:ShowCloseButton(true);
DFrame1:MouseCapture( false );
DFrame1:SetMouseInputEnabled( false );
DFrame1:SetKeyboardInputEnabled( false );

DCombobox = vgui.Create('DComboBox', DFrame1);
DLabel1 = vgui.Create('DLabel', DFrame1);
DTextEntry1 = vgui.Create('DTextEntry', DFrame1);
	
 // Panel functions
DFrame1.Exit = function()

DFrame1:Close();
DFrame1:KillFocus();
DFrame1:SetKeyboardInputEnabled( false );
DFrame1:SetMouseInputEnabled( false );
RunConsoleCommand( "say", DTextEntry1:GetValue() );
TextEntryLoseFocus(DTextEntry1);

DTextEntry1:KillFocus();
DTextEntry1:SetKeyboardInputEnabled( false );
DTextEntry1:SetMouseInputEnabled( false );

DButton1:KillFocus();
DButton1:SetKeyboardInputEnabled( false );
DButton1:SetMouseInputEnabled( false )

DCombobox:KillFocus();
DCombobox:SetKeyboardInputEnabled( false );
DCombobox:SetMouseInputEnabled( false );

DLabel1:KillFocus();
DLabel1:SetKeyboardInputEnabled( false );
DLabel1:SetMouseInputEnabled( false );

gui.EnableScreenClicker( false );

end;

DFrame1.Open = function()

DFrame1:SetSize(518, 262)
DFrame1:SetPos(chatboxpos.x , chatboxpos.y )
DFrame1:SetTitle('FusionBase Chatbox - Escape after closing')
gui.EnableScreenClicker( true );
DFrame1:SetKeyboardInputEnabled( true );
DFrame1:SetMouseInputEnabled( true );
DFrame1:MakePopup();

DButton1 = vgui.Create('DButton', DFrame1)
DButton1:SetSize(70, 25)
DButton1:SetPos(450, 25)
DButton1:SetText('Send')
DButton1.DoClick = function() DFrame1.Exit() end


DCombobox:SetPos(350, 25)
DCombobox:SetSize(100, 25)
DCombobox.OnMousePressed = function() DCombobox:OpenMenu() end
function DCombobox.OnSelect(Index, Value, Data) end
DCombobox:AddChoice("Chat")
DCombobox:AddChoice("PM")
DCombobox:AddChoice("OOC")
DCombobox:AddChoice("LOOC")
DCombobox:AddChoice("Advert")

DLabel1:SetPos(labelpositions.label1.posx, labelpositions.label1.posy)
DLabel1:SetText('')
DLabel1:SizeToContents()

DTextEntry1:SetSize(490, 25)
DTextEntry1:SetPos(0, 232)
DTextEntry1:MouseCapture( true )
DTextEntry1:SetMouseInputEnabled( true )
DTextEntry1:RequestFocus();
	DTextEntry1.OnEnter = function()
		SendChatMessage(LocalPlayer(), DTextEntry1:GetValue())
		DFrame1.Exit();
	end
end;
DFrame1.Open();

end;
[/lua]

But Wizzard you’re trying to make me do something else than those two.

Firstly, chat.AddText is clientside only, which mean it only prints on the client that called it.
Secondly, derma panels follow a hierarchy. So, if you close the parent of all the other panels (DFrame1), all the other will be closed too (only if the hierarchy from the child leads to DFrame1).
Thirdly, what Wizard said. You have to hook onto the client’s chat hooks. I think they are StartChat, ChatText, ChatTextChanged, FinishChat and OnChatTab.
Forthly, that is a huge mess. I would recommend redoing it whole.

Thanks for clearing that up.

When I tried it on a lan server the messages were printed on us both although only one of us were told to print the messages though.

I did as you suggested Danielga & Wizzard of ass.
And I can say that it was…
Completely successful.

I found out that the reason that the player’s were reciving the message twice was because of a callback from the players who received it.

In the maybe weird code below the original sender will be the first to send a message and therefore always have the number of 0 which is incremented to one.

Now when the number is 2 because of the client callback. I use a return statement to stop him.
And set the number to 0 again so chat can continue.

init.lua
[lua]
local number = 0;

net.Receive( “ChatNMsg”, function(len, ply)

local text = net.ReadString();
local sendentity = net.ReadString();

if (number >= 1) then
	number = 0;
	return;
	else
	number = number + 1;
end;

if (IsValid(ply) && ply:IsPlayer() && number == 1) then
for k, v in pairs(ents.FindInSphere(Vector(ply:GetPos().x, ply:GetPos().y, ply:GetPos().z), 100)) do
if (IsValid(v) && v != nil && v:IsPlayer() && ply:IsPlayer()) then
umsg.Start(“ChatUMSG”, v);
umsg.String(text);
umsg.String(sendentity);
umsg.End();
end;
end;

	else 
	print("Currenter Number: " .. number)
	number = 0;
	return;
end;

end);
[/lua]

chatbox.lua
[lua]
function SendChatMessage(ply, text)
if (IsValid(ply)) then
if (text != nil && text != “”) then
net.Start(“ChatNMsg”);
net.WriteString(text);
net.WriteString(ply:GetName());
net.SendToServer();
end;
end;
end;

function GM:OnPlayerChat( ply, msg, teamonly, dead )
print(ply:Name() … " Has written a message: " … msg)
if (dead) then
chat.AddText(Color(230, 100, 100, 255), “You can’t type when you’re dead!”);
else
SendChatMessage(ply, msg);
end
return true;
end

function ChatRecieve(data)

local text = data:ReadString();
local ply = data:ReadString();

chat.AddText(Color(255, 255, 255, 255), ply, " Says: ", text);

print(LocalPlayer():Name() … " Has Received: " … text … " From player: " … ply);

end
usermessage.Hook(“ChatUMSG”, ChatRecieve);
[/lua]

So the status is that I got my chatbox.
Thanks to all who participated in this thread.
Tell me if I could do anything better.
Happy new year & have a good one!