Custom Toggle Shortcut (for an operation that doesn't normally toggle)? I'd like to set up a keyboard shortcut that in Object Mode will apply Smooth Shading to a mesh object, or alternately apply Flat Shading if all faces are already smooth.

This would ideally behave similarly to how pressing A will either Select All or Deselect All depending on whether anything is currently selected.

Here's my current setup with two separate shortcuts:



If this were a simple Boolean value situation, like a checkbox that is either on or off, this answer would be a solution. The challenge is that Smooth/Flat is not as simple as yes/no because a mesh can have a combination of both smooth and flat faces.

I'm guessing this will probably involve some scripting because logic will be required to first check the shading of the faces on the mesh. Or maybe (less ideal, but) just apply Smooth Shading the first time the shortcut is pressed, and then alternate between smooth and flat for all subsequent shortcut calls. 

I realize it's a geeky ambition, but can anyone point me in the right direction (if this is even possible)? Thanks.
As you are a veteran here, I will point you in the right direction and when you have it you can post it here as answer:)

You need an operator that doesn't simply do a thing, but does a thing based on condition. That means you will have to script your own operator, and assign a custom shortcut to it:


How to register custom operator
In the execute function, you will have to check the active object and all it's faces and see their smooth attribute: 


How to get Bmesh from active object
Bmesh faces: BMFace.smooth

Based in which state the mesh is, execute


shade_smooth operator
shade_flat operator
How to run an operator: refer to point 1 last line of example code

Assign a custom shortcut to your operator:


Adding a custom shortcut

Turn it into an addon, so you don't have to run the script every time (or also save it in the startup file, check register and enable auto run scripts):


Simple addon example with operator