One would suppose that this warning would be as old as the first hat, but its actual history appears to have been no earlier than the closing years of the nineteenth century.
What is kept under the hat, of course, is retained within the head; that is, it remains a secret.