| 1 |
__all__ = ('KeyListener', 'FrameCallback') |
| 2 |
|
| 3 |
from jnius import PythonJavaClass, java_method |
| 4 |
|
| 5 |
|
| 6 |
class KeyListener(PythonJavaClass): |
| 7 |
""" |
| 8 |
Represents a Python implementation of an Android `OnKeyListener`. |
| 9 |
|
| 10 |
This class provides an interface for handling key events in an Android environment |
| 11 |
using Python. It acts as a bridge between Python and the Android KeyListener |
| 12 |
interface. The primary purpose is to allow the execution of a Python function |
| 13 |
each time a key event is triggered. |
| 14 |
|
| 15 |
The class is structured to integrate with the Java ecosystem and is instantiated |
| 16 |
with a listener function defined in Python. This function enables the handling of |
| 17 |
key events by passing relevant details to it, empowering flexible event handling |
| 18 |
in a Pythonic manner. |
| 19 |
|
| 20 |
Attributes: |
| 21 |
listener (Callable): A Python callable function that will be triggered |
| 22 |
when a key event occurs. The function is expected to have three parameters: |
| 23 |
- v (android.view.View): The view that triggered the key event. |
| 24 |
- key_code (int): The key code of the key event. |
| 25 |
- event (android.view.KeyEvent): The Android KeyEvent object. |
| 26 |
""" |
| 27 |
|
| 28 |
__javacontext__ = 'app' |
| 29 |
__javainterfaces__ = ['android/view/View$OnKeyListener'] |
| 30 |
|
| 31 |
def __init__(self, listener): |
| 32 |
self.listener = listener |
| 33 |
|
| 34 |
@java_method('(Landroid/view/View;ILandroid/view/KeyEvent;)Z') |
| 35 |
def onKey(self, v, key_code, event): |
| 36 |
return self.listener(v, key_code, event) |
| 37 |
|
| 38 |
|
| 39 |
class FrameCallback(PythonJavaClass): |
| 40 |
__javacontext__ = 'app' |
| 41 |
__javainterfaces__ = ['android/view/Choreographer$FrameCallback'] |
| 42 |
|
| 43 |
def __init__(self, do_frame): |
| 44 |
self.do_frame = do_frame |
| 45 |
|
| 46 |
@java_method('(J)V') |
| 47 |
def doFrame(self, frame_time_nanos): |
| 48 |
self.do_frame(frame_time_nanos) |