How to create a HUD toggle bind in Source engine games?

HUD is disabled with the cl_drawhud 0
command and enabled again with cl_drawhud 1
. Additionally r_drawviewmodel 0
and r_drawviewmodel 1
can be used to toggle the viewmodel. Instead of pasting the commands into the console every time I want to capture a screenshot I'd prefer a hotkey to toggle (H would be sensible) which can be achieved by using alias, I believe.
Best Answer
You can use the following commands in your Source game (tested in Half-Life 2: Episode 2). Paste and enter one line at a time:
alias noHUD "HUD_toggle1"
alias HUD_toggle1 "cl_drawhud 0; r_drawviewmodel 0; alias noHUD HUD_toggle2"
alias HUD_toggle2 "cl_drawhud 1; r_drawviewmodel 1; alias noHUD HUD_toggle1"
bind "h" noHUD
The names of the functions eventually don't really matter.
Pictures about "How to create a HUD toggle bind in Source engine games?"



Creating A Heads Up Display (HUD) Part 1 - #46 Unreal Engine 4 Beginner Tutorial Series
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Andrea Piacquadio, Andrea Piacquadio, Andrea Piacquadio, Andrea Piacquadio