DButton problem (not calling OnMouseReleased when it should)

I have a DButton that uses different variables contained in itself to print certain things in the Paint function of it.



concommand.Add( "annoyingpanelproblem", function()

local PaintBorder = vgui.Create( "DFrame" )
PaintBorder:SetSize((ScrW()-100),(ScrH()-100))
PaintBorder:Center()
PaintBorder:MakePopup()

local button = vgui.Create( "DButton", PaintBorder )
	button:Dock(FILL)

button.OnMousePressed = function(b,m)
	if(m==MOUSE_LEFT) then
		b.IsLDown = true
	end
	 if(m==MOUSE_RIGHT) then
		b.IsRDown = true
	end
end

button.OnMouseReleased = function(b,m)
	if(m==MOUSE_LEFT) then
		b.IsLDown = false
	end
	if(m==MOUSE_RIGHT) then
		b.IsRDown = false
	end
end

button.Paint = function( self, w, h )
draw.RoundedBox( 8, 0, 0, w, h, Color( 255, 255, 255, 255 ))
	if(self.IsLDown) then
		print('Button is being left clicked')
	elseif(self.IsRDown) then
		print('Button is being right clicked')
	end
end

end )


Ignore all the functions apart from OnMousePressed, OnMouseReleased and the button.Paint functions.
This code works fine and behaves how it should, printing the right thing repetitively for the right amount of time- there’s only one problem:

If you hold down the button (with either click), them move your cursor off it and let go, the OnMouseReleased function never gets called, so the button keeps printing what it should. I’m not sure why it is behaving like this, so any insights would be extremely helpful.

Becuase OnMouseRelease gets called only when a mouse is released over the button, Same as if you click off the button and drag on it and let go, Then it will be called. You should check.

Use http://wiki.garrysmod.com/page/GM/KeyRelease to check when they release it.

Could you provide an example relative to my code, please?
I’m a bit worried that KeyRelease won’t work with panels…

It works with anything, It does not get called on a panel it gets called when ever someone releases a key, Read the wiki page

I found an alternate method to get the panel to work-



button.Paint = function( self, w, h )
draw.RoundedBox( 8, 0, 0, w, h, Color( 255, 255, 255, 255 ))
if !input.IsMouseDown( MOUSE_LEFT ) then self.IsLDown = false end
if !input.IsMouseDown( MOUSE_RIGHT ) then self.IsRDown = false end
	if(self.IsLDown) then
		print('Button is being left clicked')
	elseif(self.IsRDown) then
		print('Button is being right clicked')
	end
end


I think I’ll use this way instead.