[
Date Prev][Date Next][
Thread Prev][Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [ease-dev] Button Keylistener Python
|
Hi Jonah,
Thanks for the explanation. It is working now!
Code:
--------------------------------------------------------------------------------------
# name : Keylistener Example
loadModule('/System/UI Builder')
loadModule('/System/UI')
def callback():
print('Button clicked')
class MyKeyListener():
def keyPressed(self, e):
pass
def keyReleased(self, e):
key_code = py4j.java_gateway.get_field(e,'keyCode')
print(f"Key released: {key_code}")
if key_code == 13: # CR
print("CR released.")
callback()
class Java:
implements = ['org.eclipse.swt.events.KeyListener']
createView('Example')
textBox = createText("")
button = createButton('DoIt', 'callback()')
executeUI(f'''
textBox.addKeyListener(MyKeyListener())
''')
-----------------------------------------------------------------------------------------------
BR,
Raphael
Am Fr., 10. Jan. 2025 um 17:49 Uhr schrieb Jonah Graham
<jonah@xxxxxxxxxxxxxxxx>:
>
> Hi Raphael,
>
> Thanks for reporting back what the change was that resolved your problem.
>
> As for fields, when using CPython with EASE, the underlying Python <-> Java bridge is provided by Py4J. Please have a look at https://www.py4j.org/faq.html#how-to-access-a-field for accessing fields.
>
> Note: This documentation link mentions "auto_field" as an option. Unfortunately EASE doesn't let you change that. The parameter would need to be passed in when starting py4j here in the code. You could consider making a pull request, or just stick with the "get_field" way of accessing the fields.
>
> HTH
> Jonah
>
> ~~~
> Jonah Graham (he/him)
> Kichwa Coders
> www.kichwacoders.com
>
>
> On Fri, 10 Jan 2025 at 02:54, Raphael Geissler <raphael.geissler@xxxxxxxxx> wrote:
>>
>> Hi Jonah,
>>
>> thanks for the link.
>>
>> class Java:
>> implements = ['org.eclipse.swt.events.KeyListener']
>>
>> does fix it.
>>
>> I have one other question. How are public fields of a Java class
>> mapped to Python.
>> My Code looks now like this:
>> ------------------------------------------------------------------------------------
>> # name : Keylistener Example
>>
>> loadModule('/System/UI Builder')
>> loadModule('/System/UI')
>>
>> class MyKeyListener():
>> def keyPressed(self, e):
>> print(f"Key pressed: {e.keyCode}")
>>
>> def keyReleased(self, e):
>> print(f"Key released: {e}")
>>
>> class Java:
>> implements = ['org.eclipse.swt.events.KeyListener']
>>
>> createView('Example')
>> textBox = createText("")
>> executeUI(f'''
>> textBox.addKeyListener(MyKeyListener())
>> ''')
>> ------------------------------------------------------------------------------------
>> If I press the 'Carriage Return' key, I see this output:
>> ------------------------------------------------------------------------------------
>> Key pressed: <py4j.java_gateway.JavaMember object at 0x0000013CBAFE6C80>
>> Key released: KeyEvent{Text {} time=169739578 data=null character='
>> '=0xd keyCode=0xd keyLocation=0x0 stateMask=0x0 doit=true}
>> ------------------------------------------------------------------------------------
>>
>> As you can, if I print the KeyEvent class, there is a member keyCode,
>> but if I print the keyCode, I get this strange output:
>> <py4j.java_gateway.JavaMember object at 0x0000013CBAFE6C80>
>>
>> So, I don't know how I can access the keyCode value.
>>
>> Does someone have an idea, how I can solve this?
>>
>> BR,
>>
>> Raphael
>>
>> Am Do., 9. Jan. 2025 um 16:11 Uhr schrieb Jonah Graham <jonah@xxxxxxxxxxxxxxxx>:
>> >
>> > Hi Raphael,
>> >
>> > I haven't been actively using EASE for sometime, but this sample from the tutorial course I ran at EclipseCon demonstrates using SWT listeners so may be useful to you: https://github.com/jonahgraham/EASE-Python-Examples/blob/master/Solutions/Integrate%20Solutions/listviewer.py
>> >
>> > HTH
>> > Jonah
>> >
>> > ~~~
>> > Jonah Graham (he/him)
>> > Kichwa Coders
>> > www.kichwacoders.com
>> >
>> >
>> > On Thu, 9 Jan 2025 at 09:12, Raphael Geissler via ease-dev <ease-dev@xxxxxxxxxxx> wrote:
>> >>
>> >> Hi!
>> >>
>> >> Thanks for the hint. Unfortunately it doesn't work.
>> >>
>> >> BR
>> >>
>> >> Am Do., 9. Jan. 2025 um 13:52 Uhr schrieb Yvan LUSSAUD <yvan.lussaud@xxxxxxx>:
>> >> >
>> >> > I didn't test it, but there is a method getUiEvent() (found using the
>> >> > Modules Explorer view). You can call it in the callback. Maybe you can
>> >> > call it in your callback() method to distinguish between a click, key
>> >> > pressed etc...
>> >> >
>> >> > Yvan Lussaud.
>> >> >
>> >> > Le jeudi 09 janvier 2025 à 13:32 +0100, Raphael Geissler a écrit :
>> >> > > Hi Yvan,
>> >> > >
>> >> > > you're right. This was not correct, but it does not solve the error
>> >> > > with the MyKeyListener class.
>> >> > >
>> >> > > Do you have any idea how I have to implement this key listener class?
>> >> > >
>> >> > > BR,
>> >> > >
>> >> > > Raphael
>> >> > >
>> >> > > Am Do., 9. Jan. 2025 um 13:28 Uhr schrieb Yvan LUSSAUD
>> >> > > <yvan.lussaud@xxxxxxx>:
>> >> > > >
>> >> > > > Hi Raphael,
>> >> > > >
>> >> > > > You need to write the code of the call back as a String:
>> >> > > >
>> >> > > > button = createButton('DoIt', 'callback()')
>> >> > > >
>> >> > > > regards,
>> >> > > > Yvan Lussaud.
>> >> > > >
>> >> > > > Le jeudi 09 janvier 2025 à 13:03 +0100, Raphael Geissler via ease-
>> >> > > > dev a
>> >> > > > écrit :
>> >> > > > > Hi *,
>> >> > > > >
>> >> > > > > does anyone know, how I can add a key listener to a button within
>> >> > > > > a
>> >> > > > > Python EASE Script. I tried following, but I get following error.
>> >> > > > >
>> >> > > > > Code:
>> >> > > > > -----------------------------------------------------------------
>> >> > > > > ----
>> >> > > > > ------------------
>> >> > > > > # name : Keylistener Example
>> >> > > > > loadModule('/System/UI Builder')
>> >> > > > >
>> >> > > > > def callback():
>> >> > > > > print('Button clicked')
>> >> > > > >
>> >> > > > > class MyKeyListener(org.eclipse.swt.events.KeyListener):
>> >> > > > > def keyPressed(self, event):
>> >> > > > > print("Key pressed:", event.character)
>> >> > > > >
>> >> > > > > def keyReleased(self, event):
>> >> > > > > print("Key released:", event.character)
>> >> > > > >
>> >> > > > > createView('Example')
>> >> > > > >
>> >> > > > > button = createButton('DoIt', callback)
>> >> > > > > button.addKeyListener(MyKeyListener())
>> >> > > > > -----------------------------------------------------------------
>> >> > > > > ----
>> >> > > > > ------------------
>> >> > > > >
>> >> > > > > Error:
>> >> > > > > -----------------------------------------------------------------
>> >> > > > > ----
>> >> > > > > ------------------
>> >> > > > > org.eclipse.ease.ScriptExecutionException: Traceback (most recent
>> >> > > > > call last):
>> >> > > > > File "workspace://ease/working/keylistener_example.py", line 9,
>> >> > > > > in
>> >> > > > > <module>
>> >> > > > > # Jonah Graham (Kichwa Coders) - initial API and implementation
>> >> > > > > TypeError: JavaClass.__init__() takes 3 positional arguments but
>> >> > > > > 4
>> >> > > > > were given
>> >> > > > > -----------------------------------------------------------------
>> >> > > > > ----
>> >> > > > > ------------------
>> >> > > > >
>> >> > > > > Maybe someone has an idea what I do wrong...
>> >> > > > >
>> >> > > > > BR,
>> >> > > > >
>> >> > > > > Raphael
>> >> > > > > _______________________________________________
>> >> > > > > ease-dev mailing list
>> >> > > > > ease-dev@xxxxxxxxxxx
>> >> > > > > To unsubscribe from this list, visit
>> >> > > > > https://www.eclipse.org/mailman/listinfo/ease-dev
>> >> > > >
>> >> >
>> >> _______________________________________________
>> >> ease-dev mailing list
>> >> ease-dev@xxxxxxxxxxx
>> >> To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/ease-dev