You can simply do this.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// 1. onKeyDown is a boolean function, which returns the state of the KeyEvent.
// 2. This function is an internal function, that functions outside the actual application.
// 3. When the any Key is pressed, a Toast appears with the following message.
// 4. This code can be used to check if the device responds to any Key.
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> Toast.makeText(applicationContext, "Volume Down Key Pressed", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_VOLUME_UP -> Toast.makeText(applicationContext, "Volume Up Key Pressed", Toast.LENGTH_SHORT).show()
return true
}
}
Let me know the result