티스토리 뷰
키 이벤트를 처리하기 위한 함수 예시 코드
- override fun dispatchKeyEvent(event: KeyEvent): Boolean
- override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean
- override fun onGenericMotionEvent(event: MotionEvent?): Boolean
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
// Check if the event is a key down event
val keyName = KeyEvent.keyCodeToString(event.keyCode)
Log.d("[dispatchKeyEvent()] ", "$keyName @ ${event?.source}")
if (event?.action == KeyEvent.ACTION_DOWN) {
when (event.keyCode) {
KeyEvent.KEYCODE_BACK -> {
// Finish the activity, which will effectively close the app
finish()
return true
}
KeyEvent.KEYCODE_ENTER -> {
if (isWarmingupFinished) {
val intent = Intent(this, GameSelection::class.java)
startActivity(intent)
return true
}
if (isPartImageUpdatable) updatePartImage(false)
if (!isWarmingupStarted) {
isWarmingupStarted = true
ivCountdown.visibility = View.VISIBLE
}
return true
}
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP -> {
// Handle volume keys if needed
return true
}
else -> {
// Handle other keys
val keyName = KeyEvent.keyCodeToString(event.keyCode)
val currentTime = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
displayText = "Key: $keyName\nTime: $currentTime"
Toast.makeText(applicationContext, displayText, Toast.LENGTH_LONG).show()
Log.d("[Key]", keyName)
// Convert the text to a bitmap and set it to the ImageView
return true
}
}
}
// If the event is not handled, let the system handle it
return super.dispatchKeyEvent(event)
}
override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean {
Log.d("[Source]", ev?.source.toString())
if (ev?.source == 8194) {
Log.d("8194", "pushed")
return true
}
return super.dispatchGenericMotionEvent(ev)
}
override fun onGenericMotionEvent(event: MotionEvent?): Boolean {
Log.d("[Source - onGenericMotionEvent]", event?.source.toString())
if (event?.source == 8194) {
Log.d("8194 - onGenericMotionEvent", "pushed")
return false // 이벤트가 처리되었음을 알리기 위해 true를 반환
}
return super.onGenericMotionEvent(event) // 나머지 이벤트는 기본 처리 방식으로 처리
}
반응형
'SWDesk > App' 카테고리의 다른 글
안드로이드앱에서 광고 및 결제를 관리하기 위한 예제 코드 (2) | 2025.01.18 |
---|---|
[Android] ImageView Event Treatment (0) | 2024.06.18 |
[Android] Fragment 개념 (1) | 2024.04.08 |
[Android] 그래프 예제 (2) | 2024.04.06 |
[Android] RecyclerView 예제 (0) | 2024.04.04 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 치매방지
- 절연형
- 배프
- Innovation&Hurdles
- Decorator
- 전류
- 심심풀이
- 아두이노
- image
- 심심풀이치매방지기
- 전압
- 혁신
- 빌리언트
- DYOV
- 치매
- 혁신과허들
- Innovations&Hurdles
- 둎
- 허들
- Innovations
- Hurdles
- bilient
- BSC
- ServantClock
- 오블완
- arduino
- Video
- 티스토리챌린지
- badp
- 전압전류모니터링
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함