| 1 | package imgui.example.android |
| 2 | |
| 3 | import android.app.NativeActivity |
| 4 | import android.os.Bundle |
| 5 | import android.content.Context |
| 6 | import android.view.inputmethod.InputMethodManager |
| 7 | import android.view.KeyEvent |
| 8 | import java.util.concurrent.LinkedBlockingQueue |
| 9 | |
| 10 | class MainActivity : NativeActivity() { |
| 11 | public override fun onCreate(savedInstanceState: Bundle?) { |
| 12 | super.onCreate(savedInstanceState) |
| 13 | } |
| 14 | |
| 15 | fun showSoftInput() { |
| 16 | val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
| 17 | inputMethodManager.showSoftInput(this.window.decorView, 0) |
| 18 | } |
| 19 | |
| 20 | fun hideSoftInput() { |
| 21 | val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
| 22 | inputMethodManager.hideSoftInputFromWindow(this.window.decorView.windowToken, 0) |
| 23 | } |
| 24 | |
| 25 | // Queue for the Unicode characters to be polled from native code (via pollUnicodeChar()) |
| 26 | private var unicodeCharacterQueue: LinkedBlockingQueue<Int> = LinkedBlockingQueue() |
| 27 | |
| 28 | // We assume dispatchKeyEvent() of the NativeActivity is actually called for every |
| 29 | // KeyEvent and not consumed by any View before it reaches here |
| 30 | override fun dispatchKeyEvent(event: KeyEvent): Boolean { |
| 31 | if (event.action == KeyEvent.ACTION_DOWN) { |
| 32 | unicodeCharacterQueue.offer(event.getUnicodeChar(event.metaState)) |
| 33 | } |
| 34 | return super.dispatchKeyEvent(event) |
| 35 | } |
| 36 | |
| 37 | fun pollUnicodeChar(): Int { |
| 38 | return unicodeCharacterQueue.poll() ?: 0 |
| 39 | } |
| 40 | } |