That would be the court’s decision based on the testimony of the accused and the mental health professional. But at least it is due process. And according to settled law, there must be a compelling reason to deny someone their rights.
Something needs to trigger the mental health exam. Do we depend on the shooter himself or his family? The track record on that is extremely poor. Outsiders? What gives them the authority? Who provides these exams? Who pays for it? At what level is it OK to take someone's rights away? It's a solution that sounds great in theory but had too many questions that need to be answered if it is to be implemented.