How to Perform the click on Android device hardware button ?

For the below code we can perform the click on Device Hardware Button :

For this action I use ” On Key Event ” :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch (keyCode) {

case KeyEvent.KEYCODE_MENU:
Toast.makeText(StudentProfilePage.this, "Menu Button is Pressed", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_HOME:
Toast.makeText(StudentProfilePage.this, "Home Button is Pressed", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_BACK:
Toast.makeText(getBaseContext(), "Back Button is Pressed",Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
Toast.makeText(getBaseContext(), "Volume Down Button is Pressed",Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
Toast.makeText(getBaseContext(), "Volume Up Button is Pressed",Toast.LENGTH_SHORT).show();
return true;
}

return false;
}