Getting Material data of target entity

Hey guys

I’m trying to make an addon that is essentially an edit of the default materials tool. Instead of giving the user the option to choose the material in a window, this is what I want to happen:

Left Click: Sets the material currently loaded
Reload: Clears the override material
Right Click: When used on an entity, it takes the material info of the entity and loads it to be applied with Left Click.

Basically it grabs materials of entities with Right Click and sets them on other props with default mapping with the Left Click.

Now, I’m having trouble with the Right Click - as in getting the material - part.
Lets face it I’m a noob at Lua and want to learn more, so could one of you guys help me out with explaining the structure of how this section of code would work?